我正在尝试在 Windows 应用商店应用程序中使用定位功能。我已经在 XAML 页面中放置了 Bing 地图控件。
在后面的代码中,我试图使用以下代码获取我的当前位置:
private async Task SetMyLocation()
{
var position = await this.GetCurrentPosition();
if (position != null)
this.DataContext = position;
this.myLocation = new Location(position.Latitude, position.Longitude);
this.myMap.Center = this.myLocation;
}
和 。. .
private async Task<Position> GetCurrentPosition()
{
try
{
Geolocator geolocator = new Geolocator();
geolocator.DesiredAccuracy = PositionAccuracy.High;
geolocator.MovementThreshold = 0;
Geoposition location = await geolocator.GetGeopositionAsync();
var postion = new Position
{
Latitude = location.Coordinate.Latitude,
Longitude = location.Coordinate.Longitude
};
return postion;
}
catch (Exception ex)
{
. . .
}
上面的所有代码都是在用户单击应用程序中的按钮时执行的。然后在执行 GetCurrentPosition() 方法并尝试执行以下代码行时:
Geoposition location = await geolocator.GetGeopositionAsync();
我收到一条弹出消息,要求用户允许使用定位功能。
所以,问题是:有没有一开始就问同样的事情?我的意思是,当应用程序启动时?
在此先感谢您的帮助
问候!