none
C# Bing Maps Windows Store Apps. Update GPS location?

    Question

  • Hi,

    Bing Maps app in Windows Store, GPS location is realtime, location pin moves when you move. I wonder how it's done? I haven't been able to find anything from the API of how to update the location continuously, with this it updates only every 1-2 minutes.

    public sealed partial class MainPage : Page
        {
            Geolocator geolocator;
            LocationIcon locationIcon;
    
            public MainPage()
            {
                this.InitializeComponent();
                geolocator = new Geolocator();
                
                // Add the location icon to map layer so that we can position it.
                locationIcon = new LocationIcon(MyMap)
    
                {
    
                    Visibility = Windows.UI.Xaml.Visibility.Collapsed
    
                };
    
                MyMap.Children.Add(locationIcon);
                geolocator.PositionChanged += new Windows.Foundation.TypedEventHandler<Geolocator, PositionChangedEventArgs>(geolocator_PositionChanged);
            }
    
            private void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
            {
                // Need to set map view on UI thread.
                this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, new DispatchedHandler(
                    () =>
                    {
                        //Get the current location
                        
                        Location location = new Location(args.Position.Coordinate.Latitude, args.Position.Coordinate.Longitude);
    
    
                        //Update the position of the GPS pushpin
                        
                            MapLayer.SetPosition(locationIcon, location);
                        
    
    
    
                        //Set the radius of the Accuracy Circle
    
                        locationIcon.SetRadius(args.Position.Coordinate.Accuracy);
    
    
    
                        //Make GPS pushpin visible
    
                        locationIcon.Visibility = Windows.UI.Xaml.Visibility.Visible;
    
    
    
                        //Update the map view to the current GPS location
    
                        MyMap.SetView(location, 17);
                    }));
            }

    Thursday, June 27, 2013 8:19 AM

Answers

All replies

  • You are on the right track. To make it update faster you have to set the report interval on the Geolocator. You can also set the movement threshold which is the minimum distance you must move before the position changed event is fired. Here is the documentation: http://msdn.microsoft.com/en-us/library/windows/apps/windows.devices.geolocation.geolocator.aspx

    Note that by lowering the report interval time and the movement threshold you will find that your app uses a lot more battery power.


    http://rbrundritt.wordpress.com


    Thursday, June 27, 2013 9:56 AM
    Owner
  • Dunno what i'm doing wrong, can't get it to work on either report interval or movement treshold. Could you post a sample how to use it in the above sample code? I think I have some simple error or forgot something.
    Thursday, June 27, 2013 7:59 PM
  • This is how I tried it, but not sure whats wrong with setting the property.

    public sealed partial class MainPage : Page
        {
            Geolocator geolocator;
            LocationIcon locationIcon;
    
            public MainPage()
            {
                this.InitializeComponent();
                geolocator = new Geolocator() { ReportInterval = 2000 };
                
                // Add the location icon to map layer so that we can position it.
                locationIcon = new LocationIcon(MyMap)
    
                {
    
                    Visibility = Windows.UI.Xaml.Visibility.Collapsed
    
                };
    
                MyMap.Children.Add(locationIcon);
                geolocator.PositionChanged += new Windows.Foundation.TypedEventHandler<Geolocator, PositionChangedEventArgs>(geolocator_PositionChanged);
            }
    
            private void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
            {
                // Need to set map view on UI thread.
                this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, new DispatchedHandler(
                    () =>
                    {
                        //Get the current location
                        
                        Location location = new Location(args.Position.Coordinate.Latitude, args.Position.Coordinate.Longitude);
    
    
                        //Update the position of the GPS pushpin
                        
                            MapLayer.SetPosition(locationIcon, location);
                        
    
    
    
                        //Set the radius of the Accuracy Circle
    
                        locationIcon.SetRadius(args.Position.Coordinate.Accuracy);
    
    
    
                        //Make GPS pushpin visible
    
                        locationIcon.Visibility = Windows.UI.Xaml.Visibility.Visible;
    
    
    
                        //Update the map view to the current GPS location
    
                        MyMap.SetView(location, 17);
                    }));
            }
    
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
            }
    
            public uint DesiredAccuracyInMeters
            {
                get { return geolocator.ReportInterval; }
                set { geolocator.ReportInterval = value; }
            }    
        }
    }

    Friday, June 28, 2013 7:07 AM
  • Try smaller values for the ReportInterval and Movement Threshold.

    http://rbrundritt.wordpress.com

    Friday, June 28, 2013 9:09 AM
    Owner
  • I tried but no luck. 2000 for ReportInterval should be 2 seconds as it's in milliseconds, or am I wrong?
    Friday, June 28, 2013 3:00 PM
  • I tested with simple app sample, it updates TextBlocks when it gets new location data. Same thing, if I add ReportInterval into it, location data gets updated about once in a minute.

    public sealed partial class MainPage : Page
        {
            private Geolocator location;
    
            public MainPage()
            {
                this.InitializeComponent();
            }
    
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                location = new Geolocator() { ReportInterval = 2 };
                location.PositionChanged += location_PositionChanged;
            }
    
            protected override void OnNavigatedFrom(NavigationEventArgs e)
            {
                base.OnNavigatedFrom(e);
                location.PositionChanged -= location_PositionChanged;
            }
    
            async void location_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
            {
                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    Geoposition position = args.Position;
                    LatitudeTB.Text = position.Coordinate.Latitude.ToString();
                    LongitudeTB.Text = position.Coordinate.Longitude.ToString();
                    AccuracyTB.Text = position.Coordinate.Accuracy.ToString() + " meter(s)";
                    TimestampTB.Text = position.Coordinate.Timestamp.ToString();
                    AltitudeTB.Text = position.Coordinate.Altitude.ToString() + " meter(s)";
                });
            }
    
            public uint DesiredInterval
            {
                get { return location.ReportInterval; }
                set { location.ReportInterval = value; }
            }
    
            /*public double DesiredThreshold
            {
                get { return location.MovementThreshold; }
                set { location.MovementThreshold = value; }
            }*/
        }

    Friday, June 28, 2013 3:25 PM
  • I have no idea what the problem is. GPS is working fine, if I launch Bing Maps app and walk around the house with the laptop, my location gets updated in the map while I walk. But when I try to use these properties in any sample, they don't seem to have any effect on the location update speed.

    Only thing i can think of is that the property is not set as it should.

    Saturday, June 29, 2013 7:03 AM
  • ReportInterval and MovementThreshold didn't have any visible effect without this:

    geolocator.DesiredAccuracy = PositionAccuracy.High;

    • Marked as answer by Joni_78 Wednesday, July 03, 2013 12:20 PM
    Wednesday, July 03, 2013 12:20 PM