Search code examples
c#uwpbing-mapsgeopointsuwp-maps

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


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?


Solution

  • 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.