How to add a marker in a map? i using SupportMapFragment

时间:2018-04-20 01:10:43

标签: xamarin.android google-maps-markers supportmapfragment

This is my code, I am trying to add a bookmark to google maps, and also to initialize the map in a specific place, but I can not understand how SupporMapFragment works, if someone could help me explaining how to do it, thanks

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

i new in xamarin android, i cant achieved understand how add de marker to map

1 个答案:

答案 0 :(得分:1)

实际上你要做的就是以下几点:

  • 在您的类型变量中,Google会根据您的情况映射private GoogleMap GoogleMap;您需要添加标记

  • 现在添加标记可能很棘手,因为您需要在Google地图对象为空且此对象获取其值运行时才需要添加它们,因此您需要确保它不是null,因为如果然后你的应用程序肯定会崩溃 (如果没有处理)。

  • 我为这种情况做的事情总是在添加标记之前进行空检查,并且因为它是位图(这通常是Android中千种类型的内存泄漏的主要原因),On destroy我的地图片段我打电话给垃圾收集器,每当我离开地图页面时,都会通过拨打GoogleMap.Clear();清除我的谷歌地图对象。

  • 指出添加标记的代码如下:

                    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();
    
  • 在这里,您需要标记的纬度,经度和标题,如果您想提供自定义标记,那么您可以将其替换为Drawable.marker也可以在此处使用lat和long。

如果您还有其他任何问题需要回复。

古德勒克! 快乐的编码

相关问题