问题
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.
回答1:
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
}
}
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
}
}
For Xamarin.Forms
look at the samples on how to custom the map via custom renderers to implement those native features shown above:
- https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/custom-renderer/map/
来源:https://stackoverflow.com/questions/46895114/in-xamarin-i-need-to-set-marker-in-the-center-of-the-map-and-get-the-position