简体   繁体   中英

Tracking location in realtime in Windows phone 8.1

Sorry if this is a dumb question, I'm a beginner to windows phone 8.1 development. I am using MapControl to display my current location on the map but as I move my position does not get updated automatically in realtime unless I click on a button and re-initialize the position of the pushpin which was earlier created. Is there a better way where this happens in the without the user having to push the button everytime he wants to see his current location.

private async Task setMyLocation() 
        {
            try
            {
                var gl = new Geolocator() { DesiredAccuracy = PositionAccuracy.High };
                Geoposition location = await gl.GetGeopositionAsync(TimeSpan.FromMinutes(5), TimeSpan.FromSeconds(5));
                var pin = new MapIcon()
                {
                    Location = location.Coordinate.Point,
                    Title = "You are here",
                    NormalizedAnchorPoint = new Point() { X = 0, Y = 0 },
                };
                myMapView.MapElements.Add(pin);
                await myMapView.TrySetViewAsync(location.Coordinate.Point, 20);
            }
            catch
            {
                myMapView.Center = new Geopoint(App.centerPin);
                myMapView.ZoomLevel = 20;
                Debug.WriteLine("GPS NOT FOUND");
            }
            App.centerPin = myMapView.Center.Position;
        }

Thanks in Advance!

Rather than running a timer and polling, handle the Geolocator.PositionChanged event. This will fire each time the position changes and will be significantly more efficient.

You can set the Geolocator.MovementThreshold property so it will only fire if the user has moved a given distance and not do anything if the user stands in the same place. The threshold you pick will probably depend on how far your map is zoomed in.

The way I would do it is to put a Timer that requests information from the server at a given interval.

Take a look onto this answer containing a code snippet : Stackoverflow answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM