Using pushpins with maps

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:

  1. We start by creating a new project titled HelloMaps-Pushpin.
  2. From our 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:
    Using pushpins with maps
  3. Open 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;
    
  4. Now in your 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();
    }
    
  5. The 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.
  6. We begin the 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);
    
  7. Now we add this pushpin to the map by using the Children collection of the Map class as:
    myMap.Children.Add(myPin);
    
    
  8. Lastly, we change the map's current view to the detected location to focus the map at that geocoordinate and specify a zoom level.
    myMap.SetView(new GeoCoordinate(newLoc.Position.Location.Latitude,newLoc.Position.Location.Longitude), zoomLevel);
    

    Note

    Don't forget to import the following Location and Maps namespaces in your C# code:

    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Controls.Maps;
    using System.Device.Location;
    
  9. Running the application in the emulator produces the following result:
    Using pushpins with maps
  10. Now open up the Location simulator and select a few locations around Paris. Do it one at a time and see the real-time pushpin magic!!
    Using pushpins with maps

You can find this example project in the code files for the book underChapter 3, titled HelloMaps-Pushpin.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.17.127