我正在参与一个使用 Xamarin Forms 和 OpenStreetMap 的项目。 我正在尝试获取地图中心的坐标。
这是我创建地图的代码
public static Map CreateMap(double latitud, double longitud)
{
map = new Map();
var location = new Point(longitud, latitud);
var sphericalMercatorCoordinate = SphericalMercator.FromLonLat(location.X, location.Y);
map.Layers.Add(OpenStreetMap.CreateTileLayer());
map.Home = n => n.NavigateTo(sphericalMercatorCoordinate, map.Resolutions[15]);
return map;
}
这是我在文件 xaml 中的代码。
<maps:MapView x:Name="MapView"
TouchMove="MapView_TouchMove"
MyLocationFollow="True"
IsMyLocationButtonVisible="False"
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand" />
尝试添加方法“TouchMove”。但只能获取地图的边界而不是坐标。
private void MapView_TouchMove(object sender, Mapsui.UI.TouchedEventArgs e)
{
var center = MapView.Bounds.Center;
}
所以,尝试添加一个方法MapClicked,用于从“MapView_TouchMove”调用此方法,但我不知道如何调用。
private void MapView_MapClicked(object sender, MapClickedEventArgs e)
{
var lat = e.Point.Latitude;
}
有什么想法吗..??? 谢谢。
我使用导航器的 Viewport 属性获得它:
var vp = _mapControl.Map?.Navigator.Viewport;
MapCentre = new MPoint(vp.Value.CenterX, vp.Value.CenterY);
x = vp.Value.CenterX;
y = vp.Value.CenterY;
var LonLat = SphericalMercator.ToLonLat(x, y);
Longitude = LonLat.lon;
Latitude = LonLat.lat;