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

橙三吉。 提交于 2019-12-12 08:07:32

问题


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?


回答1:


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




回答2:


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;
   }



回答3:


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.




回答4:


If you like to use a view instead of a map fragment here is the implementation I applied to get this transition from a project that used a previous play-services version. Updated to 9.6.0 to comply with new FCM and caused issues 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>

Important to note the following permissions on 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 was able to achieve this setup with out any errors on 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!




回答5:


Your MapViewActivity class must implement IOnMapReadyCallback.

Also make sure you have installed the Xamarin.GooglePlayServices.Maps package from NuGet and specified the required permissions: - Access to the Network State – The Maps API must be able to check if it can download the map tiles. - Internet Access – Internet access is necessary to download the map tiles and communicate with the Google Play Servers for API access. (check the offcial documentation: Using the Google Maps API in your application)

using Android.Gms.Maps.Model;
using MvvmCross.Platforms.Android.Views;

public class MapViewActivity : MvxActivity, IOnMapReadyCallback
    {
       private GoogleMap _map;
       private MapFragment _mapFragment;

       protected override void OnCreate(Bundle bundle)
        {
            _mapFragment = (MapFragment) FragmentManager.FindFragmentById(Resource.Id.map);

            if (_mapFragment != null) return;

           //GoogleMap properties
            var mapOptions = new GoogleMapOptions()
                .InvokeMapType(GoogleMap.MapTypeNormal)
                .InvokeZoomControlsEnabled(true)
                .InvokeCompassEnabled(true);

            var fragTx = FragmentManager.BeginTransaction();

            //MapFragment can be programmatically instantiated
            _mapFragment = MapFragment.NewInstance(mapOptions);
            fragTx.Add(Resource.Id.activitiesmap, _mapFragment, "map");
            fragTx.Commit();

            //used to initialize the GoogleMap that is hosted by the fragment                        
            _mapFragment.GetMapAsync(this);

            // remainder of code omitted
        }

        //will be invoked when it is possible for the app to interact with the GoogleMap object
         public void OnMapReady(GoogleMap googleMap)
        {
            _map = googleMap;
            // If you have a map do something with it
            if (_map != null) 
            {
                _map.MapType = GoogleMap.MapTypeNormal;
                _map.MoveCamera (cameraUpdate);
            }

        }
    }



回答6:


this definitly work in NAVIGATION_DRAWER ACTIVITY WITH FRAGMENT MAP

Run your prorramm by adding this in Top

 SupportMapFragment supportMapFragment;

ADD THIS in ON CREATE METDOD()

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();

this will work chick it



来源:https://stackoverflow.com/questions/28172897/use-getmapasync-instead-getmap-method-with-google-play-services-xamarin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!