Professional Documents
Culture Documents
Andy Wigley | Microsoft Technical Evangelist Rob Tiffany | Microsoft Enterprise Mobility Strategist
50:00
50:00 60:00 50:00 25:00 25:00 30:00
50:00
35:00
35:00 25:00 25:00 60:00 50:00 50:00
50:00
50:00
Module Agenda
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
Microsoft
12/4/20
ID_CAP_LOCATION Capability
You must include the ID_CAP_LOCATION capability in your app manifest
Location Sources GPS: +Accuracy, -Power, -Speed, -Indoors Cell Towers: -Accuracy, +Power, +Speed, -Wilderness WiFi: +/- Accuracy, +/- Power, +/- Speed, +/- Urban Areas
{
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.";
User Consent
Application certification requirements on user consent still the same as in 7.1
If you are using location data just within your app, you do not need to ask explicitly for
user consent (although they give consent when they install your app) You only have to get user consent if you plan to make location data available to any other
Location Service API to any other service or other person (including advertising networks),
your application must implement a method to obtain opt-in consent.
Location on Emulator
Windows Phone Emulator comes with Location simulator
Demo 1
Location Tracking
If your app only needs the users location at the current time,
Geolocator Status
void geolocator_StatusChanged(Geolocator sender, StatusChangedEventArgs args) { string status = ""; switch (args.Status) { case PositionStatus.Disabled: // the application does not have the right capability or the location master switch is off status = "location is disabled in phone settings"; break; case PositionStatus.Initializing: // the geolocator started the tracking operation status = "initializing"; break; case PositionStatus.NoData: // the location service was not able to acquire the location status = "no data"; break;
Geolocator Status
void geolocator_StatusChanged(Geolocator sender, StatusChangedEventArgs args) { string status = ""; switch (args.Status) { case PositionStatus.Disabled: // the application does not have the right capability or the location master switch is off status = "location is disabled in phone settings"; break; case PositionStatus.Initializing: // the geolocator started the tracking operation status = "initializing"; break; case PositionStatus.NoData: // the location service was not able to acquire the location status = "no data"; break; case PositionStatus.Ready:
Position Changed
This feature enables scenarios such as an app that provides turn-by-turn directions or a
run tracker
toast.Show();
} }
Maps
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 upgrade an app from Windows Phone OS 7.1 to Windows Phone 8 Usage of the new Maps control differs from the Bing Maps control
ID_CAP_MAP Capability
You must include the ID_CAP_MAP capability in your app manifest
Map Control
Add a Map to your UI
In XAML:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <maps:Map x:Name="MyMap"/> </Grid>
In Code:
private void CreateMap() { Map MyMap = new Map(); ContentPanel.Children.Add(MyMap); }
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 = new GeoCoordinate(47.6097, -122.3331); //Set the map zoom by using ZoomLevel property MyMap.ZoomLevel = 10;
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 panning, 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 geometric degrees 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 Parabolic) when the view changes
Cartographic Mode
Set the cartographic mode of the map with the CartographicMode property
Road (default)
Aerial
Hybrid
Terrain
Light (default)
Dark
Pushpins
Unlike the Bing Maps API, the Windows Phone Maps API does not have a specific PushPin object
Creating a Pushpin
private Grid CreatePushpin() { //Creating a Grid element. Grid MyGrid = new Grid();
MyGrid.RowDefinitions.Add(new RowDefinition()); MyGrid.RowDefinitions.Add(new RowDefinition()); MyGrid.Background = new SolidColorBrush(Colors.Transparent); //Creating a Rectangle Rectangle MyRectangle = new Rectangle(); MyRectangle.Fill = new SolidColorBrush(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
private Grid CreatePushpin() { //Creating a Grid element. Grid MyGrid = new Grid(); MyGrid.RowDefinitions.Add(new RowDefinition()); MyGrid.RowDefinitions.Add(new RowDefinition());
MyGrid.Background = new SolidColorBrush(Colors.Transparent); //Creating a Rectangle Rectangle MyRectangle = new Rectangle(); MyRectangle.Fill = new SolidColorBrush(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);
{
Grid MyGrid = CreatePushpin(); //Creating a MapOverlay and adding the Grid to it. MapOverlay MyOverlay = new MapOverlay(); MyOverlay.Content = MyGrid; MyOverlay.GeoCoordinate = new GeoCoordinate(47.6097, -122.3331); MyOverlay.PositionOrigin = new Point(0, 0.5);
//Creating a MapLayer and adding the MapOverlay to it MapLayer MyLayer = new MapLayer(); MyLayer.Add(MyOverlay); MyMap.Layers.Add(MyLayer);
}
Demo 5 Pushpins
MapsTask
MapsTask makes launching the built-in Maps application easy
MapsTask mapsTask = new MapsTask(); //Omit the Center property to use the user's current location. mapsTask.Center = new GeoCoordinate(47.6204, -122.3493);
MapsDirectionsTask
Launching built-in Maps tasks with directions enabled is trivial too!
// Get Directions MapsDirectionsTask mapsDirectionsTask = new MapsDirectionsTask(); // // // // You can specify a label and a geocoordinate for the end point. GeoCoordinate spaceNeedleLocation = new GeoCoordinate(47.6204,-122.3493); LabeledMapLocation spaceNdleLML = new LabeledMapLocation("Space Needle", spaceNeedleLocation);
// If you set the geocoordinate parameter to null, the label parameter // is used as a search term. LabeledMapLocation spaceNdleLML = new LabeledMapLocation("Space Needle", null); // If mapsDirectionsTask.Start is not set, the user's current location // is used as start point. mapsDirectionsTask.End = spaceNdleLML; mapsDirectionsTask.Show();
MapDownloaderTask
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 = new MapDownloaderTask(); mapDownloaderTask.Show();
MapUpdaterTask
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 = new MapUpdaterTask(); mapUpdaterTask.Show();
Review
Windows Phone Runtime location API is new in Windows Phone 8. It has the following
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, and 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
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.
MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN 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.