Geocoding In Universal Windows Platform (UWP)

Geocoding is the process of converting an address into geographical coordinates (longitude, latitude) and reverse geocoding is, well, the reverse of it. It converts geographical coordinates to an address.

In this tutorial we'll learn how to convert any location/address entered by user to geographical coordinates and display a pushpin at that specified location in UWP.

To add map to your app, read my previous post:

We'll modify the UI a bit, we'll add a textbox and a button. The following code can be used to achieve this. 
  1. <StackPanel Orientation="Vertical">  
  2.     <StackPanel Orientation="Horizontal">  
  3.         <TextBox Name="enterValue" PlaceholderText="Enter address/coordinates" FontSize="24" Margin="8" Width="300" Height="44" />  
  4.         <Button Content="Go" Click="Button_Click"></Button>  
  5.     </StackPanel>  
  6.     <Maps:MapControl x:Name="MapControl1" MapServiceToken="vqtfpD6NhEco0aJ1jCwf~lOLpp3bZwLbt6lr2uS6J0g~AudUQ4mxwgJUEmgqu- dn6AUjwJqTaTu6KBnxkNSklnpFSU5FuZrdezfHWddFHA-A" ZoomLevel="3" Height="550" />  
  7. </StackPanel>  
In the click event of button, read the value entered by the user and then pass it on to another function called Geocode which contains the actual logic of geo-coding. The code for the click event of button is given below.
  1. private void Button_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     string valueEntered = enterValue.Text;  
  4.     if (valueEntered != null || valueEntered != "")  
  5.     {  
  6.         Geocode(valueEntered);  
  7.     }  
  8. }  
The above code simply assigns the value of the textbox to a string and before passing it as an argument to the Geocode function, makes sure that it's not empty.
  1. private async void Geocode(string valueEntered)  
  2. {  
  3.   
  4.     BasicGeoposition location = new BasicGeoposition();  
  5.     location.Latitude = 33.6667;  
  6.     location.Longitude = 73.1667;  
  7.     Geopoint hintPoint = new Geopoint(location);  
  8.     MapLocationFinderResult result =  
  9.         await MapLocationFinder.FindLocationsAsync(valueEntered, hintPoint);  
  10.   
  11.     if (result.Status == MapLocationFinderStatus.Success)  
  12.     {  
  13.         BasicGeoposition newLocation = new BasicGeoposition();  
  14.         newLocation.Latitude = result.Locations[0].Point.Position.Latitude;  
  15.         newLocation.Longitude = result.Locations[0].Point.Position.Longitude;  
  16.         Geopoint geoPoint = new Geopoint(newLocation);  
  17.         MapAddress mapAddress = result.Locations[0].Address;  
  18.         string address = mapAddress.FormattedAddress;  
  19.         AddMapIcon(geoPoint, address);  
  20.     }  
  21. }  
In the above code we have created a variable "location" of type BasicGeopostion and set it's longitude and latitude to the coordinates of Pakistan.

The reason behind setting its coordinates to Pakistan is that this variable will be used as a hint point while geo-coding. Given my geographical location I am assuming that most of my users will be from Pakistan and that is why I have used that as a hint point. You can modify the hint point based on the geographical source of your queries.

Now, let's dissect line 8-9.

MapLocationFinderResult returns the result of MapLocationFinderQuery.

MapLocationFinder is a static class that has three functions,

 

  1. FindLocationsAsync(string address, Geopoint referencePoint) , this function will return just one result.

  2. FindLocationsAsync(string address, Geopoint referencePoint), int maxCount), this function returns the number of maxCount result.

  3. FindLocationsAtAsync(Geopoint queryPoint), this is called reverse geo-coding.

You must have noticed the use of async and await keywords. The reason behind it is that the return type of all three functions of MapLocationFinder class is IAsyncOperation<TResult>.

The marked async method uses the await keyword, which tells the compiler that it can't move past that point until the awaited asynchronous process is complete.

Finally, the value returned by FindLocationsAsync(), which is a read-only collection of elements that can be accessed by index, is stored in the variable result of type MapLocationFinderResult.

The if statement compares the status of the result query to the enum MapLocationFinderStatus and if it is successful we proceed ahead.

Finally, we create a new variable called "newLocation" of type BasicGeoposition and assign its latitude and longitude to the coordinates of Location at index 0 of list Locations. Then we assign the address of this location to another variable mapAddress of type MapAddress which can hold an address of a geographical location. To store the address in a string type variable we will simply call the read-only property FormattedAddress of MapAddress which returns the complete result in a string format.

We also created a new Geopoint variable from the newLocation variable of type BasicGeoposition using the parameterized constructor.

Now we pass geoPoint and address to another function called AddMapIcon().

  1. private async void AddMapIcon(Geopoint geoPoint, String address)  
  2. {  
  3.     if (mapIcon != null)  
  4.     {  
  5.         MapControl1.MapElements.Remove(mapIcon);  
  6.     }  
  7.     mapIcon.Location = geoPoint;  
  8.     mapIcon.Title = address;  
  9.   
  10.     MapControl1.MapElements.Add(mapIcon);  
  11.     await MapControl1.TrySetViewAsync(geoPoint);  
  12. }  
We declared mapIcon as a class field just to make sure that there is only a single pushpin at any given time. The above code simply removes mapIcon from the MapControl1 if it's not null. Otherwise set it's Location and Title to the passed parameters. Line 10 adds the mapIcon to MapControl1 and Line 11 animates camera movement as map shifts it's center from one location to another.

In the next post we will learn how to reverse geo-code.
 
Read more articles on Universal Windows Platform:

 


Similar Articles