在 Xamarin 中,我需要在地图的中心设置标记并获取位置 [英] In Xamarin I need to set marker in the center of the map and get the position
问题描述
我正在尝试在 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屋!