Introduction

As explained in the previous article, the geolocation is one of the most frequently used features in the mobile and beyond, with it we are able to retrieve the location and information about the place in which we find ourselves, and used by an application to have other information, such as restaurants, hotels, places of interest and many other scenarios. We continue in this second article, our path ongeolocation services, we will see how to convert the coordinates of latitude, longitude is in an address, so be displayed in an application with all the information on where we are, and use them in future articles within a control Nokia Maps Nokia, and also an example of how to detect the weather conditions.

The class ReverseGeocodeQuery

Besides the class Geolocator that we find in Namespace Windows.Devices.Geolocation, we have another important class that theWindows Runtime provides for us developers, the class ReverseGeocodeQuery. If you think about it, when we develop an application that makes use of the maps above, or want to give the user more information on the site, such as country name, address, and more, that's the class ReverseGeocodeQuery and what we need. We analyse the main methods, properties, and the only event available:

Propertiesoverview

  • GeoCoordinate
  • IsBusy

The most used method:

  • QueryAsync ()

The only event:

  • QueryCompleted

GeoCoordinate

GeoCoordinate, here we enter the latitude longitude is to ensure that they are then converted into an address from the classReverseGeocodeQuery, all through an object of type GeoCoordinate, then we will see in the example how to do it. IsBusy, read-only property, which is useful if we need to know if the query and in preparation for the recovery of data.

Methods

The method QueryAsync (), when called, executes the query using the coordinates of latitude and longitude, note that this method iscalled albeit with Async end, it is executed asynchronously, but being of type void, not based on the pattern Async / Await. For instance,a method void when it is executed, it not returns any result, as is the case for example, if we call a method of type Int.

Events

The only event QueryCompleted, executed when the query is finished and returns all the data concerning the address, in an object of type IList <MapLocation>. It is exactly all the results that were found by the query enclosed in a collection, because they can also be more than one. To find the information we need to refer to the Result property. Besides this we have:

  • Cancelled:bool type, which is useful to understand when and if asynchronous action has been canceled.
  • Error: also of type bool, which is useful in case you need an error during the execution of the query, in fact, from this property, you will get an object of type Exception AsyncCompletedEventArgs with the error that occurred.
  • Result: we have described before, in other words the query results in a collection of type IList <MapLocation>.
  • UserState: properties of type Object, it returns the unique identifier for the asynchronous task running.

Convert coordinates latitude longitude is in an address.

Until now, you've taken a quick overview of the class ReverseGeocodeQuery, in other words, a brief analysis of the methods, properties,most used, along with the unique event QueryCompleted. For more details on the subject leave the linkto the official documentation of MSDN Library.

Continuing the example that we created in the previous article, for those who had not yet downloaded, the download is available at this link. The application of the test to the previous article was structured as follows.

We have four TextBlock controls, which were used to display the latitude longitude is using the tap on the Button control named Find data. Open the project with Visual Studio 2013; I remember that this example was created with the Professional version. In exploring solutions, double click with the mouse on the MainPage.xaml file. In the GUI, go to delete all the controls from the main grid calledContentPanel, and insert the following code.

01.<Gridx:Name="ContentPanel"Grid.Row="1"Margin="12,0,12,0">
02.<Grid.RowDefinitions>
03.<RowDefinitionHeight="*"/>
04.<RowDefinitionHeight="Auto"/>
05.</Grid.RowDefinitions>
06.
07.<StackPanelOrientation="Horizontal">
08.<StackPanel>
09.<TextBlockText="City "/>
10.<TextBlockText="Country "/>
11.<TextBlockText="CountryCode "/>
12.<TextBlockText="HouseNumber "/>
13.<TextBlockText="PostalCode "/>
14.<TextBlockText="State "/>
15.<TextBlockText="Street "/>
16.</StackPanel>
17.
18.<StackPanel>
19.<TextBlockx:Name="tbkCity"/>
20.<TextBlockx:Name="tbkCountry"/>
21.<TextBlockx:Name="tbkCountryCode"/>
22.<TextBlockx:Name="tbkHouseNumber"/>
23.<TextBlockx:Name="tbkPostalCode"/>
24.<TextBlockx:Name="tbkState"/>
25.<TextBlockx:Name="tbkStreet"/>
26.</StackPanel>
27.</StackPanel>
28.
29.<ButtonGrid.Row="1"x:Name="btnFindCoordinate"Content="Find data"Tap="btnFindCoordinate_Tap"/>
30.</Grid>

Were added other controls TextBlock, to view some of the most important properties, such as City, Country, House number and other data which we will see later. After entering the code xaml, our GUI will take this aspect.

