How to get current country location and possibly CountryCode from Windows Phone 8.1

How do I get the current location of the country and possibly CountryCode from Windows Phone 8.1? I did this, but FindLocationsAsync throws me a "Value is not in the expected range"

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);
        }
    }

      

+3


source to share


2 answers


Its called reverse geolocation try this



 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;

      

+1


source


You should try this. I am declaring Map_Tapped event private async void

  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..");
            }




        }
      

Run code




mapNearByUser_MapTapped (Windows.UI.Xaml.Controls.Maps.MapControl sender, Windows.UI.Xaml.Controls.Maps.MapInputEventArgs args) {// Your map is clicked 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..");
        }




    }

      

+1


source







All Articles