We have seen how to change map modes, however just a simple map without any focus does not make much sense, does it? So we move further and modify the HelloMaps
example by using the Windows Phone Location Service to add a pushpin to the map — signifying the user's detected location.
The Pushpin
class is part of the Microsoft.Phone.Controls.Maps
namespace, with location as one of the properties. So let's use this property in our code and render a simple pushpin:
HelloMaps-Pushpin
. HelloMaps
example, reintegrate the same UI, maps, and text labels, and from our Hello Location
example, import the TextBlock
named statusText
. Your UI should now look like the following screenshot: MainPage.xaml.cs
and copy over the location variable and methods from the Hello Location
example. Add a variable in the global scope called zoomLevel
that controls your map's zoom level.GeoCoordinateWatcher locationManager; double zoomLevel = 10.00;
MainPage()
constructor copy over the location manager code from the Hello Location example, and for your map, set ZoomBarVisibility
to Visible
. This property is from the Map
class that allows the zoom in/zoom out control to be visible on the map. The following is your constructor code:public MainPage() { InitializeComponent(); locationManager = new GeoCoordinateWatcher(GeoPositionAccuracy.High); locationManager.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs> (locationManager_getStatus); locationManager.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>> (locationManager_getPosition); myMap.ZoomBarVisibility = Visibility.Visible; locationManager.Start(); }
StatusChanged
event handler locationManager_getStatus
remains similar to the Hello Location
example, however the PositionChanged
event handler undergoes a few changes by adding a new pushpin at the detected location. PositionChanged
event handler function by creating a new pushpin named myPin
. The Location
property of the Pushpin
class is then used to transfer the hardware's detected location to the pushpin by the following lines of code:void locationManager_getPosition(object sender, GeoPositionChangedEventArgs<GeoCoordinate> newLoc) { Pushpin myPin = new Pushpin(); myPin.Location = new GeoCoordinate(newLoc.Position.Location.Latitude, newLoc.Position.Location.Longitude);
Children
collection of the Map
class as:myMap.SetView(new GeoCoordinate(newLoc.Position.Location.Latitude,newLoc.Position.Location.Longitude), zoomLevel);
You can find this example project in the code files for the book underChapter 3, titled HelloMaps-Pushpin
.
3.143.17.127