Weitere ähnliche Inhalte Ähnlich wie LUMIA APP LABS: HERE MAPS AND LOCATION APIS FOR WINDOWS PHONE (20) Mehr von Microsoft Mobile Developer (20) Kürzlich hochgeladen (20) LUMIA APP LABS: HERE MAPS AND LOCATION APIS FOR WINDOWS PHONE2. 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. LOCATION
API
USAGES
© 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 API
geolocator = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);
geolocator.MovementThreshold = 20; // 20 meters
geolocator.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. WP8
USING THE GEOLOCATOR API
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.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 DIFFERENCE
System.Device.Location.GeoCoordinate: http://bit.ly/Yg9rIb
• Used by GeoCoordinateWatcher
Windows.Devices.Geolocation.Geocoordinate: http://bit.ly/Yg9wvq
• used by Geolocator, as well as Maps API in general
MyCoord = new
System.Device.Location.GeoCoordinate(Position.Coordinate.Latitude,
Position.Coordinate.Longitude);
© 2012 Nokia. All rights reserved. 4/18/2013
© 2012 Microsoft. All rights reserved.
8. WP8
REGISTER 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. WP8
TRACK 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. WP8
DEACTIVATION 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. MAP
API
USAGES
© 2012 Nokia. All rights reserved. 4/18/2013
© 2012 Microsoft. All rights reserved.
12. WP8
MAP 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 APP
xmlns: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. WP8
SUPPORTED FUNCTIONALITY
Add content User services
• Markers (MapOverlay) • Geocoding (convert address to location)
• Polylines, polygons (MapElement) • Reverse geocoding (convert location to address)
• Route • Routing
Change 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
WP8
GeoCoordinateCollection RectLocations = CreateRectangle(new GeoCoordinate(60.35, 24.60), new GeoCoordinate(60.25, 24.80));
//Set the polygon properties
PolyRect.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
WP8
List<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
REVERSEGEOCODING
ReverseGeocodeQuery 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.
20. WP8
NO 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. WP8
NO 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. WP8
HIDING CONTENT
if (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. WP8
MAKING DRAGGABLE MARKERS (1/2)
Add event handled for the touch events:
System.Windows.Input.Touch.FrameReported += Touch_FrameReported;
Add internal boolean variable draggingNow to indicate
dragging status and set it to dragging inside mouse handler
set for the marker. Also disable the map while we do
dragging
void 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. WP8
MAKING DRAGGABLE MARKERS (2/2)
In Touch_FrameReported move the marker with touch
events, 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 IMAGE
private 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. WP8
ALL IN ONE PLACE
Nokia project:
http://projects.developer.nokia.com/WP8MapsExamples
1. Hello Map 12. Routing
2. Map events 13. Advanced Routing
3. Map interaction 14. Location Selector
4. Simple Map content 15. Area Selector
5. More Map content 16. LaunchDirectionsTask
6. Draggable Marker 17. LaunchMapSearchTask
7. Map markers 18. LaunchMapTask
8. Dynamic polyline 19. GetMyGeoposition
9. My Location 20. TrackMyPosition And TrackMyPositionTwo
10. Geo Coding 21. TrackMeInBackground
11. 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.