【问题标题】:How get location (Latitude, Longitude) in center of map Xamarin Forms如何在地图中心获取位置(纬度,经度)Xamarin Forms
【发布时间】:2020-07-09 14:13:56
【问题描述】:

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

任何想法..??? 谢谢。

【问题讨论】:

  • 如果您链接到您正在使用的实际地图控件会很有帮助
  • 添加了我创建地图的代码。我使用 OpenStreetMap
  • 我知道您正在使用 OpenStreetMap。我假设您正在使用一些 Nuget 包作为控件,我要求您提供指向它的链接

标签: android ios xamarin xamarin.forms openstreetmap


【解决方案1】:

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

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

MapView 控件有一个Map 属性,您可以从中获取Envelope

【讨论】:

    猜你喜欢
    • 2015-03-24
    • 2012-12-08
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-08
    相关资源
    最近更新 更多