📜 ⬆️ ⬇️

Nokia Maps API on the example of Maps Explorer

This is the third article dedicated to the Nokia Developer API.
For those who missed, these are the first two parts:
Nokia Music API
Advanced Camera API

Today we will talk about using the Nokia Maps API.



')

Map Explorer


Map Explorer is a simple Silverlight application that demonstrates how to use the Windows Phone 8 Maps API instead of Bing Maps, which were used in Windows Phone 7.

The main page of the application contains buttons and sliders that allow you to change map settings (map view, tilt and direction). More detailed camera settings are available in the application settings menu. The main cartographic functionality that the application demonstrates is receiving and displaying the current position of the phone on the map, searching by keywords, drawing a route and direction from a given point to a destination.

Design


The application is written to demonstrate the capabilities of Windows Phone 8 controls in one window, such as address search, route calculation to the point of interest, and positioning on the map. Saving all controls on the screen allows you to demonstrate an instant visual response when changing parameters. At startup, the application requests access to user location data. It is recommended that you give the application access to them, since a number of functions are based on location data. Once received permission, the application will no longer issue a request.



After receiving a response to the request, the application opens on the start page. It contains elements for direct control of map parameters in the map mode (road, aerial view, hybrid, terrain), in tilt mode (adjustable viewing angle of the map) and direction (allows you to rotate the map in the direction of a given route). Centering and approximation are carried out by dragging or "tweaking".

Access to the functionality of Windows Phone 8, tied to the card data is possible using the buttons in the application menu. The “Search” button opens the data entry form. If one or more objects that match the search criteria are found, the map is shifted by animation to the first object. The “Route” button launches the route building functionality and requires you to enter a destination. A given direction can be enabled by opening the menu and selecting the menu item “Directions On”, two driving modes to choose from: “Driving” and “Walking”.
The button "Find me" starts the definition of user coordinates. When data is received, the animation map moves to the current location. If at the launch of the application the permission to determine the coordinates was not received, the request will be shown again. The permission to determine the coordinates will start the search process.

Additional settings are in the application menu, where you can enable the display of such parameters as attractions, pedestrian routes, and directions. Here you can select the color mode of the map: dark and light.

Architecture Overview




Parsing architecture


User interface

The user interface consists of MainPage and Information AboutPage - derived from PhoneApplicationPage. MainPage contains the map and all the cartographic functionality of this application.

Windows Phone Location API

The Windows Phone Location API is used in the Windows.Devices.Geolocation namespace to determine the current location. The application must have the ID_CAP_LOCATION specified in the WMAppManifest.xml file to use location services.

Maps API

The new Windows Phone 8 Maps APIs offer current map controls (Microsoft.Phone.Maps.Controls) and corresponding map services (Microsoft.Phone.Maps.Services). To use the Maps API, an application must have ID_CAP_LOCATION specified in the WMAppManifest.xml file.

Direct interaction with map settings


Direct interaction with the settings of the control elements of the maps is carried out very simply, as the following example clearly demonstrates. The slider to control the approach of the map is described in the file MainPage.xaml.

<Slider x:Name="PitchSlider" ... Minimum="0" Maximum="70" Value="0" ValueChanged="PitchValueChanged"/> 


The event handler for the ValueChanged event is then executed in the MainPage.xaml.cs file.

 using Microsoft.Phone.Maps.Controls; ... public partial class MainPage : PhoneApplicationPage { ... private void PitchValueChanged(object sender, EventArgs e) { if (PitchSlider != null) { MyMap.Pitch = PitchSlider.Value; } } ... } 


Getting the coordinates of the current location


