使用.GetMapAsync代替.getMap方法与Google Play服务(Xamarin)

时间:2015-01-27 14:30:23

标签: android google-maps xamarin google-play-services

旧代码以这种方式完美运行:

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

但是现在.Map已过时且已弃用,我必须以某种方式使用.GetMapAsync

theMap = googleMap.GetMapAsync (IOnMapReadyCallback);

但我不明白如何。

有人可以帮助我吗?

6 个答案:

答案 0 :(得分:17)

您的地图片段类必须实现OnMapReadyCallback并覆盖onMapReady()

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

onCreateView使用getMapAsync()设置片段的回调:

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

您需要实现Google地图V2所需的一切: https://developers.google.com/maps/documentation/android/map

答案 1 :(得分:3)

这可能是对您的问题的迟回复,但可能对有相同问题的其他人有帮助。 GetMapAsync()期望实现类型为IOnMapReadyCallback的回调对象。

在我的博客条目中查找更多详细信息: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);
     }
    }

现在在成功的Map初始化时调用GetMapAsync()和事件Action返回映射实例。

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

答案 2 :(得分:1)

请看这个:

public class MapActivity : Activity, IOnMapReadyCallback
{

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

希望这会对你有所帮助。

答案 3 :(得分:0)

如果您想使用视图而不是地图片段,那么我应用的实现是从使用以前的播放服务版本的项目中获得此转换。已更新至9.6.0以符合新的FCM并导致我们的地图片段出现问题。

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;

}

在你的片段中:

<?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>

重要的是要注意您的清单上的以下权限:

<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" />

我能够在我的Gradle上的API 23和以下库中的任何错误中实现此设置:

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

希望这有帮助!

答案 4 :(得分:0)

您的MapViewActivity类必须实现IOnMapReadyCallback。

还要确保您已经从NuGet安装了Xamarin.GooglePlayServices.Maps包并指定了所需的权限: -访问网络状态-Maps API必须能够检查其是否可以下载地图图块。 -互联网访问-下载地图图块并与Google Play服务器通信以进行API访问需要互联网访问。 (请查看官方文档: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);
            }

        }
    }

答案 5 :(得分:-1)

这绝对适用于NAVIGATION_DRAWER活动与碎片地图

在Top

中添加此prorramm
 SupportMapFragment supportMapFragment;

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

这会使小鸡