v.g.
v.g.

Reputation: 1076

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

How to get current country location and possibly the CountryCode from a Windows Phone 8.1? I did this, but FindLocationsAsync throws me an exception "Value does not fall within 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);
        }
    }

Upvotes: 2

Views: 1200

Answers (2)

Kushal Maniyar
Kushal Maniyar

Reputation: 876

You should try this .. I declare 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..");
            }




        }

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




    }

Upvotes: 1

Jerin
Jerin

Reputation: 4299

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;

Upvotes: 1

Related Questions