Android 将.GetMapAsync改为.getMap方法与Google Play服务(Xamarin)配合使用
旧代码以这种方式完美工作:Android 将.GetMapAsync改为.getMap方法与Google Play服务(Xamarin)配合使用,android,google-maps,xamarin,google-play-services,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 = bu
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 fragment类必须实现
OnMapReadyCallback
并重写onMapReady()
:
在onCreateView
中,使用getMapAsync()
设置片段的回调:
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map); mapFragment.getMapAsync(this);
<?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>
实现google maps V2所需的全部内容如下:
请看以下内容:
public class MapActivity : Activity, IOnMapReadyCallback
{
public void OnMapReady(GoogleMap googleMap)
{
if (googleMap != null)
{
//Map is ready for use
}
}
}
希望这会对您有所帮助。这可能是对您的问题的延迟答复,但可能会对其他有相同问题的人有所帮助。 GetMapAsync()需要实现IOnMapReadyCallback类型的回调对象 在我的博客条目中找到更多详细信息:
//OnMapReadyClass
MapReadyClass上的公共类:Java.Lang.Object,IOnMapReadyCallback
{
公共谷歌地图{get;private set;}
公共事件行动图;
4月1日公开作废(谷歌地图谷歌地图)
{
地图=谷歌地图;
if(MapReadyAction!=null)
MapReadyAction(Map);
}
}
现在,在映射初始化成功时调用GetMapAsync()和事件操作返回映射实例
//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;
}
//MyActivityClass.cs
谷歌地图;
bool SetUpGoogleMap()
{
if(null!=map)返回false;
var frag=FragmentManager.findframentbyid(Resource.Id.map);
var mapReadyCallback=new OnMapReadyClass();
mapReadyCallback.MapReadyAction+=委托(谷歌地图谷歌地图)
{
地图=谷歌地图;
};
frag.GetMapAsync(mapReadyCallback);
返回true;
}
这肯定适用于带有片段地图的导航抽屉活动
通过在顶部添加此项来运行prorramm
SupportMapFragment supportMapFragment;
在创建METDOD()时添加此项。
如果您想使用视图而不是地图片段,这将非常有用。下面是我从使用以前play services版本的项目获得此转换所应用的实现。更新至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;
}
在你的片段中:
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map); mapFragment.getMapAsync(this);
<?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>
希望这有帮助 您的MapViewActivity类必须实现IOnMapReadyCallback 另外,请确保已从NuGet安装Xamarin.GooglePlayServices.Maps包,并指定了所需的权限: -访问网络状态–地图API必须能够检查是否可以下载地图分幅。 -互联网接入——互联网接入是下载地图贴片和与Google Play服务器通信以访问API所必需的。 (查看官方文件:)
我知道在java中可以这样做:mapFragment.getMapAsync(new-OnMapReadyCallback(){@Override-public-void-onMapReady(GoogleMap-GoogleMap){mGoogleMap=GoogleMap;moveMapAddBounds();}});但我是C语言的新手,在C语言中没有现成的接口实现。。。您必须创建实现接口的类,并将其传递给此函数
public类VeryNaiveImpl:IOnMapReadyCallback{public Action Callback;public void onMapReady(GoogleMap){if(Callback!=null)Callback(GoogleMap);}}
然后像googleMapFrag.getmapsync(new VeryNaiveImpl{Callback=(map)一样使用它=>{/*映射已就绪*/})代码>记住,大小很重要:Pi不会向你学习C#。。。看看这里,因为我最近升级了我的:我认为您不需要测试null;至少我是这么理解的。但我不知道Xamarin是否也是如此,我只是一个初学者,只是在你的答案和文档中同时出现了一些问题。你好,请看这里:这是Xamarin官方文档。您必须检查null。
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" />
compile 'com.google.android.gms:play-services:9.6.0'
compile 'com.google.android.gms:play-services-maps:9.6.0'
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);
}
}
}