2

我正在尝试在 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();

我收到一条弹出消息,要求用户允许使用定位功能。

所以,问题是:有没有一开始就问同样的事情?我的意思是,当应用程序启动时?

在此先感谢您的帮助

问候!

4

2 回答 2

4

您看到的弹出窗口无法以编程方式调用。调用时会自动显示GetGeopositionAsync。如果应用程序已定义网络摄像头功能,则可以看到相同的对话框。类似的对话框只有在调用照片捕获方法时才会出现,您无法提前获得特定权限的“是”或“否”。

您可以做的是,在特定页面而不是在按钮单击事件中调用SetMyLocation()方法。OnNavigatedTo(...)

此外,您可以通过代码设置权限。它完全取决于用户。用户可以通过设置魅力 -> 权限选项卡来允许/阻止权限。

于 2013-11-07T05:29:46.727 回答
1

您可以创建一个MessageDialog对象。就像是:

MessageDialog dialog = new MessageDialog("Do you want to allow XXXX to use your location?");
dialog.Commands.Add(new UICommand("Yes"));
dialog.Commands.Add(new UICommand("No"));
var result = await dialog.ShowAsync();
if(result.Label == "Yes")
    App.UseLocation = true; // Where UseLocation is a static property in your app somewhere

希望这对编码有所帮助和快乐!

于 2013-11-07T04:41:24.387 回答