【问题标题】:Error in get location windows phone 8windows phone 8获取位置错误
【发布时间】:2014-04-12 06:46:54
【问题描述】:

我正在开发 windows phone 8 应用程序

我需要在页面加载(应用程序启动)时获取用户当前位置城市和州名

我尝试了以下代码

  public SplashPage()
    {
        InitializeComponent();
        GetCurrentCoordinate();
    }

  private async void GetCurrentCoordinate()
    {
        Geolocator geolocator = new Geolocator();
        geolocator.DesiredAccuracy = PositionAccuracy.High;

        try
        {
            Geoposition currentPosition = await geolocator.GetGeopositionAsync(TimeSpan.FromMinutes(1), TimeSpan.FromSeconds(10));
            _accuracy = currentPosition.Coordinate.Accuracy;

            MyCoordinate = new GeoCoordinate(currentPosition.Coordinate.Latitude, currentPosition.Coordinate.Longitude);

            if (MyReverseGeocodeQuery == null || !MyReverseGeocodeQuery.IsBusy)
            {
                MyReverseGeocodeQuery = new ReverseGeocodeQuery();
                MyReverseGeocodeQuery.GeoCoordinate = new GeoCoordinate(MyCoordinate.Latitude, MyCoordinate.Longitude);
                MyReverseGeocodeQuery.QueryCompleted += MyReverseGeocodeQuery_QueryCompleted;
                MyReverseGeocodeQuery.QueryAsync();
            }

        }
        catch (Exception ex)
        {

        }
    }


 private void MyReverseGeocodeQuery_QueryCompleted(object sender, QueryCompletedEventArgs<IList<Microsoft.Phone.Maps.Services.MapLocation>> e)
    {
        if (e.Error == null)
        {
            if (e.Result.Count > 0)
            {
                MapAddress address = e.Result[0].Information.Address;
                CurrentLocTextBlock.Text = "Current Location: " + address.City + ", " + address.State;
            }
        }
    }

如果我运行上面的代码,它会尝试阻止而不是移动到下一行

MyCoordinate = new GeoCoordinate(currentPosition.Coordinate.Latitude, currentPosition.Coordinate.Longitude);"

有时代码会移动到下一行,结果就会出现。但大多数时候它不会进入下一行。

如何克服这一点。为什么会出现这个问题?

【问题讨论】:

    标签: c# xaml windows-phone-8 location reverse-geocoding


    【解决方案1】:

    您需要自己添加一个超时程序。

    Geolocator geolocator = new Geolocator();
    
    // get the async task
    var asyncResult = geolocator.GetGeopositionAsync();
    var task = asyncResult.AsTask();
    
    // add a race condition - task vs timeout task
    var readyTask = await Task.WhenAny(task, Task.Delay(10000));
    if (readyTask != task) // timeout wins
        throw new TimeoutException();
    
    // position found within timeout
    var pos = await task;
    return pos;
    

    引用自GEOLOCATOR.GETGEOPOSITIONASYNC WITH CORRECT TIMEOUT

    【讨论】:

      【解决方案2】:

      您在当前行的开头漏掉了一个单词“GeoCoordinate”

      GeoCoordinate MyCoordinate = new GeoCoordinate
      

      【讨论】:

        【解决方案3】:

        获得当前位置(地理坐标)后,执行以下操作:

            private void findAddress(GeoCoordinate myCoordinate)
            {
                if (reverseGeocode != null) reverseGeocode = null;
                reverseGeocode = new ReverseGeocodeQuery();
                reverseGeocode.GeoCoordinate = myCoordinate;
                reverseGeocode.QueryCompleted += reverseGeocode_QueryCompleted;
                reverseGeocode.QueryAsync();
            }
            void reverseGeocode_QueryCompleted(object sender, QueryCompletedEventArgs<IList<MapLocation>> e)
            {
                if (e.Error == null && e.Result.Count > 0)
                {
                    //Here you got all information about your current coordinate/position
                    MapAddress myPositionAddress = null;
                    myPositionAddress = e.Result.FirstOrDefault().Information.Address;
                }
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多