s14 location and maps.pdf

Upload: edmundo-lozada

Post on 02-Apr-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/27/2019 S14 Location and Maps.pdf

    1/47

    M14: Location and Maps

    Andy Wigley | Microsoft Technical Evangelist

    Rob Tiffany | Microsoft Enterprise Mobility Strategist

  • 7/27/2019 S14 Location and Maps.pdf

    2/47

    Target Agenda | Day 1

    Module and Topic | 10-minute breaks after each session / 60-minute meal break

    1a - Introducing Windows Phone 8 Application Development | Part 1 1b - Introducing Windows Phone 8 Application Development | Part 2

    2 - Designing Windows Phone Apps

    3 - Building Windows Phone Apps

    4 - Files and Storage on Windows Phone 8

    Meal Break | 60-minutes

    5 - Windows Phone 8 Application Lifecycle

    6 - Background Agents

    7 - Tiles and Lock Screen Notifications

    8 - Push Notifications

    9 - Using Phone Resources on Windows Phone 8

  • 7/27/2019 S14 Location and Maps.pdf

    3/47

    Target Agenda | Day 2

    Module and Topic | 10-minute breaks after each session / 60-minute meal break

    10 - App to App Communication 11 - Network Communication on Windows Phone 8

    12 - Proximity Sensors and Bluetooth

    13 - Speech Input on Windows Phone 8

    14 - Maps and Location on Windows Phone 8

    15 - Wallet Support

    16 - In App Purchasing

    Meal Break | 60-minutes

    17 - The Windows Phone Store

    18 - Enterprise Applications in Windows Phone 8: Architecture and Publishing

    19 - Windows 8 and Windows Phone 8 Cross Platform Development

    20 Mobile Web

  • 7/27/2019 S14 Location and Maps.pdf

    4/47

    Introducing the new Location API and the new Maps Controls

    Windows Phone Runtime Location API

    How to Get the Phones Current Location

    How to Continuously Track the Phones Location

    How to Run Location-Tracking Apps in the Background

    Getting Started with the New Map Control

    Specifying Map Center and Zoom

    Animating Map Display Using Map Views

    Module Agenda

  • 7/27/2019 S14 Location and Maps.pdf

    5/4712/4/20Microsoft

    Windows Phone

    Runtime Locatio

  • 7/27/2019 S14 Location and Maps.pdf

    6/47

    Location APIs on Windows Phone 8

    .NET Location API from Windows Phone OS 7.1 is still supported

    System.Device.Location.GeoCoordinateWatcher and related classes

    New Windows Phone Runtime location API

    Accessible from managed and native code

    Improved ability for one-shot location acquisition

    Improved capabilities for location tracking

    Convergent with Windows 8 location API

  • 7/27/2019 S14 Location and Maps.pdf

    7/47

    ID_CAP_LOCATION Capability

    You must include the ID_CAP_LOCATION capability in your app manifest

    If you forget, calls to location APIs throw an UnauthorizedAccessException

  • 7/27/2019 S14 Location and Maps.pdf

    8/47

    Location Sources

    GPS: +Accuracy, -Power, -Spe

    WiFi: +/- Accuracy, +/- Power+/- Urban Areas

    Cell Towers: -Accuracy, +Powe-Wilderness

  • 7/27/2019 S14 Location and Maps.pdf

    9/47

    Controlling the Sources the Geolocation Service Us

    You cant!

    You can set the DesiredAccuracy property of the Geolocator object:

    PositionAccuracy.High if you want the most accurate data available, bu

    of increased battery usage, network bandwidth and possibly monetary ch

    wireless network operators. Often this causes the GPS to be activated

    PositionAccuracy.Default to optimize for power

    You can also set the DesiredAccuracyInMeters property to indicate to the

    service the desired accuracy of any results

    However, the Geolocation service determines the best location data to prov

    the application

  • 7/27/2019 S14 Location and Maps.pdf

    10/47

    How to Get the Phones Current Location

    privateasyncvoid OneShotLocation_Click(object sender, RoutedEventArgs e)

    {

    Geolocator geolocator = newGeolocator();geolocator.DesiredAccuracyInMeters = 50;

    try

    {

    Geoposition geoposition = await geolocator.GetGeopositionAsync(

    maximumAge: TimeSpan.FromMinutes(5),

    timeout: TimeSpan.FromSeconds(10) );

    LatitudeTextBlock.Text = geoposition.Coordinate.Latitude.ToString("0.00LongitudeTextBlock.Text = geoposition.Coordinate.Longitude.ToString("0.0

    }

    catch (UnauthorizedAccessException)

    {

    // the app does not have the right capability or the location master swit

    StatusTextBlock.Text = "location is disabled in phone settings.";

    }

    }

  • 7/27/2019 S14 Location and Maps.pdf

    11/47

    User Consent

    Application certification requirements on user consent still the same as in 7.

    If you are using location data just within your app, you do not need to ask euser consent (although they give consent when they install your app)

    You only have to get user consent if you plan to make location data availabl

    service or other person:

    2.7.4 If your application publishes or makes available location data obtainedLocation Service API to any other service or other person (including adverti

    your application must implement a method to obtain opt-in consent.

  • 7/27/2019 S14 Location and Maps.pdf

    12/47

    Location on Emulator

    Windows Phone Emulator comes with Location simulator

  • 7/27/2019 S14 Location and Maps.pdf

    13/47

    Demo 1

    Get Phone Position

    and Location Emulator

  • 7/27/2019 S14 Location and Maps.pdf

    14/47

    Location Tracking

    If your app only needs the users location at the current time,

    use GetGeopositionAsync as already described Continuously tracking the users location drains the users

    battery more and should only be used for apps that require it

  • 7/27/2019 S14 Location and Maps.pdf

    15/47

    privatevoid TrackLocation_Click(object sender, RoutedEventArgs e)

    {

    if (!tracking) {

    geolocator = newGeolocator();

    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;

    }

    }

    How to Track Location

  • 7/27/2019 S14 Location and Maps.pdf

    16/47

    void geolocator_StatusChanged(Geolocator sender, StatusChangedEventArgs args)

    {

    string status = "";

    switch (args.Status)

    {

    casePositionStatus.Disabled:

    // the application does not have the right capability or the location master

    status = "location is disabled in phone settings";

    break;

    casePositionStatus.Initializing:// the geolocator started the tracking operation

    status = "initializing";

    break;

    casePositionStatus.NoData:

    // the location service was not able to acquire the location

    status = "no data";

    break;

    Geolocator Status

  • 7/27/2019 S14 Location and Maps.pdf

    17/47

    void geolocator_StatusChanged(Geolocator sender, StatusChangedEventArgs args)

    {

    string status = "";

    switch (args.Status)

    {

    casePositionStatus.Disabled:

    // the application does not have the right capability or the location master

    status = "location is disabled in phone settings";

    break;

    casePositionStatus.Initializing:// the geolocator started the tracking operation

    status = "initializing";

    break;

    casePositionStatus.NoData:

    // the location service was not able to acquire the location

    status = "no data";

    break;

    casePositionStatus.Ready:

    Geolocator Status

  • 7/27/2019 S14 Location and Maps.pdf

    18/47

    void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args{

    Dispatcher.BeginInvoke(() =>

    {

    LatitudeTextBlock.Text = args.Position.Coordinate.Latitude.ToString("0.0

    LongitudeTextBlock.Text = args.Position.Coordinate.Longitude.ToString("0

    });

    }

    Position Changed

  • 7/27/2019 S14 Location and Maps.pdf

    19/47

    Demo 2

    Tracking Phone Position

  • 7/27/2019 S14 Location and Maps.pdf

    20/47

    Enable Location Tracking in the Background

    Normally, when your user navigates away from your app, it is made dorman

    activity including location tracking is suspended In Windows Phone 8, a location-tracking app can continue to run in the bac

    the user navigates away, as long as the app continues to actively track locat

    This feature enables scenarios such as an app that provides turn-by-turn dir

    run tracker

  • 7/27/2019 S14 Location and Maps.pdf

    21/47

    Edit WMAppManifest.xml using the XML (Text) Editor

    Replace element as shown

    Enable Background Execution

  • 7/27/2019 S14 Location and Maps.pdf

    22/47

    In App.Xaml, register an event handler for the RunningInBackground event This event is raised when the user navigates away from your background executi

    while you are actively tracking location

    When this event is raised, your app should stop all tasks that are not related to l

    including updates to the apps UI

    Register Event Handler for RunningInBackground E

  • 7/27/2019 S14 Location and Maps.pdf

    23/47

    // Static variables global to application to support tracking

    publicstaticGeolocator Geolocator { get; set; }

    publicstaticbool RunningInBackground { get; set; }

    // Code to execute when the application is activated (brought to foreground)

    privatevoid Application_Activated(object sender, ActivatedEventArgs e)

    {

    RunningInBackground = false;

    }

    // Code to execute when the application is deactivated and is tracking location

    privatevoid Application_RunningInBackground(object sender, RunningInBackgroundEv

    {

    RunningInBackground = true;

    // Suspend all unnecessary processing such as UI updates

    }

    Implement RunningInBackground Event Handler

  • 7/27/2019 S14 Location and Maps.pdf

    24/47

    void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args

    {

    if (!App.RunningInBackground){

    Dispatcher.BeginInvoke(() => {

    LatitudeTextBlock.Text = args.Position.Coordinate.Latitude.ToString(

    LongitudeTextBlock.Text = args.Position.Coordinate.Longitude.ToString

    }

    else

    { // DEMO purposes only: Show toast if running in backgroundMicrosoft.Phone.Shell.ShellToast toast = new Microsoft.Phone.Shell.ShellT

    toast.Content = args.Position.Coordinate.Latitude.ToString("0.00");

    toast.Title = "Location: ";

    toast.NavigationUri = newUri("/Page2.xaml", UriKind.Relative);

    toast.Show();

    }

    }

    Do Not Update UI When Running in the BackgrounExample

  • 7/27/2019 S14 Location and Maps.pdf

    25/47

    Demo 3

    Background Location

    Tracking

  • 7/27/2019 S14 Location and Maps.pdf

    26/47

    Maps

  • 7/27/2019 S14 Location and Maps.pdf

    27/47

    Maps APIs on Windows Phone 8

    Windows Phone 8 has new Map controls, accessible in the

    following namespaces: Microsoft.Phone.Maps

    Microsoft.Phone.Maps.Controls

    Microsoft.Phone.Maps.Services

    The Bing Maps Control is still supported, but is deprecated

    You should only use the Bing Maps Control when you upgradean app from Windows Phone OS 7.1 to Windows Phone 8

    Usage of the new Maps control differs from the Bing Maps control

  • 7/27/2019 S14 Location and Maps.pdf

    28/47

    ID_CAP_MAP Capability

    You must include the ID_CAP_MAP capability in your app manifest

    If you forget, calls to location APIs throw an UnauthorizedAccessException

  • 7/27/2019 S14 Location and Maps.pdf

    29/47

    Add a Map to your UI In XAML:

    In Code:

    privatevoid CreateMap()

    {

    Map MyMap = newMap();

    ContentPanel.Children.Add(MyMap);

    }

    Map Control

  • 7/27/2019 S14 Location and Maps.pdf

    30/47

    By default, map displays at zoom level 1 (world view)and centered at Lat: 0, Long: 0

    Use the Center and ZoomLevel properties to change

    this, in XAML or in code

    //Set the Map center by using Center property

    MyMap.Center = newGeoCoordinate(47.6097, -122.3331);

    //Set the map zoom by using ZoomLevel property

    MyMap.ZoomLevel = 10;

    Center and Zoom Level

  • 7/27/2019 S14 Location and Maps.pdf

    31/47

    MapViews

    It is common to move a map display from one location to another

    A new map view is defined any time the position of the map is changed as a result of

    zooming, rotating, or tilting

    You can use the SetView method to define a map view

    This takes the following parameters:

    Center: A GeoCoordiante object defining the center of the map view

    ZoomLevel: zoom level between 1 and 20

    Heading: specifies the directional heading that is pointing up on the mapin geom

    between 0 and 360

    Pitch: specifies the degree to which the map is tilted as a value between 0 and 180

    BoundingRectangle: a LocationRectangle object that contains the Map control

    AnimationKind: sets the kind of animation you want to see (None, Linear or Parabo

    view changes

  • 7/27/2019 S14 Location and Maps.pdf

    32/47

    Set the cartographic mode of the map with the CartographicMode property

    Cartographic Mode

    Road (default) Aerial Hybrid T

  • 7/27/2019 S14 Location and Maps.pdf

    33/47

    You can display the map in a light or dark color mode by setting the ColorMode prop

    Light and Dark Color Modes

    Light (default) Dark

  • 7/27/2019 S14 Location and Maps.pdf

    34/47

    Pedestrian Features and Landmarks

    You can display additional elements on your map, such as

    landmarks and pedestrian features

    Set the LandmarksEnabled property to true to display

    landmarks

    Set the PedestrianFeaturesEnabled to true to display

    pedestrian features

    Landmarks are visible only when the ZoomLevel is 7 or higher,

    and the Pitch property is 25 or higher

  • 7/27/2019 S14 Location and Maps.pdf

    35/47

    Demo 4

    Maps Control

  • 7/27/2019 S14 Location and Maps.pdf

    36/47

    Unlike the Bing Maps API, the Windows Phone

    Maps API does not have a specific PushPin object

    However, you can create your own PushPins by

    drawing UIElements onto a MapOverlay, then

    add the MapOverlay to a MapLayer which you

    add to the Map

    Pushpins

  • 7/27/2019 S14 Location and Maps.pdf

    37/47

    privateGrid CreatePushpin()

    {

    //Creating a Grid element.Grid MyGrid = newGrid();

    MyGrid.RowDefinitions.Add(newRowDefinition());

    MyGrid.RowDefinitions.Add(newRowDefinition());

    MyGrid.Background = newSolidColorBrush(Colors.Transparent);

    //Creating a Rectangle

    Rectangle MyRectangle = newRectangle();

    MyRectangle.Fill = newSolidColorBrush(Colors.Black);MyRectangle.Height = 20;

    MyRectangle.Width = 20;

    MyRectangle.SetValue(Grid.RowProperty, 0);

    MyRectangle.SetValue(Grid.ColumnProperty, 0);

    //Adding the Rectangle to the Grid

    MyGrid.Children.Add(MyRectangle);

    Creating a Pushpin

    i h i

  • 7/27/2019 S14 Location and Maps.pdf

    38/47

    privateGrid CreatePushpin()

    {

    //Creating a Grid element.

    Grid MyGrid = newGrid();

    MyGrid.RowDefinitions.Add(newRowDefinition());

    MyGrid.RowDefinitions.Add(newRowDefinition());

    MyGrid.Background = newSolidColorBrush(Colors.Transparent);

    //Creating a Rectangle

    Rectangle MyRectangle = newRectangle();

    MyRectangle.Fill = newSolidColorBrush(Colors.Black);

    MyRectangle.Height = 20;

    MyRectangle.Width = 20;

    MyRectangle.SetValue(Grid.RowProperty, 0);

    MyRectangle.SetValue(Grid.ColumnProperty, 0);

    //Adding the Rectangle to the Grid

    MyGrid.Children.Add(MyRectangle);

    Creating a Pushpin

    D i UIEl M L

  • 7/27/2019 S14 Location and Maps.pdf

    39/47

    privatevoid AddMapOverlay()

    {

    Grid MyGrid = CreatePushpin();

    //Creating a MapOverlay and adding the Grid to it.

    MapOverlay MyOverlay = newMapOverlay();

    MyOverlay.Content = MyGrid;

    MyOverlay.GeoCoordinate = newGeoCoordinate(47.6097, -122.3331);

    MyOverlay.PositionOrigin = newPoint(0, 0.5);

    //Creating a MapLayer and adding the MapOverlay to it

    MapLayer MyLayer = newMapLayer();

    MyLayer.Add(MyOverlay);

    MyMap.Layers.Add(MyLayer);

    }

    Drawing a UIElement onto a MapLayer

  • 7/27/2019 S14 Location and Maps.pdf

    40/47

    Demo 5

    Pushpins

  • 7/27/2019 S14 Location and Maps.pdf

    41/47

    New Maps Launchers

    in Windows Phone 8.0

    M T k

  • 7/27/2019 S14 Location and Maps.pdf

    42/47

    MapsTaskMapsTask makes launching the built-in Maps application easy

    MapsTask mapsTask = newMapsTask();

    //Omit the Center property to use the user's currentlocation.mapsTask.Center = newGeoCoordinate(47.6204, -122.3493);

    mapsTask.SearchTerm = "coffee";mapsTask.ZoomLevel = 17;

    mapsTask.Show();

  • 7/27/2019 S14 Location and Maps.pdf

    43/47

    MapDownloaderTask

  • 7/27/2019 S14 Location and Maps.pdf

    44/47

    Use the map downloader task to enable users to download map

    data for offline use

    The task launches the Maps settings application which allows

    the user to select a region of map data to download

    MapDownloaderTask

    MapDownloaderTask mapDownloaderTask = newMapDownloaderTask();

    mapDownloaderTask.Show();

    MapUpdaterTask

  • 7/27/2019 S14 Location and Maps.pdf

    45/47

    Use the map updater task to enable users to update map data

    they have previously downloaded for offline use

    The task launches the Maps settings application which

    immediately checks to see if there are updates available for any

    previously downloaded map data

    MapUpdaterTask

    MapUpdaterTask mapUpdaterTask = newMapUpdaterTask();

    mapUpdaterTask.Show();

    Review

  • 7/27/2019 S14 Location and Maps.pdf

    46/47

    Review

    Windows Phone Runtime location API is new in Windows Phone 8. It has the

    features:

    Accessible from managed and native code

    Greater support for one-shot location acquisition

    Support for Background Location Tracking

    Convergent with Windows 8

    Use the new Maps APIs in Windows Phone 8 to develop maps-based apps,

    incorporate location and search features

    Set Center and Zoom Level

    Animate to new location and zoom using map views

    Select Road, Aerial, Hybrid or Terrain cartographic display modes

    Draw UIElements onto a MapOverlay on top of the Map

  • 7/27/2019 S14 Location and Maps.pdf

    47/47

    The information herein is for informational

    purposes only an 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.

    2012 Microsoft Corporation.

    All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.

    MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION

    IN THIS PRESENTATION.