12 maps, geolocation, and geofencing
DESCRIPTION
Building Apps for Windows Phone 8.1 Jump Start . Videos at: http://channel9.msdn.com/Series/Building-Apps-for-Windows-Phone-8-1TRANSCRIPT
![Page 1: 12 maps, geolocation, and geofencing](https://reader035.vdocument.in/reader035/viewer/2022062405/5560b628d8b42af43b8b49e8/html5/thumbnails/1.jpg)
Matthias Shapiro @matthiasshapAndy Wigley @andy_wigley
Maps, Geolocation and Geofencing
30 April 2014
Building Apps for Windows Phone 8.1 Jump Start
WinRT Apps & Silverlight
![Page 2: 12 maps, geolocation, and geofencing](https://reader035.vdocument.in/reader035/viewer/2022062405/5560b628d8b42af43b8b49e8/html5/thumbnails/2.jpg)
In this module…Launching mapping apps
Programming the Map control
Location APIs
Geofencing
![Page 3: 12 maps, geolocation, and geofencing](https://reader035.vdocument.in/reader035/viewer/2022062405/5560b628d8b42af43b8b49e8/html5/thumbnails/3.jpg)
Launching mapping apps
![Page 4: 12 maps, geolocation, and geofencing](https://reader035.vdocument.in/reader035/viewer/2022062405/5560b628d8b42af43b8b49e8/html5/thumbnails/4.jpg)
Mapping URI scheme—Bing Maps:
Can be used for all Windows Store and Windows XAML Phone apps
Subset on Windows Phone appsView: cp, lvl, bb, trfc, collectionSearch: where, qRoute: rtpSee MSDN// The URI to launchstring uriToLaunch = @"bingmaps:?cp=51.501156~-0.141706&lvl=17";var uri = new Uri(uriToLaunch);
Windows.System.Launcher.LaunchUriAsync(uri);
![Page 5: 12 maps, geolocation, and geofencing](https://reader035.vdocument.in/reader035/viewer/2022062405/5560b628d8b42af43b8b49e8/html5/thumbnails/5.jpg)
Driving & Walking Directions - ms-drive-to:
Driving - ms-drive-to:Walking - ms-walk-to:Parameters: destination.latitude destination.longitude destination.name
If no apps available, invites search for suitable app in Store
May display disambiguation dialog on Phone// The URI to launchstring uriToLaunch = @"ms-drive-to:?destination.latitude=47.6451413797194" + "&destination.longitude=-122.141964733601&destination.name=Redmond, WA";var uri = new Uri(uriToLaunch);
Windows.System.Launcher.LaunchUriAsync(uri);
![Page 6: 12 maps, geolocation, and geofencing](https://reader035.vdocument.in/reader035/viewer/2022062405/5560b628d8b42af43b8b49e8/html5/thumbnails/6.jpg)
Launching mapping apps
demo
![Page 7: 12 maps, geolocation, and geofencing](https://reader035.vdocument.in/reader035/viewer/2022062405/5560b628d8b42af43b8b49e8/html5/thumbnails/7.jpg)
Windows Runtime Apps Map Control
![Page 8: 12 maps, geolocation, and geofencing](https://reader035.vdocument.in/reader035/viewer/2022062405/5560b628d8b42af43b8b49e8/html5/thumbnails/8.jpg)
Many Map controls…
Silverlight 8.0/8.1 apps:Microsoft.Phone.Maps.Controls.Map- “Here Maps Control”
Windows Runtime Phone 8.1 AppsWindows.UI.Xaml.Maps.MapControl
Silverlight 7.x apps:Microsoft.Phone.Controls.Maps.Map- “Bing Maps Control” Windows 8.x Store
AppsBing.Maps.Map
![Page 9: 12 maps, geolocation, and geofencing](https://reader035.vdocument.in/reader035/viewer/2022062405/5560b628d8b42af43b8b49e8/html5/thumbnails/9.jpg)
The new map control: Windows.UI.Xaml.Maps.MapControlSignificant API changeGeofencingSimpler/more consistent geometry
MapIcon
MapControl.Children -> XAML objects bindable
Geopoint Improved custom tile source support
New/different in Windows Phone 8.1 mapping
![Page 10: 12 maps, geolocation, and geofencing](https://reader035.vdocument.in/reader035/viewer/2022062405/5560b628d8b42af43b8b49e8/html5/thumbnails/10.jpg)
Pins• XAML panel children• MapIcons
Shapes and tiles• Polyline/Polygons• Routes• Tile source• Traffic• Extruded buildings
Ways to display your content
![Page 11: 12 maps, geolocation, and geofencing](https://reader035.vdocument.in/reader035/viewer/2022062405/5560b628d8b42af43b8b49e8/html5/thumbnails/11.jpg)
MapItemsControl
MapControl attached properties• MapControl.Location—geopoint• MapControl.NormalizedAnchorPosition—point (0-1 in x, y)
XAML Children
![Page 12: 12 maps, geolocation, and geofencing](https://reader035.vdocument.in/reader035/viewer/2022062405/5560b628d8b42af43b8b49e8/html5/thumbnails/12.jpg)
There are three types • MapIcons
• Image• Text (optional)
• MapPolygons• MapPolylines
Map elements
![Page 13: 12 maps, geolocation, and geofencing](https://reader035.vdocument.in/reader035/viewer/2022062405/5560b628d8b42af43b8b49e8/html5/thumbnails/13.jpg)
Tiles overlay on base map
Multiple data sources• HTTPMapTileDataSource—images from web• LocalMapTileDataSource—images from disk• CustomMapTileDataSource—bitmap deferral request
MapTileLayer
TileSources
![Page 14: 12 maps, geolocation, and geofencing](https://reader035.vdocument.in/reader035/viewer/2022062405/5560b628d8b42af43b8b49e8/html5/thumbnails/14.jpg)
Layer drawing order
XAML Children(icon, polyline, polygon) Map
elementsForeground overlayRoad overlayArea overlay
Background overlayBackground replacement
LabelsRoadsArea (parks, industrial, etc.)
Background (land/water)
![Page 15: 12 maps, geolocation, and geofencing](https://reader035.vdocument.in/reader035/viewer/2022062405/5560b628d8b42af43b8b49e8/html5/thumbnails/15.jpg)
Drawing onto a Map
demo
![Page 16: 12 maps, geolocation, and geofencing](https://reader035.vdocument.in/reader035/viewer/2022062405/5560b628d8b42af43b8b49e8/html5/thumbnails/16.jpg)
XAML—databound small set of interactive XAML elements which must be shown
Map icons—larger amounts of elements which can be shown with best effort
Tile source—display tiled images with finer grained layering
Displaying content—the map
Tips!
![Page 17: 12 maps, geolocation, and geofencing](https://reader035.vdocument.in/reader035/viewer/2022062405/5560b628d8b42af43b8b49e8/html5/thumbnails/17.jpg)
Location WinRT APIs
![Page 18: 12 maps, geolocation, and geofencing](https://reader035.vdocument.in/reader035/viewer/2022062405/5560b628d8b42af43b8b49e8/html5/thumbnails/18.jpg)
Location service
Location service
Core logic
CellWiFi GNSS
Geofence core
Geofencing WinRT API
GeofencesMicrosoft Positionin
g Services
Geocoordinate .NET API
Geofence
software tracking
Geolocation WinRT API
![Page 19: 12 maps, geolocation, and geofencing](https://reader035.vdocument.in/reader035/viewer/2022062405/5560b628d8b42af43b8b49e8/html5/thumbnails/19.jpg)
• Namespace: Windows.Devices.Geolocation
• Session types:• Single position session• Use cached positions if possible
• Tracking session• Distance based or periodic
• Use DesiredAccuracyInMeters when you need more granularity than high/default
• Use PositionSource to identify how the position was acquired
Geolocation API recapParity with Windows 8.1
Supported in: Windows Store apps
Windows Phone Store apps (Silverlight 8, Silverlight 8.1, WinRt )
![Page 20: 12 maps, geolocation, and geofencing](https://reader035.vdocument.in/reader035/viewer/2022062405/5560b628d8b42af43b8b49e8/html5/thumbnails/20.jpg)
How to Get the Current Location
Geolocator geolocator = new Geolocator(); geolocator.DesiredAccuracyInMeters = 50; try { Geoposition geoposition = await geolocator.GetGeopositionAsync( maximumAge: TimeSpan.FromMinutes(5), timeout: TimeSpan.FromSeconds(10) ); LatitudeTextBlock.Text = geoposition.Coordinate.Latitude.ToString("0.00"); LongitudeTextBlock.Text = geoposition.Coordinate.Longitude.ToString("0.00"); } catch (UnauthorizedAccessException) { // the app does not have the right capability or the location master switch is off StatusTextBlock.Text = "location is disabled in phone settings."; }
![Page 21: 12 maps, geolocation, and geofencing](https://reader035.vdocument.in/reader035/viewer/2022062405/5560b628d8b42af43b8b49e8/html5/thumbnails/21.jpg)
How to Track Location
private void TrackLocation_Click(object sender, RoutedEventArgs e) { if (!tracking) { geolocator = new Geolocator(); geolocator.DesiredAccuracy = PositionAccuracy.High; geolocator.MovementThreshold = 100; // The units are meters. geolocator.StatusChanged += geolocator_StatusChanged; geolocator.PositionChanged += geolocator_PositionChanged; tracking = true; } else { geolocator.PositionChanged -= geolocator_PositionChanged; geolocator.StatusChanged -= geolocator_StatusChanged; geolocator = null; tracking = false; } }
![Page 22: 12 maps, geolocation, and geofencing](https://reader035.vdocument.in/reader035/viewer/2022062405/5560b628d8b42af43b8b49e8/html5/thumbnails/22.jpg)
Mapping services WinRT APIs
![Page 23: 12 maps, geolocation, and geofencing](https://reader035.vdocument.in/reader035/viewer/2022062405/5560b628d8b42af43b8b49e8/html5/thumbnails/23.jpg)
• Simple DrivingMapRouteFinderResult result = await MapRouteFinder.GetDrivingRouteAsync(a, b);
• Waypoints and Traffic MapRouteFinderResult result = await MapRouteFinder.GetDrivingRouteFromWaypointsAsync(collection, MapRouteOptimization.TimeWithTraffic);
• WalkingMapRouteFinderResult result = await MapRouteFinder.GetWalkingRouteAsync(a, b);
Windows.Services.Maps.MapRouteFinder
![Page 24: 12 maps, geolocation, and geofencing](https://reader035.vdocument.in/reader035/viewer/2022062405/5560b628d8b42af43b8b49e8/html5/thumbnails/24.jpg)
• Lat, long (geopoint) to postal address
MapLocationFinderResult result2 = await MapLocationFinder.FindLocationsAtAsync(point);
• Postal address and hint to lat, longMapLocationFinderResult result3 = await MapLocationFinder.FindLocationsAsync("1 Microsoft way Redmond, WA", center);
• This is not a local/places search!
Windows.Services.Maps.MapLocationFinder
![Page 25: 12 maps, geolocation, and geofencing](https://reader035.vdocument.in/reader035/viewer/2022062405/5560b628d8b42af43b8b49e8/html5/thumbnails/25.jpg)
Geofencing WinRT APIs
![Page 26: 12 maps, geolocation, and geofencing](https://reader035.vdocument.in/reader035/viewer/2022062405/5560b628d8b42af43b8b49e8/html5/thumbnails/26.jpg)
Enables contextual or automatic experiences• Used to start actions based on location
based notifications• Delight the user with relevant proposals, reminders,
data…• Get things done behalf of the user automatically
Multitasking: geofence notifications can be received in the background by a background task
What is a geofence and why use it
Geofence:virtual perimeter around a place of interest to an app/user
![Page 27: 12 maps, geolocation, and geofencing](https://reader035.vdocument.in/reader035/viewer/2022062405/5560b628d8b42af43b8b49e8/html5/thumbnails/27.jpg)
• Namespace: Windows.Devices.Geolocation.Geofencing
• Requires location capability• Supported in Windows 8.x and
Windows Phone 8.1 devices• Support in foreground and
background mode
Geofencing API recapParity with Windows 8.1
Supported in: Windows Store apps
Windows Phone Store apps (Silverlight 8.1, Runtime)
![Page 28: 12 maps, geolocation, and geofencing](https://reader035.vdocument.in/reader035/viewer/2022062405/5560b628d8b42af43b8b49e8/html5/thumbnails/28.jpg)
1. App creates fences and registers for events or background task notification
2. System tracks geofences for all apps
3. App/background task is notified when fence conditions are satisfied
4. App/background task reads notification details and performs custom actions
High-level architectureLocation serviceGeofences storageAppAppAppCreate fencesTrigger task (BG)Fence notification (FG)Background coreRead notification infoHardware based trackingGeofence tracking for all appsGeofenceMonitorOptimized, adaptive software tracking
![Page 29: 12 maps, geolocation, and geofencing](https://reader035.vdocument.in/reader035/viewer/2022062405/5560b628d8b42af43b8b49e8/html5/thumbnails/29.jpg)
Geofences have intuitive, easy-to-use properties.
Circular geofences are supported in this release.
A geofence consists of
Property Required/optional Values Default
Id Required String N/A
Geoshape RequiredGeocircle (BasicGeoposition + Radius)
N/A
MonitoredStates Optional Entered/Exited/Removed Entered/Exited
SingleUse Optional True/False False
DwellTime Optional Timespan 10s
StartTime Optional DateTimeOffset 0/epoch
Duration Optional Timespan 0/forever
![Page 30: 12 maps, geolocation, and geofencing](https://reader035.vdocument.in/reader035/viewer/2022062405/5560b628d8b42af43b8b49e8/html5/thumbnails/30.jpg)
• Lock screen pinning not needed for background mode
• Background task execution throttled by 2 minutes
• Geofence background task can use limited memory, run for limited time
• Coalesce enter/exit events• Background task can do toasts, sound, etc.
Notification center available
Geofencing differences with Windows
![Page 31: 12 maps, geolocation, and geofencing](https://reader035.vdocument.in/reader035/viewer/2022062405/5560b628d8b42af43b8b49e8/html5/thumbnails/31.jpg)
• Recommended radius of 50 meters or more• Recommended no more than 1000 fences per app• Limited geofences per app (about 20,000)• Hysteresis logic: enter/exit detection is asymmetric• Do not use foreground mode or background mode at
same time• Limited resources for background tasks: light
operations• Always check the time at which the event occurred, in
case there were delays launching the background task
Geofencing tips and tricks
![Page 32: 12 maps, geolocation, and geofencing](https://reader035.vdocument.in/reader035/viewer/2022062405/5560b628d8b42af43b8b49e8/html5/thumbnails/32.jpg)
Geofencing
demo
![Page 33: 12 maps, geolocation, and geofencing](https://reader035.vdocument.in/reader035/viewer/2022062405/5560b628d8b42af43b8b49e8/html5/thumbnails/33.jpg)
Mapping in Universal apps
![Page 34: 12 maps, geolocation, and geofencing](https://reader035.vdocument.in/reader035/viewer/2022062405/5560b628d8b42af43b8b49e8/html5/thumbnails/34.jpg)
Mapping convergence
To cut a long story short: not there yet
Bing Maps vs new Windows Runtime Maps control
Location vs GeopointGeoposition (for tracking)
Events on map (phone) vs events on shapes (Win8)
Significant delta in advanced capabilities and simulation
![Page 35: 12 maps, geolocation, and geofencing](https://reader035.vdocument.in/reader035/viewer/2022062405/5560b628d8b42af43b8b49e8/html5/thumbnails/35.jpg)
Is there mapping conversion at all?
Geolocation 100%Geofencing 100%Geopoint
Processing geodata in shared part of PCL
![Page 36: 12 maps, geolocation, and geofencing](https://reader035.vdocument.in/reader035/viewer/2022062405/5560b628d8b42af43b8b49e8/html5/thumbnails/36.jpg)
©2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, Office, Azure, System Center, Dynamics and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.