Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 16. Bing Maps > Location Tracking

Location Tracking

Location Tracking In Chapter 15, “Geographic Location,” you saw how a GeoCoordinateWatcher provides geographic location updates. In this section you see how the custom IGeoPositionWatcher (presented in that chapter) can be used to track the location of the device using a Bing Maps control. The BingMapsViewModel class constructor instantiates either a GeoCoordinateWatcher or a MockGeoCoordinateWatcher, depending on whether the class is executing within the emulator. See the following excerpt: public BingMapsViewModel(IRouteCalculator routeCalculator)
{
...
    geoPositionWatcher = EnvironmentValues.UsingEmulator
           ? new MockGeoCoordinateWatcher(2000)
           : (IGeoPositionWatcher<GeoCoordinate>)new GeoCoordinateWatcher();

    geoPositionWatcher.PositionChanged
      += delegate(object o, GeoPositionChangedEventArgs<GeoCoordinate> args)
        {
            GeoCoordinate geoCoordinate = args.Position.Location;
            Center = geoCoordinate;

            Message = string.Format("{0} Lat, {1} Long",
                geoCoordinate.Latitude, geoCoordinate.Longitude);
        };

    geoPositionWatcher.StatusChanged
        += delegate(object o, GeoPositionStatusChangedEventArgs args)
        {
            GeoPositionStatus = args.Status;
        };

    trackCommand = new DelegateCommand(obj => Start());
...
} The IGeoPositionWatcher instance is used to monitor the position of the phone. When the watcher’s PositionChanged event is raised, the visible center point of the Bing Maps control is set to the Center property of the viewmodel via a data binding. The viewmodel’s TrackCommand property is data-bound to an AppBarIconButton in the view, and when executed, calls the Start method of the viewmodel. The Start method uses a Boolean tracking field to prevent tracking if it is already underway. Note To pass the Windows Phone Marketplace Certification your app must ask the user whether it is okay to use location services. Additionally, you must provide an option in the settings page of your app to allow the user to disable location services. Before tracking can take place, prompt the user to confirm the use of location services. This is done by using the ViewModelBase class’s IMessageService. If the AskYesNoQuestion method returns true, the geoPositionWatcher is started. See the following excerpt: void Start()
{
    if (tracking)
    {
        return;
    }

    lock (startLock)
    {
        if (tracking)
        {
            return;
        }

        /* Before using geo location, it is a certification requirement
         * that you seek permission from the user first. */
        bool canTrack = MessageService.AskYesNoQuestion(
           "Is it OK to use the geographic location system on your phone?");

        if (!canTrack)
        {
            return;
        }

        tracking = true;
        OnPropertyChanged(() => Tracking);
        geoPositionWatcher.Start();
    }
} For more information regarding the IMessageService see Chapter 2, “Fundamental Concepts in Silverlight Development for Windows Phone.” When the PositionChanged event is raised, the viewmodel’s Center property is set to the event argument’s Position.Location property. This raises the viewmodel’s PropertyChanged event and moves the map view to that location. The BingMapsView.xaml contains a custom AppBar control, shown in the following excerpt, with various AppBarIconButtons, including one that executes the TrackCommand: <u:AppBar BarOpacity="0.7">
    <u:AppBarIconButton
        Command="{Binding TrackCommand}"
        Text="Track"
        IconUri="/BingMaps/Images/ApplicationBarIcons/Route.png" />
...
</u:AppBar> Tapping the Track button causes the watcher to begin raising events. The map center is moved to the location as reported by the GeoPositionChangedEventArgs (see Figure 16.10). Figure 16.10. Use the track button to emulate location changes that are displayed on the map.

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint