LUMIA APP LABS: HERE MAPS AND LOCATION APIS FOR WINDOWS PHONE

  • 5,488 views
Uploaded on

Learn how to use the location-related capabilities of Nokia Lumia hardware, Windows Phone Location APIs, and the HERE location platform, including HERE Maps, HERE Drive, and HERE Transit. Starting …

Learn how to use the location-related capabilities of Nokia Lumia hardware, Windows Phone Location APIs, and the HERE location platform, including HERE Maps, HERE Drive, and HERE Transit. Starting with the basics — retrieving location information from the phone and registering for background location notifications — this presentation then looks at map-related services APIs available for your Windows Phone apps for Nokia Lumia smartphones, including APIs for map rendering, geocoding, and routing controls.

For detailed documentation on the maps features in Lumia smartphones see http://www.developer.nokia.com/Resources/Library/Lumia/#!guide-to-the-wp8-maps-api.html and don't forget to check out the examples at http://projects.developer.nokia.com/WP8MapsExamples

For more details about developing for Nokia Lumia smartphones visit http://www.developer.nokia.com/windowsphone

Check out details of the other Lumia App Labs, including the future schedule, here: http://www.developer.nokia.com/Develop/Windows_Phone/Learn/

More in: Technology , Business
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
5,488
On Slideshare
0
From Embeds
0
Number of Embeds
5

Actions

