Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use .GetMapAsync instead .getMap method with Google Play Services (Xamarin)

An old code works perfectly in this way:

LatLng location = new LatLng (myClass.myLocation.Latitude, myClass.myLocation.Longitude);
CameraPosition.Builder builder = CameraPosition.InvokeBuilder ();
builder.Target (location);
builder.Zoom (18);
CameraPosition cameraPosition = builder.Build ();
MapsInitializer.Initialize (this);
CameraUpdate cameraUpdate = CameraUpdateFactory.NewCameraPosition (cameraPosition);
MapFragment googleMap = FragmentManager.FindFragmentById<MapFragment> (Resource.Id.map);
theMap = googleMap.Map;
if (theMap != null) {
    theMap.MapType = GoogleMap.MapTypeNormal;
    theMap.MoveCamera (cameraUpdate);
}

but now that the .Map is obsolete and deprecated, I must to use .GetMapAsync in some way:

theMap = googleMap.GetMapAsync (IOnMapReadyCallback);

But I don't understand how.

There is somebody that can help me?

like image 303
Blasco73 Avatar asked Jan 27 '15 14:01

Blasco73


Video Answer


3 Answers

Your map fragment class must implement OnMapReadyCallback and override onMapReady():

@Override
public void onMapReady(final GoogleMap map) {
    this.map = map;
    map.setMyLocationEnabled(true);
}

In your onCreateView use getMapAsync() to set the callback on the fragment:

MapFragment mapFragment = (MapFragment) getFragmentManager()
    .findFragmentById(R.id.map); mapFragment.getMapAsync(this);

All that you need to implement the google maps V2 is here: https://developers.google.com/maps/documentation/android/map

like image 157
Nomar Avatar answered Nov 07 '22 05:11

Nomar


This might be late reply to your question, but may helpful to somebody else with the same issue. GetMapAsync() expects implementation of callback object of type IOnMapReadyCallback.

Find more detail here in my blog entry: http://appliedcodelog.com/2015/08/androidgmsmapsmapfragmentmap-is.html

    //OnMapReadyClass
public class OnMapReadyClass :Java.Lang.Object,IOnMapReadyCallback
    { 
  public GoogleMap Map { get; private set; }
  public event Action<GoogleMap> MapReadyAction;

  public void OnMapReady (GoogleMap googleMap)
     {
        Map = googleMap; 

        if ( MapReadyAction != null )
           MapReadyAction (Map);
     }
    }

Now Call the GetMapAsync() and event Action return map instance on successful Map initialization.

    //MyActivityClass.cs 
GoogleMap map;
bool SetUpGoogleMap()
   {
     if(null != map ) return false;

     var frag = FragmentManager.FindFragmentById<mapfragment>(Resource.Id.map);
     var mapReadyCallback = new OnMapReadyClass();

     mapReadyCallback.MapReadyAction += delegate(GoogleMap googleMap )
       {
        map=googleMap; 
       };

     frag.GetMapAsync(mapReadyCallback); 
     return true;
   }
like image 31
Suchith Avatar answered Nov 07 '22 04:11

Suchith


Please see this:

public class MapActivity : Activity, IOnMapReadyCallback
{

 public void OnMapReady(GoogleMap googleMap)
 {
        if (googleMap != null)
        {
           //Map is ready for use
        }
    }
  }

Hope this will help you.

like image 34
Daniel Krzyczkowski Avatar answered Nov 07 '22 03:11

Daniel Krzyczkowski