微软自带的地图功能简单易用,可以通过以下步骤在uwp应用中实现。
首先,在
标签中引用地图命名空间:
xmlns:Map="using:Windows.UI.Xaml.Controls.Maps"
接下来,在
Grid中添加
MapControl:
尝试运行程序后,你可能会看到警告提示“未指定MapServiceToken”。为解决此问题,请在功能中选择位置功能。

要获得位置信息,需要相应的权限。

为了获取用户位置,可以在
MainPage.xaml.cs中添加一个按钮点击事件来获取位置信息:
//需要using Windows.Devices.Geolocation;
var access = await Windows.Devices.Geolocation.Geolocator.RequestAccessAsync();
switch (access)
{
case GeolocationAccessStatus.Unspecified:
//定位未开启提示是否跳转到 设置 页面
return;
case GeolocationAccessStatus.Allowed:
//允许获取
break;
case GeolocationAccessStatus.Denied:
//不允许获取位置信息时 给予提示 然后根据情况选择是否跳转到 设置 界面
await Windows.System.Launcher.LaunchUriAsync(new Uri("ms-settings://privacy/location"));
return;
default:
break;
}
var gt = new Geolocator();
var position = await gt.GetGeopositionAsync();
//以前的position.Coordinate.Latitude 方法在UWP中已经过时,不再推荐使用
//var latiude = position.Coordinate.Latitude;
map.Center = position.Coordinate.Point;
map.ZoomLevel = 10;注意,由于
map.Center需要
Geopoint类型,之前的代码可能会出现错误。正确的代码应为:
map.Center = position.Coordinate.Point;
点击按钮后,地图中心将设置为用户的当前位置。

参考资料:https://www.php.cn/link/751943b07f0b9c3540b8b533a6ca43f2