Shares
Downloads
361
Comments
0
Likes
3

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. MAPS AND LOCATIONAPIS FOR WINDOWSPHONEJukka SilvennoinenChief EngineerNokia Developer Offering
  • 2. CONTENT. • Location API usages • Maps API briefly • Some workarounds • Examples available © 2012 Nokia. All rights reserved. 4/18/2013 © 2012 Microsoft. All rights reserved.
  • 3. LOCATIONAPIUSAGES © 2012 Nokia. All rights reserved. 4/18/2013 © 2012 Microsoft. All rights reserved.
  • 4. LOCATION API GENERAL INFO• Geolocator API is new for WP8 offers following features: • Accessible from managed and native code • Allows application to specify the desired accuracy of the location as well as maximum time allowed for acquiring one shot location result • Applications can request location updates after a specified time interval or a distance threshold is crossed • Convergent with Windows 8 with only minor differences. Most code should be reusable on both platforms• GeoCoordinateWatcher API still available in Windows Phone 8.• Location tracking applications can be enabled to run in the background• To enable location features add the capability: ID_CAP_LOCATION to the manifest file (WMAppManifest.xml) © 2012 Nokia. All rights reserved. 4/18/2013 © 2012 Microsoft. All rights reserved.
  • 5. USING THE GEOCOORDINATEWATCHER APIgeolocator = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);geolocator.MovementThreshold = 20; // 20 metersgeolocator.PositionChanged += geolocator_PositionChanged;…private void Button_Click_1(object sender, RoutedEventArgs e){ if (!tracking){ geolocator.Start(); tracking = true; StarStopBut.Content = "Stop tracking"; } else { geolocator.Stop(); tracking = false; StarStopBut.Content = "Start tracking"; }}…void geolocator_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> args){ latitudeBox.Text = args.Position.Location.Latitude.ToString("0.00"); longitudeBox.Text = args.Position.Location.Longitude.ToString("0.00");} © 2012 Nokia. All rights reserved. 4/18/2013 © 2012 Microsoft. All rights reserved.
  • 6. WP8USING THE GEOLOCATOR APIprivate void TrackLocation_Click(object sender, RoutedEventArgs e){ if (!tracking) { geolocator = new Geolocator(); geolocator.DesiredAccuracy = PositionAccuracy.High; geolocator.MovementThreshold = 100; // The units are meters. geolocator.PositionChanged += geolocator_PositionChanged; tracking = true; TrackLocationButton.Content = "stop tracking"; } else { geolocator.PositionChanged -= geolocator_PositionChanged; geolocator = null; tracking = false; TrackLocationButton.Content = "track location"; StatusTextBlock.Text = "stopped"; }}void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args){ Dispatcher.BeginInvoke(() => { LatitudeTextBlock.Text = args.Position.Coordinate.Latitude.ToString("0.00"); LongitudeTextBlock.Text = args.Position.Coordinate.Longitude.ToString("0.00"); });} © 2012 Nokia. All rights reserved. 4/18/2013 © 2012 Microsoft. All rights reserved.
  • 7. NOTE THE DIFFERENCESystem.Device.Location.GeoCoordinate: http://bit.ly/Yg9rIb• Used by GeoCoordinateWatcherWindows.Devices.Geolocation.Geocoordinate: http://bit.ly/Yg9wvq• used by Geolocator, as well as Maps API in generalMyCoord = newSystem.Device.Location.GeoCoordinate(Position.Coordinate.Latitude,Position.Coordinate.Longitude); © 2012 Nokia. All rights reserved. 4/18/2013 © 2012 Microsoft. All rights reserved.
  • 8. WP8REGISTER FOR BACKGROUND TRACKING In WMAppManifest.xml <DefaultTask Name="_default" NavigationPage="MainPage.xaml"> <BackgroundExecution> <ExecutionType Name="LocationTracking" /> </BackgroundExecution> </DefaultTask> In App.xaml: <shell:PhoneApplicationService Launching="Application_Launching" Closing="Application_Closing" Activated="Application_Activated" Deactivated="Application_Deactivated" RunningInBackground="Application_RunningInBackground"/> © 2012 Nokia. All rights reserved. 4/18/2013 © 2012 Microsoft. All rights reserved.
  • 9. WP8TRACK IF APP IS RUNNING IN BG In App.xaml.cs private void Application_Activated(object sender, ActivatedEventArgs e) { MyGlobalObject.RunningInBackground = false; } // raised when the user navigates away from your app while it is actively // tracking location and your app begins to run in the background private void Application_RunningInBackground(object sender, RunningInBackgroundEventArgs args) { MyGlobalObject.RunningInBackground = true; // Suspend all unnecessary processing such as UI updates } © 2012 Nokia. All rights reserved. 4/18/2013 © 2012 Microsoft. All rights reserved.
  • 10. WP8DEACTIVATION OF APPS RUNNING IN THE BACKGROUND• Event handlers for the PositionChanged and StatusChanged events of the Geolocator are removed.• GeoCoordinateWatcher.Stop().• Battery Saver is active.• Device memory is low.• The user disables Location Services on the phone.• Another app begins running in the background.• The app has run in the background for 4 hours without user interaction.. © 2012 Nokia. All rights reserved. 4/18/2013 © 2012 Microsoft. All rights reserved.
  • 11. MAPAPIUSAGES © 2012 Nokia. All rights reserved. 4/18/2013 © 2012 Microsoft. All rights reserved.
  • 12. WP8MAP API GENERAL INFO• New Maps API introduced with Windows Phone 8• Bing Maps are now deprecated, but will continue to work• Use Bing Maps only when upgrading from WP7.5• To enable location features add the capability: ID_CAP_MAP (and maybe also ID_CAP_LOCATION) to the manifest file (WMAppManifest.xml)• Maps data is shared, and if downloaded before (by any app) you can use it offline. © 2012 Nokia. All rights reserved. 4/18/2013 © 2012 Microsoft. All rights reserved.
  • 13. WP8 ADDING MAP TO THE APPxmlns:maps="clr-namespace:Microsoft.Phone.Maps.Controls;assembly=Microsoft.Phone.Maps"... <maps:Map Name="map1" Center="47.6097, -122.3331" ZoomLevel="10" Heading="90" Pitch="45" CartographicMode="Hybrid" ColorMode="Dark" PedestrianFeaturesEnabled="true" HorizontalAlignment="Stretch“ VerticalAlignment="Stretch"> </maps:Map> © 2012 Nokia. All rights reserved. 4/18/2013 © 2012 Microsoft. All rights reserved.
  • 14. WP8SUPPORTED FUNCTIONALITYAdd content User services• Markers (MapOverlay) • Geocoding (convert address to location)• Polylines, polygons (MapElement) • Reverse geocoding (convert location to address)• Route • RoutingChange settings• animation modes (parabolic, linear, none)• heading, pitch & zoom levels• map color (light/dark) modes• map types (road, Arial, hybrid, terrain)• pedestrian features & landmarks on/off’ Full list of APIs: http://bit.ly/WkUacZ © 2012 Nokia. All rights reserved. 4/18/2013 © 2012 Microsoft. All rights reserved.
  • 15. WP8 ADDING ‘MARKER’Rectangle rect = new Rectangle();rect.Fill = new SolidColorBrush(Colors.Red);rect.Height = 15;rect.Width = 15;MapOverlay overlay = new MapOverlay();overlay.Content = rect;overlay.GeoCoordinate = new GeoCoordinate(47.6097, -122.3331);overlay.PositionOrigin = new Point(0.5, 0.5);MapLayer layer = new MapLayer();layer.Add(overlay);MyMapControl.Layers.Add(layer); © 2012 Nokia. All rights reserved. 4/18/2013 © 2012 Microsoft. All rights reserved.
  • 16. ADDING POLYGON WP8GeoCoordinateCollection RectLocations = CreateRectangle(new GeoCoordinate(60.35, 24.60), new GeoCoordinate(60.25, 24.80));//Set the polygon propertiesPolyRect.Path = RectLocations;PolyRect.FillColor = Color.FromArgb(0x55, 0x00, 0x00, 0x00);PolyRect.StrokeColor = Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF);PolyRect.StrokeThickness = 4;map1.MapElements.Add(PolyRect);public static GeoCoordinateCollection CreateRectangle(GeoCoordinate topLeft, GeoCoordinate bottomRight){ var locations = new GeoCoordinateCollection(); locations.Add(new GeoCoordinate(topLeft.Latitude, topLeft.Longitude)); locations.Add(new GeoCoordinate(topLeft.Latitude, bottomRight.Longitude)); locations.Add(new GeoCoordinate(bottomRight.Latitude, bottomRight.Longitude)); locations.Add(new GeoCoordinate(bottomRight.Latitude, topLeft.Longitude)); return locations;} © 2012 Nokia. All rights reserved. 4/18/2013 © 2012 Microsoft. All rights reserved.
  • 17. ROUTE WP8List<GeoCoordinate> points = new List<GeoCoordinate>();points.Add(new GeoCoordinate(47.603848, -122.029953));points.Add(new GeoCoordinate(47.669444, -122.123889));RouteQuery query = new RouteQuery();query.Waypoints = points;query.QueryCompleted += MyQuery_QueryCompleted;query.QueryAsync();void MyQuery_QueryCompleted(object sender, QueryCompletedEventArgs<Route> e){ Route route = e.Result; MapRoute mapRoute = new MapRoute(route); MyMapControl.AddRoute(mapRoute); foreach (RouteLeg leg in MyRoute.Legs) { foreach (RouteManeuver maneuver in leg.Maneuvers) maneuver.InstructionText; } © 2012 Nokia. All rights reserved. 4/18/2013} © 2012 Microsoft. All rights reserved.
  • 18. WP8 REVERSEGEOCODINGReverseGeocodeQuery geoQ = new ReverseGeocodeQuery();geoQ.QueryCompleted += geoQ_QueryCompleted;geoQ.GeoCoordinate = map1.Center;geoQ.QueryAsync();void geoQ_QueryCompleted(object sender, QueryCompletedEventArgs<IList<MapLocation>> e){ if (e.Result.Count() > 0) { string showString = e.Result[0].Information.Name; showString = showString + "nAddress: "; showString = showString + "n" + e.Result[0].Information.Address.HouseNumber; showString = showString + "n" + e.Result[0].Information.Address.Street; showString = showString + "n" + e.Result[0].Information.Address.PostalCode; showString = showString + "n" + e.Result[0].Information.Address.City; showString = showString + "n" + e.Result[0].Information.Address.Country; showString = showString + "n" + e.Result[0].Information.Address.CountryCode; showString = showString + "nDescription: "; showString = showString + "n" + e.Result[0].Information.Description.ToString(); MessageBox.Show(showString); }} © 2012 Nokia. All rights reserved. 4/18/2013 © 2012 Microsoft. All rights reserved.
  • 19. SOME LIMITATIONSANDWORKAROUNDS © 2012 Nokia. All rights reserved. 4/18/2013 © 2012 Microsoft. All rights reserved.
  • 20. WP8NO DIRECT WAY FOR FITTING CONTENT(1/2)LocationRectangle setRect = null;setRect = LocationRectangle.CreateBoundingRectangle(myPolyGon.Path);map1.SetView(setRect);LocationRectangle setRect = null;GeoCoordinate[] geoArr = new GeoCoordinate[markerLayer.Count()];for (var p = 0; p < markerLayer.Count(); p++){geoArr[p] = markerLayer[p].GeoCoordinate;}setRect = LocationRectangle.CreateBoundingRectangle(geoArr);map1.SetView(setRect); © 2012 Nokia. All rights reserved. 4/18/2013 © 2012 Microsoft. All rights reserved.
  • 21. WP8NO DIRECT WAY FOR FITTING CONTENT´(2/2) double north = 0; double west = 0; double south = 0; double east = 0; north = south = polyline.Path[0].Latitude; west = east = polyline.Path[0].Longitude; foreach (var p in polyline.Path.Skip(1)) { if (north < p.Latitude) north = p.Latitude; if (west > p.Longitude) west = p.Longitude; if (south > p.Latitude) south = p.Latitude; if (east < p.Longitude) east = p.Longitude; } setRect = new LocationRectangle(north, west, south, east); map1.SetView(setRect); © 2012 Nokia. All rights reserved. 4/18/2013 © 2012 Microsoft. All rights reserved.
  • 22. WP8HIDING CONTENTif (markerLayer != null){ for (var i = 0; i < markerLayer.Count(); i++){ Ellipse markker = (markerLayer[i].Content as Ellipse); if (markker != null){ if (markker.Visibility == System.Windows.Visibility.Visible){ markker.Visibility = System.Windows.Visibility.Collapsed; }else{ markker.Visibility = System.Windows.Visibility.Visible; } } }}if (poly.StrokeColor == Color.FromArgb(0xFF, 0x00, 0x00, 0xFF)){ poly.FillColor = Color.FromArgb(0x00, 0x00, 0xFF, 0x00); poly.StrokeColor = Color.FromArgb(0x00, 0x00, 0x00, 0xFF);}else{ poly.FillColor = Color.FromArgb(0x55, 0x00, 0xFF, 0x00); poly.StrokeColor = Color.FromArgb(0xFF, 0x00, 0x00, 0xFF);} © 2012 Nokia. All rights reserved. 4/18/2013 © 2012 Microsoft. All rights reserved.
  • 23. WP8MAKING DRAGGABLE MARKERS (1/2)Add event handled for the touch events:System.Windows.Input.Touch.FrameReported += Touch_FrameReported;Add internal boolean variable draggingNow to indicatedragging status and set it to dragging inside mouse handlerset for the marker. Also disable the map while we dodraggingvoid marker_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)) { if (oneMarker != null){ draggingNow = true; map1.IsEnabled = false; }} © 2012 Nokia. All rights reserved. 4/18/2013 © 2012 Microsoft. All rights reserved.
  • 24. WP8MAKING DRAGGABLE MARKERS (2/2)In Touch_FrameReported move the marker with touchevents, and stop dragging with TouchAction.Up:void Touch_FrameReported(object sender, TouchFrameEventArgs e){ if (draggingNow == true) { TouchPoint tp = e.GetPrimaryTouchPoint(map1); if (tp.Action == TouchAction.Move){ if (oneMarker != null) { oneMarker.GeoCoordinate = map1.ConvertViewportPointToGeoCoordinate(tp.Position); } }else if (tp.Action == TouchAction.Up){ draggingNow = false; map1.IsEnabled = true; } }} © 2012 Nokia. All rights reserved. 4/18/2013 © 2012 Microsoft. All rights reserved.
  • 25. SAVE/SHARE MAP IMAGEprivate void SaveButton_Click(object sender, EventArgs e) { WriteableBitmap wb = new WriteableBitmap(mapControl, null); wb.Render(mapControl, null); MemoryStream memoryStream = new MemoryStream(); wb.SaveJpeg(memoryStream, wb.PixelWidth, wb.PixelHeight, 0, 80); MediaLibrary library = new MediaLibrary(); library.SavePictureToCameraRoll("SavedMap_" + DateTime.Now.ToString() + ".jpg", memoryStream.GetBuffer()); } © 2012 Nokia. All rights reserved. 4/18/2013 © 2012 Microsoft. All rights reserved.
  • 26. EXAMPLES © 2012 Nokia. All rights reserved. 4/18/2013 © 2012 Microsoft. All rights reserved.
  • 27. WP8ALL IN ONE PLACENokia project:http://projects.developer.nokia.com/WP8MapsExamples1. Hello Map 12. Routing2. Map events 13. Advanced Routing3. Map interaction 14. Location Selector4. Simple Map content 15. Area Selector5. More Map content 16. LaunchDirectionsTask6. Draggable Marker 17. LaunchMapSearchTask7. Map markers 18. LaunchMapTask8. Dynamic polyline 19. GetMyGeoposition9. My Location 20. TrackMyPosition And TrackMyPositionTwo10. Geo Coding 21. TrackMeInBackground11. Reverse Geo coding © 2012 Nokia. All rights reserved. 4/18/2013 © 2012 Microsoft. All rights reserved.
  • 28. Thank you!© 2012 Nokia. All rights reserved. 4/18/2013© 2012 Microsoft. All rights reserved.