如何让我的json查询等待手机的位置?

时间:2014-08-18 14:26:23

标签: c# windows-phone-8 geolocation

这里我试图发送一个带有纬度和经度的json查询,但这些值仍然是NaN,即使我调用ShowMyLocationOnTheMap,它首先检索手机位置。如何首先完成showMyLocation方法,然后调用json查询。你能救我一下吗?

public partial class ClosestBusStops : PhoneApplicationPage
{
    WebClient webClient;
    public ClosestBusStops()
    {
        InitializeComponent();
        ShowMyLocationOnTheMap(GeoPositionAccuracy.Default);
        webClient = new WebClient();
        webClient.DownloadStringCompleted += webClient_DownloadStringCompleted;
        string jsonQuery= "www.blabla.com?lat="+App.lat+"&lng="+App.lng;
        webClient.DownloadStringAsync(new Uri(jsonQuery));
    }

    private async void ShowMyLocationOnTheMap(GeoPositionAccuracy accuracy)
    {
        Geolocator myGeolocator = new Geolocator();
        Geoposition myGeoposition = await myGeolocator.GetGeopositionAsync();
        Geocoordinate myGeocoordinate = myGeoposition.Coordinate;
        GeoCoordinate myGeoCoordinate = CoordinateConverter.ConvertGeocoordinate(myGeocoordinate);

        App.lat = myGeoCoordinate.Latitude;
        App.lng = myGeoCoordinate.Longitude;

        this.pushPin(this.mapWithMyLocation, myGeoCoordinate);

    }
}

已更新

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {

            await ShowMyLocationOnTheMap(GeoPositionAccuracy.Default);
}

private async Task ShowMyLocationOnTheMap(GeoPositionAccuracy accuracy)
        {
            Geolocator myGeolocator = new Geolocator();
            Geoposition myGeoposition = await myGeolocator.GetGeopositionAsync();
            Geocoordinate myGeocoordinate = myGeoposition.Coordinate;
            GeoCoordinate myGeoCoordinate = CoordinateConverter.ConvertGeocoordinate(myGeocoordinate);

            App.lat = myGeoCoordinate.Latitude;
            App.lng = myGeoCoordinate.Longitude;

            this.pushPin(this.mapWithMyLocation, myGeoCoordinate);
        }

1 个答案:

答案 0 :(得分:3)

ShowMyLocationOnTheMap是一种异步方法,因此如果您想等待此方法的完成,则需要使用await关键字,如下所示:

await ShowMyLocationOnTheMap(GeoPositionAccuracy.Default);

由于您无法在构造函数中使用await,因此需要将其移动到其他位置,例如OnNavigatedTo方法或Loaded事件处理程序。

同样,ShowMyLocationOnTheMap的返回类型应为Task而不是void

更新:

    public ClosestBusStops()
    {
        InitializeComponent();
    }

    private async Task ShowMyLocationOnTheMap(GeoPositionAccuracy accuracy)
    {
        Geolocator myGeolocator = new Geolocator();
        Geoposition myGeoposition = await myGeolocator.GetGeopositionAsync();
        Geocoordinate myGeocoordinate = myGeoposition.Coordinate;

        App.lat = myGeoCoordinate.Latitude;
        App.lng = myGeoCoordinate.Longitude;

        this.pushPin(this.mapWithMyLocation, myGeoCoordinate);
    }

    protected async override void OnNavigatedTo(NavigationEventArgs e)
    {
        await ShowMyLocationOnTheMap(GeoPositionAccuracy.Default);
        webClient = new WebClient();
        webClient.DownloadStringCompleted += webClient_DownloadStringCompleted;
        string jsonQuery= "www.blabla.com?lat="+App.lat+"&lng="+App.lng;
        webClient.DownloadStringAsync(new Uri(jsonQuery));
    }

希望这有帮助。