How can I call MapLocationFinder.FindLocationsAsync() to get the coordinates for an address without already knowing the latitude and longitude?

368 views Asked by At

I'm trying to adapt the code here to get the latitude and longitude for a passed-in address.

However, one of the arguments passed to MapLocationFinder.FindLocationsAsync() is a Geopoint with latitude and longitude already set, like this in the code referenced above:

// The nearby location to use as a query hint.
   BasicGeoposition queryHint = new BasicGeoposition();
   queryHint.Latitude = 47.643;
   queryHint.Longitude = -122.131;
   Geopoint hintPoint = new Geopoint(queryHint);

   // Geocode the specified address, using the specified reference point
   // as a query hint. Return no more than 3 results.
   MapLocationFinderResult result =
         await MapLocationFinder.FindLocationsAsync(
                           addressToGeocode,
                           hintPoint,
                           3);

The ultimate result is that the latitude and longitude are set:

   if (result.Status == MapLocationFinderStatus.Success)
   {
      tbOutputText.Text = "result = (" +
            result.Locations[0].Point.Position.Latitude.ToString() + "," +
            result.Locations[0].Point.Position.Longitude.ToString() + ")";
   }

This doesn't make sense to me - the whole purpose of the method is to set the latitude and longitude, in other words they are unknown at the time the call to MapLocationFinder.FindLocationsAsync() is made.

This is my code, my adaptation of what is above and at the given link:

public sealed partial class MainPage : Page
{
    . . .
    // Global, set by call to SetLatAndLongForAddress()
    double latitude = 0.0;
    double longitude = 0.0;
    . . .

private async void SetLatAndLongForAddress(string fullAddress)
{
    try
    {
        string addressToGeocode = fullAddress;

        MapLocationFinderResult mapLocFinderResult =
            await MapLocationFinder.FindLocationsAsync(
                addressToGeocode,
                null, 
                1); // changed 3 to 1 - only want one returned val

        if (mapLocFinderResult.Status == MapLocationFinderStatus.Success)
        {
            latitude = mapLocFinderResult.Locations[0].Point.Position.Latitude;
            longitude = mapLocFinderResult.Locations[0].Point.Position.Longitude;
        }
    }
    . . .
}

Is this the way to do it - pass a null instead of a Geopoint as the second argument to MapLocationFinder.FindLocationsAsync()? Will that work, or what am I misunderstanding about this?

2

There are 2 answers

0
YanGu On BEST ANSWER

Yes, your way is no problem.

The FindLocationsAsync(String, Geopoint, UInt32) method will convert the specified address to a collection of geographic locations(geocoding). The specified address is provided by your fullAddress, and the second parameter whose type is Geopoint is just a hint, not the basis. The second parameter can be null, and the method can work.

When you want to get a coordinate for an address, you can give a known coordinate which is close to your target address, which may be helpful for searching your target address’s coordinate.

1
Duncan Lawler On

The Geopoint you pass to FindLocationsAsync is a "hint" to help the geocoder give the correct result. For example if you search "Paris" and are in rural Texas, you might want Paris, TX as the best result. Anywhere else, you probably want Paris, France. Passing null will try to geocode with no hint so you may not get the best results. People frequently pass either the users current location (using the geolocaton API https://learn.microsoft.com/en-us/windows/uwp/maps-and-location/get-location) or the current location the map is viewing if you have a map control visible (using the map control Center property).