From the image above, you can see that we added than before TextBlock controls that can display all the necessary data on the place inwhich we find ourselves. Now on to part of the code behind, the first thing to do is add the namespace given below, necessary if we want to use the class ReverseGeocodeQuery.

1.using Microsoft.Phone.Maps.Services;

We modify the code on the event tap of the button, invoking an asynchronous method called GetAddress ().

1.privatevoidbtnFindCoordinate_Tap(objectsender, System.Windows.Input.GestureEventArgs e)
2.{
3.GetAddress();
4.}

Now the code you need to search for information about the place we are.

01.publicasync voidGetAddress()
02.{
03.var latitude = 0d;
04.var longitude = 0d;
05.var locator = newGeolocator();
06.var reversegeocodequery = newReverseGeocodeQuery();
07.
08.if(!locator.LocationStatus.Equals(PositionStatus.Disabled))
09.{
10.var position = await locator.GetGeopositionAsync();
11.latitude = position.Coordinate.Latitude;
12.longitude = position.Coordinate.Longitude;
13.
14.
15.reversegeocodequery.GeoCoordinate = newGeoCoordinate(latitude, longitude);
16.reversegeocodequery.QueryAsync();
17.
18.reversegeocodequery.QueryCompleted += (sender, args) =>
19.{
20.if(!args.Result.Equals(null))
21.{
22.var result = args.Result.FirstOrDefault();
23.
24.tbkCity.Text = result.Information.Address.City;
25.tbkCountry.Text = result.Information.Address.Country;
26.tbkCountryCode.Text = result.Information.Address.CountryCode;
27.tbkHouseNumber.Text = result.Information.Address.HouseNumber;
28.tbkPostalCode.Text = result.Information.Address.PostalCode;
29.tbkState.Text = result.Information.Address.State;
30.tbkStreet.Text = result.Information.Address.Street;
31.}
32.};
33.
34.}
35.
36.else
37.{
38.MessageBox.Show("Service Geolocation not enabled!", AppResources.ApplicationTitle, MessageBoxButton.OK);
39.return;
40.}
41.}

We are going to analyze the content. Concerning the class Geolocator, compared to the example of the previous article nothing changes, everything remains unchanged. The news part of this line of code, here we declare a new object of type ReverseGeocodeQuery.

1.var reversegeocodequery = newReverseGeocodeQuery();

The remaining part of the code.

01.reversegeocodequery.GeoCoordinate = newGeoCoordinate(latitude, longitude);
02.reversegeocodequery.QueryAsync();
03.
04.reversegeocodequery.QueryCompleted += (sender, args) =>
05.{
06.if(!args.Result.Equals(null))
07.{
08.var result = args.Result.FirstOrDefault();
09.
10.tbkCity.Text = result.Information.Address.City;
11.tbkCountry.Text = result.Information.Address.Country;
12.tbkCountryCode.Text = result.Information.Address.CountryCode;
13.tbkHouseNumber.Text = result.Information.Address.HouseNumber;
14.tbkPostalCode.Text = result.Information.Address.PostalCode;
15.tbkState.Text = result.Information.Address.State;
16.tbkStreet.Text = result.Information.Address.Street;
17.}
18.};

Enhance the property geo-coordinates, passing as arguments the latitude longitude is that the class Geolocator noted, and later called the method QueryAsync (). Finally we endorse the event QueryCompleted, executed when the query has been executed and completed, restoring all the data in a collection of type IList <MapLocation>. The data are stored in the Address property, we have the property City,Country, CountryCode, HouseNumber, PostalCode, and State Street, or those of greater use, although in reality they are not the only ones, but we have others that can be used by the 'intellisense, Visual Studio 2013 will show all available values. Now that we have both the code and the GUI code-behind, it is time to run the application, but before you start debugging you need to activate the required capability. In exploring solutions expand the Properties folder and double-click with the mouse on the file WMAppManifest.xaml, go to the section functionality and activate the Capability ID_CAP_MAP, as shown in the figure.

After this activity, we can finally debug. F5 key, start the application, which will initially be as shown in the figure.

Taking a tap on the Find button, and everything goes well; we will have all the address data on the basis of latitude longitude is that the class Geolocator noted.

Conclusion

In this second article, we saw how to get latitude and longitude using other data on the site in which we find ourselves, all thanks to the class ReverseGeocodeQuery, in the next article, we'll take a look at the class GeocodeQuery, which has the same approach, with the difference that transforms the data to an address in coordinates latitude longitude is

See also

Another important place to find a huge amount of Windows Phone related articles is the TechNet Wiki itself. The best entry point is Windows Phone Resources on the TechNet Wiki.