36
Matthias Shapiro @matthiasshap Andy Wigley @andy_wigley Maps, Geolocation and Geofencing 30 April 2014 Building Apps for Windows Phone 8.1 Jump Start WinRT Apps & Silverlight

12 maps, geolocation, and geofencing

Embed Size (px)

DESCRIPTION

Building Apps for Windows Phone 8.1 Jump Start . Videos at: http://channel9.msdn.com/Series/Building-Apps-for-Windows-Phone-8-1

Citation preview

Page 1: 12   maps, geolocation, and geofencing

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

In this module…Launching mapping apps

Programming the Map control

Location APIs

Geofencing

Page 3: 12   maps, geolocation, and geofencing

Launching mapping apps

Page 4: 12   maps, geolocation, and geofencing

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

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

Launching mapping apps

demo

Page 7: 12   maps, geolocation, and geofencing

Windows Runtime Apps Map Control

Page 8: 12   maps, geolocation, and geofencing

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

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

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

MapItemsControl

MapControl attached properties• MapControl.Location—geopoint• MapControl.NormalizedAnchorPosition—point (0-1 in x, y)

XAML Children

Page 12: 12   maps, geolocation, and geofencing

There are three types • MapIcons

• Image• Text (optional)

• MapPolygons• MapPolylines

Map elements

Page 13: 12   maps, geolocation, and geofencing

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

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

Drawing onto a Map

demo

Page 16: 12   maps, geolocation, and geofencing

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

Location WinRT APIs

Page 18: 12   maps, geolocation, and geofencing

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

• 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

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

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

Mapping services WinRT APIs

Page 23: 12   maps, geolocation, and geofencing

• 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

• 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

Geofencing WinRT APIs

Page 26: 12   maps, geolocation, and geofencing

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

• 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

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

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

• 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

• 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

Geofencing

demo

Page 33: 12   maps, geolocation, and geofencing

Mapping in Universal apps

Page 34: 12   maps, geolocation, and geofencing

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

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

©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.