C# Android更新谷歌地图间隔后
我必须在间隔后(比如10秒)用新的位置更新谷歌地图标记。纬度和经度的值每10秒来自web服务 我使用定时器和下面的代码。它工作正常,但使用定时器时,它会在摄像机前崩溃,如下所示:C# Android更新谷歌地图间隔后,c#,android,google-maps,xamarin,xamarin.android,C#,Android,Google Maps,Xamarin,Xamarin.android,我必须在间隔后(比如10秒)用新的位置更新谷歌地图标记。纬度和经度的值每10秒来自web服务 我使用定时器和下面的代码。它工作正常,但使用定时器时,它会在摄像机前崩溃,如下所示: private void OnTimedEvent(object sender, GoogleMap googleMap) { // return; double latitude = Convert.ToDouble((FindViewById<EditText>(Resou
private void OnTimedEvent(object sender, GoogleMap googleMap)
{
// return;
double latitude = Convert.ToDouble((FindViewById<EditText>(Resource.Id.txtLatitude)).Text);
double longitude = Convert.ToDouble((FindViewById<EditText>(Resource.Id.txtLongitude)).Text);
this.GMap = googleMap;
LatLng latlng = new LatLng(Convert.ToDouble(latitude), Convert.ToDouble(longitude));
CameraUpdate camera = CameraUpdateFactory.NewLatLngZoom(latlng, 15);
this.GMap.MoveCamera(CameraUpdateFactory.NewLatLng(latlng)); // it crashes here
}
Google关于
GoogleMap
以及引发异常的方法的说明:
注意:…GoogleMap只能从主线程读取和修改从另一个线程调用GoogleMap方法将导致异常
将GoogleMap调用包装在RunOnUiThread
调用中:
RunOnUiThread(() =>
{
this.GMap.MoveCamera(CameraUpdateFactory.NewLatLng(latlng));
});
Ref:请将实际异常添加到您的问题中。我看到此异常:Java.Lang.IllegalStateException:不在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()的主线程上
RunOnUiThread(() =>
{
this.GMap.MoveCamera(CameraUpdateFactory.NewLatLng(latlng));
});