Retrieving the coordinates of your current location is quite simple using Geolocator asynchronously. The location can be determined not at a specific point, but approximately, it can be displayed in the user interface. To do this, the coordinates are represented as a semi-transparent circle with a radius depending on the accuracy of the definition, on top of the map.

 using System.Device.Location; using Windows.Devices.Geolocation; public partial class MainPage : PhoneApplicationPage { ... private GeoCoordinate MyCoordinate = null; private double _accuracy = 0.0; ... private async void GetCurrentCoordinate() { ... Geolocator geolocator = new Geolocator(); geolocator.DesiredAccuracy = PositionAccuracy.High; try { Geoposition currentPosition = await geolocator.GetGeopositionAsync(TimeSpan.FromMinutes(1), TimeSpan.FromSeconds(10)); _accuracy = currentPosition.Coordinate.Accuracy; ... Dispatcher.BeginInvoke(() => { MyCoordinate = new GeoCoordinate(currentPosition.Coordinate.Latitude, currentPosition.Coordinate.Longitude); ... }); } catch (Exception ex) { //     –      MessageBox.Show("Current location cannot be obtained. Check that location service is turned on in phone settings."); } ... } ... } 


Getting location information


You can request information about a specific location using ReverseGeocodeQuery. In the Map Explorer application, ReverseGeocodeQuery is triggered by tapping a label on the map. The geo-coordinates of each label are saved in the polygon Tag setting at the time of creating the label.

 using using Microsoft.Phone.Maps.Services; public partial class MainPage : PhoneApplicationPage { ... private ReverseGeocodeQuery MyReverseGeocodeQuery = null; ... private void Marker_Click(object sender, EventArgs e) { Polygon p = (Polygon)sender; GeoCoordinate geoCoordinate = (GeoCoordinate)p.Tag; MyReverseGeocodeQuery = new ReverseGeocodeQuery(); MyReverseGeocodeQuery.GeoCoordinate = new GeoCoordinate(geoCoordinate.Latitude, geoCoordinate.Longitude); MyReverseGeocodeQuery.QueryCompleted += ReverseGeocodeQuery_QueryCompleted; MyReverseGeocodeQuery.QueryAsync(); } private void ReverseGeocodeQuery_QueryCompleted(object sender, QueryCompletedEventArgs<IList<MapLocation>> e) { if (e.Error == null) { if (e.Result.Count > 0) { MapAddress address = e.Result[0].Information.Address; String msgBoxText = ""; ... if (address.Country.Length > 0) msgBoxText += "\n" + address.Country; MessageBox.Show(msgBoxText, AppResources.ApplicationTitle, MessageBoxButton.OK); } ... } } ... } 


Search by keyword on the map


GeocodeQuery can be used to search for the location of a given query, such as the name of a city or street. The event handler GeocodeQuery_QueryCompleted is called when searching for a keyword and when building a route. An explanation of the part concerning the route is given below. The result GeocodeQuery is a list that matches the SearchTerm query. Each MapLocation has a GeoCoordinate for positioning a location on a map, as well as a detailed LocationInformation with a name, a description and a MapAdress location.

 using Microsoft.Phone.Maps.Services; public partial class MainPage : PhoneApplicationPage { ... private GeoCoordinate MyCoordinate = null; private List<GeoCoordinate> MyCoordinates = new List<GeoCoordinate>(); private GeocodeQuery MyGeocodeQuery = null; private bool _isRouteSearch = false; // True    ,  false ... private void SearchForTerm(String searchTerm) { ... MyGeocodeQuery = new GeocodeQuery(); MyGeocodeQuery.SearchTerm = searchTerm; MyGeocodeQuery.GeoCoordinate = MyCoordinate == null ? new GeoCoordinate(0, 0) : MyCoordinate; MyGeocodeQuery.QueryCompleted += GeocodeQuery_QueryCompleted; MyGeocodeQuery.QueryAsync(); } private void GeocodeQuery_QueryCompleted(object sender, QueryCompletedEventArgs<IList<MapLocation>> e) { ... if (e.Error == null) { if (e.Result.Count > 0) { if (_isRouteSearch) //        { ... } else //        { //    MyCoordinates     . for (int i = 0; i < e.Result.Count; i++) { MyCoordinates.Add(e.Result[i].GeoCoordinate); } ... } } else { MessageBox.Show("No match found. Narrow your search eg Seattle WA."); } } ... } 


Display route from current point to destination


RouteQuery can be used to get a route from one geographic point to another. The query is launched from the GeocodeQuery_QueryCompleted event handler during the search. The current example uses only two points to build a route — a start and end. The result of RouteQuery is Route.MapRoute, which is a visualization that is easy to apply to map controls. Route is divided into RouteLegs between two waypoints. Each RoutLeg has a set of RouteManeuvers, representing options for maneuvers that can be undertaken on a segment of the path.

 public partial class MainPage : PhoneApplicationPage { ... private GeoCoordinate MyCoordinate = null; private List<GeoCoordinate> MyCoordinates = new List<GeoCoordinate>(); private RouteQuery MyRouteQuery = null; private Route MyRoute = null; private MapRoute MyMapRoute = null; private bool _isRouteSearch = false; // True    ,  false private TravelMode _travelMode = TravelMode.Driving; // Travel mode     .... private void GeocodeQuery_QueryCompleted(object sender, QueryCompletedEventArgs<IList<MapLocation>> e) { ... if (e.Error == null) { if (e.Result.Count > 0) { if (_isRouteSearch) //        { //         MyCoordinates.Add(e.Result[0].GeoCoordinate); //         List<GeoCoordinate> routeCoordinates = new List<GeoCoordinate>(); routeCoordinates.Add(MyCoordinate); routeCoordinates.Add(e.Result[0].GeoCoordinate); CalculateRoute(routeCoordinates); } else //        { ... } } ... } ... } private void CalculateRoute(List<GeoCoordinate> route) { ... MyRouteQuery = new RouteQuery(); MyRouteQuery.TravelMode = _travelMode; MyRouteQuery.Waypoints = route; MyRouteQuery.QueryCompleted += RouteQuery_QueryCompleted; MyRouteQuery.QueryAsync(); } private void RouteQuery_QueryCompleted(object sender, QueryCompletedEventArgs<Route> e) { ... if (e.Error == null) { MyRoute = e.Result; MyMapRoute = new MapRoute(MyRoute); MyMap.AddRoute(MyMapRoute); //       List<string> routeInstructions = new List<string>(); foreach (RouteLeg leg in MyRoute.Legs) { for (int i = 0; i < leg.Maneuvers.Count; i++) { RouteManeuver maneuver = leg.Maneuvers[i]; string instructionText = maneuver.InstructionText; ... routeInstructions.Add(instructionText); } } ... } ... } } 


Additional Information


For a more in-depth study of information about map controls and additional services, please refer to the Guide to the Maps .

Downloads


Map Explorer Project v1.0
Map Explorer application binary file v1.0

This application is located on Nokia Developer Projects , where you can always see the latest activity, report an error, view the code, ask questions or even contribute to the project.

Source: https://habr.com/ru/post/175003/


All Articles