Use the .GetMapAsync method instead of .getMap with Google Play services (Xamarin)

Old code works fine 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 .Map deprecated and deprecated, I have to somehow use .GetMapAsync :

 theMap = googleMap.GetMapAsync (IOnMapReadyCallback); 

But I do not understand how.

Is there anyone who can help me?

+6
source share
5 answers

The 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 to the fragment:

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

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

+17
source

It may be late to answer your question, but it may help someone else with the same problem. GetMapAsync () expects an implementation of a callback object of type IOnMapReadyCallback.

Read more 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 () instance and the Action Action map instance when the map is successfully initialized.

  //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; } 
+3
source

Please see the following:

 public class MapActivity : Activity, IOnMapReadyCallback { public void OnMapReady(GoogleMap googleMap) { if (googleMap != null) { //Map is ready for use } } } 

Hope this helps you.

+1
source

If you want to use a view instead of a map fragment, then this is the implementation I applied to get this transition from a project that used the previous version of game services. Updated to 9.6.0 to match the new FCM and caused problems in our map fragments.

 private MapView mapView; private GoogleMap mMap; public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // inflate and return the layout // inflate and return the layout View v = inflater.inflate(R.layout.mapFragment, container, false); mapView = (MapView) v.findViewById(R.id.map); mapView.onCreate(savedInstanceState); mapView.onResume(); mapView.getMapAsync(this); return v; } @Override public void onMapReady(GoogleMap map) { mMap = map; } 

And in your fragment:

 <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/Sismos" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFF" android:orientation="vertical" > <com.google.android.gms.maps.MapView android:id="@+id/mapview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </FrameLayout> 

It is important to note the following permissions for your manifest:

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-feature android:glEsVersion="0x00020000" android:required="true" /> 

I managed to achieve this setup without any errors in API 23 and the following libs on my Gradle:

 compile 'com.google.android.gms:play-services:9.6.0' compile 'com.google.android.gms:play-services-maps:9.6.0' 

Hope this helps!

0
source

this definitely works in the NAVIGATION_DRAWER ACTIVITY WITH FRAGMENT MAP

Run the program by adding it to the Top

  SupportMapFragment supportMapFragment; 

ADD THIS TO REDUCE THE METHOD ()

 supportMapFragment = SupportMapFragment.newInstance(); FragmentManager fm = getFragmentManager(); fm.beginTransaction().replace(R.id.maplayout, new` `MapFragmentClass()).commit();` supportMapFragment.getMapAsync(this); android.support.v4.app.FragmentManager sfm = getSupportFragmentManager(); sfm.beginTransaction().add(R.id.map,supportMapFragment).commit(); 

it will work chicken

-1
source

Source: https://habr.com/ru/post/981607/


All Articles