Question

I'm trying to use google maps version 2 by this code:

    public   void setMapData()
    {
        try {
            if (mapView == null && _mapFragment != null)
            {
                mapView = _mapFragment.Map;
                if (mapView != null)
                {
                    CameraPosition currentPlace = new CameraPosition.Builder()
                        .Target (currentProperty.Location)
                            .Bearing (0) 
                            .Zoom (((RPLApp)pshowAct.Application).typeOfShow .PropertyZoomLevel) //apply zoom here
                            .Build ();

                    mapView.AnimateCamera  (CameraUpdateFactory.NewCameraPosition (currentPlace));

                    mapView .Clear ();
                    MarkersHelperClass.DrawMarkerOnLocation (pshowAct , mapView, currentProperty.Location);
                }

            }
        } catch (Exception ex) {
            RltLog .HandleException (ex);
        }
    }

but mapView = _mapFragment.Map; always return null. at below url has a solution for java codes but it is not working for monodroid. How can we use it at monodroid or c#? It is not suitable because the code defined a new class inside and activity. How we can make it friend with C# syntax?

https://stackoverflow.com/a/14072348/1939409

Was it helpful?

Solution

What component have you using for Google Maps? Have you tried Google Play Services? How to implement google play services using android sdk 17 on xamarin

EDIT: Here is my layout (Resource.Layout.GoogleMap):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
        <FrameLayout
            android:id="@+id/mapView"
            android:layout_width="fill_parent"
            android:layout_height="match_parent" />
            //other controls
</RelativeLayout>

And my code:

public class GoogleMapActivity : Android.Support.V4.App.FragmentActivity
{
    private Android.Gms.Maps.GoogleMap _mapView;
    private Android.Gms.Maps.SupportMapFragment _fragment;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.GoogleMap);

        var mapOptions = new Android.Gms.Maps.GoogleMapOptions()
            .InvokeMapType(Android.Gms.Maps.GoogleMap.MapTypeNormal)
            .InvokeZoomControlsEnabled(false)
            .InvokeCompassEnabled(true);

        var fragTx = SupportFragmentManager.BeginTransaction();
        var mapFragment = Android.Gms.Maps.SupportMapFragment.NewInstance(mapOptions);
        fragTx.Add(Resource.Id.mapView, mapFragment, "mapView");
        fragTx.Commit();
    }

    protected override void OnResume()
    {
        base.OnResume();

        _fragment = ((Android.Gms.Maps.SupportMapFragment)SupportFragmentManager.FindFragmentById(Resource.Id.mapView));
        _mapView = _fragment.Map;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top