2

I have an activity that the user switches to after logging in that has a Google Maps fragment and I want to change the default location that the map loads too, however, the Map seems to not be responding to any commands. Below is the code for the whole activity, I don't have much yet.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.Gms.Maps;
using Android.Gms.Maps.Model;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace TriTrack
{
    [Activity(Label = "MapsActivity")]
    public class MapsActivity : Activity, IOnMapReadyCallback
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Map);
            MapFragment mapFragment = (MapFragment)FragmentManager.FindFragmentById(Resource.Id.map);
            mapFragment.GetMapAsync(this);
        }

        public void OnMapReady(GoogleMap googleMap)
        {
            LatLng latlng = new LatLng(Convert.ToDouble(13.0291), Convert.ToDouble(10.2083));  
            CameraUpdate camera = CameraUpdateFactory.NewLatLngZoom(latlng, 15);  
            googleMap.MoveCamera(camera);  
            MarkerOptions options = new MarkerOptions().SetPosition(latlng).SetTitle("Chennai");
            googleMap.AddMarker(options);  
        }
    }
}

The map does not move nor does it place a marker. Any ideas? Thanks in advance.

FreakyAli
  • 13,349
  • 3
  • 23
  • 63
B. Morris
  • 75
  • 1
  • 7

1 Answers1

3

You haven't show your configure, so I will show you the process I create the app:

Here is my Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
          android:versionCode="1" 
          android:versionName="1.0" 
          package="App46.App46" 
          android:installLocation="auto">
    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="27" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <application android:allowBackup="true" 
               android:icon="@mipmap/ic_launcher" 
               android:label="@string/app_name" 
               android:roundIcon="@mipmap/ic_launcher_round" 
               android:supportsRtl="true" 
               android:theme="@style/AppTheme">
        <meta-data android:name="com.google.android.geo.API_KEY" android:value="@string/google_maps_key" />
    </application>
</manifest>

Layout:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

MainActivity:

using Android.App;
using Android.Widget;
using Android.OS;
using Android.Support.V7.App;
using Android.Gms.Maps;
using Android.Gms.Maps.Model;
using System;

namespace App46
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : AppCompatActivity, IOnMapReadyCallback
    {
        public void OnMapReady(GoogleMap googleMap)
        {
            LatLng latlng = new LatLng(Convert.ToDouble(13.0291), Convert.ToDouble(10.2083));
            CameraUpdate camera = CameraUpdateFactory.NewLatLngZoom(latlng, 15);
            googleMap.MoveCamera(camera);
            MarkerOptions options = new MarkerOptions().SetPosition(latlng).SetTitle("Chennai");
            googleMap.AddMarker(options);
        }

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);
            SupportMapFragment mapFragment = (SupportMapFragment)SupportFragmentManager.FindFragmentById(Resource.Id.map);
            mapFragment.GetMapAsync(this);
        }
    }
}

Also, I have provide a demo on github for you to test.

Robbit
  • 4,300
  • 1
  • 13
  • 29