Xamarin.android 如何在地图中添加标记?我正在使用SupportMapFragment
这是我的密码, 我正在尝试向google maps添加书签,并在特定位置初始化地图,但我无法理解SuperFormapFragment是如何工作的,如果有人能帮我解释如何操作,谢谢Xamarin.android 如何在地图中添加标记?我正在使用SupportMapFragment,xamarin.android,google-maps-markers,supportmapfragment,Xamarin.android,Google Maps Markers,Supportmapfragment,这是我的密码, 我正在尝试向google maps添加书签,并在特定位置初始化地图,但我无法理解SuperFormapFragment是如何工作的,如果有人能帮我解释如何操作,谢谢 internal class DetailMapFragment : BaseFragment , IOnMapReadyCallback { private GoogleMap GoogleMap; private Su
internal class DetailMapFragment : BaseFragment , IOnMapReadyCallback
{
private GoogleMap GoogleMap;
private SupportMapFragment _mapFragment;
public override int LayoutId => Resource.Layout.map_page;
protected override void InitViews()
{
//var mapView = mView.FindViewById<MapView>(Resource.Id.mapView);
//mapView.GetMapAsync(this);
try
{
_mapFragment = Activity.SupportFragmentManager.FindFragmentByTag("map") as SupportMapFragment;
if (_mapFragment == null)
{
GoogleMapOptions mapOptions = new GoogleMapOptions()
.InvokeMapType(GoogleMap.MapTypeNormal)
.InvokeZoomControlsEnabled(false)
.InvokeMaxZoomPreference(20)
.InvokeCompassEnabled(true);
FragmentTransaction fragTx = FragmentManager.BeginTransaction();
_mapFragment = SupportMapFragment.NewInstance(mapOptions);
fragTx.Add(Resource.Id.mapView, _mapFragment, "map");
fragTx.Commit();
LatLng latlong = new LatLng(40.776408,-73.970755);
MarkerOptions mark = new MarkerOptions()
.SetPosition(latlong)
.SetTitle("New York")
.SetSnippet("Apple");
_mapFragment.AddMarker(mark);
}
_mapFragment.GetMapAsync(this);
}
catch (System.Exception ex)
{
ex.Message.ToString();
}
public void OnMapReady(GoogleMap googleMap)
{
//this.GoogleMap = googleMap;
}
void IOnMapReadyCallback.OnMapReady(GoogleMap googleMap)
{
//try
//{
// this.GoogleMap = googleMap;
// if (googleMap != null)
// {
// googleMap.AnimateCamera(CameraUpdateFactory.NewLatLng(new LatLng(-11.083271, -76.207374)));
// }
//}
//catch (System.Exception ex)
//{
// ex.Message.ToString();
//}
}
}
内部类DetailMapFragment:BaseFragment,IOnMapReadyCallback
{
私人谷歌地图谷歌地图;
私有SupportMapFragment\u mapFragment;
public override int LayoutId=>Resource.Layout.map\u页面;
受保护的重写void InitViews()
{
//var mapView=mView.findviewbyd(Resource.Id.mapView);
//GetMapAsync(这个);
尝试
{
_mapFragment=Activity.SupportFragmentManager.FindFragmentByTag(“map”)作为SupportMapFragment;
如果(_mapFragment==null)
{
GoogleMapOptions mapOptions=新建GoogleMapOptions()
.InvokeMapType(GoogleMap.MapTypeNormal)
.InvokeZoomControlsEnabled(false)
.InvokeMaxZoomPreference(20)
.invokeCompossabled(真);
FragmentTransaction fragTx=FragmentManager.BeginTransaction();
_mapFragment=SupportMapFragment.NewInstance(mapOptions);
Add(Resource.Id.mapView,_mapFragment,“map”);
fragTx.Commit();
LatLng latlong=新LatLng(40.776408,-73.970755);
标记选项标记=新标记选项()
.设置位置(拉长)
.SetTitle(“纽约”)
.Setsnipet(“苹果”);
_mapFragment.AddMarker(标记);
}
_GetMapAsync(这个);
}
catch(System.Exception-ex)
{
例如Message.ToString();
}
4月1日公开作废(谷歌地图谷歌地图)
{
//this.GoogleMap=谷歌地图;
}
void IOnMapReadyCallback.OnMapReady(谷歌地图谷歌地图)
{
//试一试
//{
//this.GoogleMap=谷歌地图;
//if(谷歌地图!=null)
// {
//googleMap.AnimateCamera(CameraUpdateFactory.NewLatLng(NewLatLng(-11.083271,-76.207374));
// }
//}
//catch(System.Exception-ex)
//{
//例如Message.ToString();
//}
}
}
我是xamarin android的新手,我无法理解如何将de marker添加到地图中其实非常简单,您只需执行以下操作:
- 在您的类型变量中,Google maps在您的示例中,此
您需要添加标记私有Google Map Google Map;
- 现在添加标记可能很棘手,因为您需要添加标记,而不是在您的google map对象为null且此对象在运行时获取其值时添加标记,因此您需要确保它不为null,因为如果为null,则您的应用程序肯定会崩溃 (如果未处理)
- 对于这种情况,我总是在添加标记之前进行空检查,因为它是位图(这通常是Android中一千种内存泄漏的主要原因),在销毁我的地图片段时,我调用垃圾收集器,并通过调用
GoogleMap.clear()清除我的google地图对象每当我离开地图页面时
- 至此,添加标记的代码如下所示:
LatLng latlngall = new LatLng(double.Parse(point.Latitude), double.Parse(point.Loungitude)); MarkerOptions options = new MarkerOptions().SetPosition(latlngall).SetTitle(point.Landmark); options.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.marker)); Marker marker = GMap.AddMarker(options); marker.Tag = point.Id.ToString();
- 在这里,您需要标记的纬度、经度和标题,如果您想提供自定义标记,则可以将其放置在
在这里也是必须的可绘制标记的位置。标记
快乐编码我不明白你在特定位置加载地图是什么意思。