【问题标题】:How to add a marker in a map? i using SupportMapFragment如何在地图中添加标记?我使用 SupportMapFragment
【发布时间】:2018-04-20 01:10:42
【问题描述】:

这是我的代码, 我正在尝试向谷歌地图添加书签,并在特定位置初始化地图,但我不明白 SupporMapFragment 是如何工作的,如果有人可以帮助我解释如何做到这一点,谢谢

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

我是 xamarin android 的新手,我无法理解如何将 de 标记添加到地图

【问题讨论】:

  • 我不明白您将地图加载到特定位置是什么意思。

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


【解决方案1】:

其实很简单,你只需要做以下事情:

  • 在你的类型变量中,谷歌在你的情况下映射这个private GoogleMap GoogleMap;你需要添加标记

  • 1234563应用程序肯定会崩溃 (如果不处理)。 1234563每当我离开地图页面时,我都会调用垃圾收集器并通过调用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。

如果您有任何其他问题,请在此处回复。

祝你好运! 快乐编码

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-19
    • 1970-01-01
    相关资源
    最近更新 更多