简体   繁体   English

在Windows Phone 8.1中实时跟踪位置

[英]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. 抱歉,这是一个愚蠢的问题,我是Windows Phone 8.1开发的初学者。 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. 我正在使用MapControl在地图上显示我的当前位置,但是当我移动时,除非单击按钮并重新初始化先前创建的图钉的位置,否则位置不会自动实时更新。 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. 处理Geolocator.PositionChanged事件,而不是运行计时器和轮询。 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. 您可以设置Geolocator.MovementThreshold属性,以便仅当用户移动了给定距离时才会触发,而如果用户站在同一位置则不执行任何操作。 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. 我要做的方法是放置一个Timer,以给定的时间间隔从服务器请求信息。

Take a look onto this answer containing a code snippet : Stackoverflow answer 看一下这个包含代码片段的答案Stackoverflow答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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