在 Xamarin 中,我需要在地图的中心设置标记并获取位置 [英] In Xamarin I need to set marker in the center of the map and get the position

查看:29
本文介绍了在 Xamarin 中,我需要在地图的中心设置标记并获取位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying in Xamarin to add a marker in the center of the map that marker will be fixe and can not be dragged, the map below that marker can be dragged normally. I need to get the position of the map that was chosen by the user, I searched a lot for the internet and found nothing that could help me. This feature is widely used in the Uber app.

推荐答案

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

On iOS, you can use the RegionChanged on a MKMapViewDelegate subclass to get updates as the user scrolls the map. Use the MKMapView.Region to get the center (CLLocationCoordinate2D) of the map's view;

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 的位置:

On Android, you can use the OnCameraMove on the GoogleMap.IOnCameraMoveListener to get updates as the user scrolls the map. Use GoogleMap.CameraPosition.Target to obtain the Lat/Long of the center of the map's view and update your MarkerOptions's position:

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,请查看有关如何通过自定义渲染器自定义地图以实现上面显示的本机功能的示例:

For Xamarin.Forms look at the samples on how to custom the map via custom renderers to implement those native features shown above:

这篇关于在 Xamarin 中,我需要在地图的中心设置标记并获取位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