【问题标题】:In Xamarin I need to set marker in the center of the map and get the position在 Xamarin 中,我需要在地图中心设置标记并获取位置
【发布时间】:2018-04-04 08:11:29
【问题描述】:

我正在 Xamarin 中尝试在地图中心添加一个标记,该标记将是固定的并且无法拖动,该标记下方的地图可以正常拖动。我需要获取用户选择的地图的位置,我在互联网上搜索了很多,但没有找到任何可以帮助我的东西。此功能在 Uber 应用中广泛使用。

【问题讨论】:

    标签: android xamarin xamarin.ios xamarin.forms xamarin.android


    【解决方案1】:

    iOS 上,您可以使用MKMapViewDelegate 子类上的RegionChanged 在用户滚动地图时获取更新。使用MKMapView.Region 获取地图视图的中心(CLLocationCoordinate2D);

    class MyMapDelegate : MKMapViewDelegate
    {
        public override void RegionChanged(MKMapView mapView, bool animated)
        {
            Console.WriteLine($"{mapView.Region.Center.Latitude} : {mapView.Region.Center.Longitude}");
            // Update your map's MKPointAnnotation's Coordinate to match the mapView.Region.Center
        }
    }
    

    Android 上,您可以使用GoogleMap.IOnCameraMoveListener 上的OnCameraMove 在用户滚动地图时获取更新。使用GoogleMap.CameraPosition.Target 获取地图视图中心的纬度/经度并更新您的MarkerOptions 的位置:

    public class MapCameraMoveListener : Java.Lang.Object, GoogleMap.IOnCameraMoveListener
    {
        readonly GoogleMap googleMap;
    
        public MapCameraMoveListener(GoogleMap googleMap) { this.googleMap = googleMap; }
    
        public void OnCameraMove()
        {
            Log.Debug("SO", $"{googleMap?.CameraPosition.Target.Latitude} : {googleMap?.CameraPosition.Target.Longitude}");
            // Update your map's `MarkerOptions`'s position to match the CameraPosition.Target
        }
    }
    

    对于Xamarin.Forms,请查看有关如何通过自定义渲染器自定义地图以实现上述原生功能的示例:

    【讨论】:

    • 非常感谢,我的朋友!我相信这就是我需要的!
    猜你喜欢
    • 1970-01-01
    • 2016-03-03
    • 1970-01-01
    • 2016-06-20
    • 1970-01-01
    • 1970-01-01
    • 2012-11-26
    • 1970-01-01
    • 2012-11-14
    相关资源
    最近更新 更多