如何获取地图中心位置(纬度、经度)Xamarin Forms

问题描述 投票:0回答:2

我正在参与一个使用 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;
}

有什么想法吗..??? 谢谢。

android ios xamarin xamarin.forms openstreetmap
2个回答
0
投票

查看 API 文档,Envelope 似乎返回您想要的内容

根据中所有图层的范围获取地图的范围 图层集合

MapView
控件有一个
Map
属性,从中可以得到
Envelope


0
投票

我使用导航器的 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;
© www.soinside.com 2019 - 2024. All rights reserved.