【问题标题】:How to get current country location and possibly the CountryCode from a Windows Phone 8.1如何从 Windows Phone 8.1 获取当前国家/地区位置以及可能的 CountryCode
【发布时间】:2015-05-21 13:40:52
【问题描述】:

如何从 Windows Phone 8.1 获取当前国家/地区位置以及可能的 CountryCode?我这样做了,但 FindLocationsAsync 向我抛出异常“值不在预期范围内”

private async void LocateMe()
    {
        Geolocator GeoLoc = new Geolocator();
        GeoLoc.DesiredAccuracyInMeters = 50;

        try
        {
            Geoposition GeoPosition = await GeoLoc.GetGeopositionAsync();

            MapLocationFinderResult result = await MapLocationFinder.FindLocationsAsync("", GeoPosition.Coordinate.Point, 5);

            if (result.Status == MapLocationFinderStatus.Success)
            {
                foreach (MapLocation mapLocation in result.Locations)
                {
                    string strCountryCode = mapLocation.Address.CountryCode;
                }
            }
        }
        catch(Exception e)
        {
            System.Diagnostics.Debug.WriteLine(e.Message);
        }
    }

【问题讨论】:

    标签: c# .net geolocation windows-phone-8.1 windows-8.1


    【解决方案1】:

    这叫反向地理定位试试这个

     var geolocator = new Geolocator();
                geolocator.DesiredAccuracyInMeters = 100;
                Geoposition position = await geolocator.GetGeopositionAsync();
    
                 //reverse geocoding
                BasicGeoposition myLocation = new BasicGeoposition
                {
                    Longitude = position.Coordinate.Longitude,
                    Latitude = position.Coordinate.Latitude
                };
                Geopoint pointToReverseGeocode = new Geopoint(myLocation);
    
                MapLocationFinderResult result = await MapLocationFinder.FindLocationsAtAsync(pointToReverseGeocode);
    
                 //here also it should be checked if there result isn't null and what to do in such a case
                string country = result.Locations[0].Address.Region;
    

    【讨论】:

      【解决方案2】:

      你应该试试这个 .. 我声明 Map_Tapped 事件 私有异步无效

        private async void mapNearByUser_MapTapped(Windows.UI.Xaml.Controls.Maps.MapControl sender, Windows.UI.Xaml.Controls.Maps.MapInputEventArgs args)
              {
                  //Your Map Tapped
                  Geopoint pointToReverseGeocode = new Geopoint(args.Location.Position);
      
                  // Reverse geocode the specified geographic location.  
                  MapLocationFinderResult result =
                      await MapLocationFinder.FindLocationsAtAsync(pointToReverseGeocode);
      
                  var resultText = new StringBuilder();
                  try
                  {
                      if (result.Status == MapLocationFinderStatus.Success)
                      {
                          resultText.AppendLine(result.Locations[0].Address.District + ", " + result.Locations[0].Address.Town + ", " + result.Locations[0].Address.Country);
                      }
                      MessageBox(resultText.ToString());
      
                  }
                  catch
                  {
                      //MessageBox(resultText.ToString());
                      MessageBox("Please wait..");
                  }
      
      
      
      
              }

      mapNearByUser_MapTapped(Windows.UI.Xaml.Controls.Maps.MapControl 发件人,Windows.UI.Xaml.Controls.Maps.MapInputEventArgs 参数) { //你的地图被点击了 Geopoint pointToReverseGeocode = new Geopoint(args.Location.Position);

              // Reverse geocode the specified geographic location.  
              MapLocationFinderResult result =
                  await MapLocationFinder.FindLocationsAtAsync(pointToReverseGeocode);
      
              var resultText = new StringBuilder();
              try
              {
                  if (result.Status == MapLocationFinderStatus.Success)
                  {
                      resultText.AppendLine(result.Locations[0].Address.District + ", " + result.Locations[0].Address.Town + ", " + result.Locations[0].Address.Country);
                  }
                  MessageBox(resultText.ToString());
      
              }
              catch
              {
                  //MessageBox(resultText.ToString());
                  MessageBox("Please wait..");
              }
      
      
      
      
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-21
        • 2015-10-21
        • 2013-10-19
        • 1970-01-01
        • 2020-12-25
        • 1970-01-01
        • 2015-12-29
        相关资源
        最近更新 更多