我试图创建一个跟踪我当前位置的应用。这是我的代码:
private Image currentLocationPin;
private async void CurrentLocationChanged(Geolocator sender, PositionChangedEventArgs args)
{
await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
mapControl.Children.Remove(currentLocationPin);
currentLocationPin = new Image() { Source = new BitmapImage(new Uri(smallIconURI)) };
var currentGeo = new Geopoint(new BasicGeoposition
{
Latitude = args.Position.Coordinate.Point.Position.Latitude,
Longitude = args.Position.Coordinate.Point.Position.Longitude
});
MapControl.SetNormalizedAnchorPoint(currentLocationPin, new Point(0.5, 1));
MapControl.SetLocation(currentLocationPin, currentGeo);
mapControl.Children.Add(currentLocationPin);
currentLocationPin.Tapped += ChildObj_Tapped;
mapControl.LandmarksVisible = false;
mapControl.TrafficFlowVisible = false;
mapControl.PedestrianFeaturesVisible = false;
currentGeopoint = currentGeo;
});
}
当我的当前位置发生变化时,将调用此函数。它工作得很好,但这个问题。因为我删除了之前的引脚,然后添加一个新引脚,所以当我在地图上看到它时,每两个引脚之间有一个延迟。我尝试使用MapIcon,但它也没有用。我试图不删除引脚并仅更新其位置,但旧引脚旁边会有一个新引脚。 谢谢。
答案 0 :(得分:0)
每次位置更改时,您都不必删除 - 重新创建 - 重新添加叠加层。更新现有叠加层的位置应该可以解决问题(尽管听起来你尝试过这种方法并且不起作用?)
我没有对此进行测试,但我调整了您的代码以更新现有的叠加层位置而不是重新创建它。
await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
// If needed, create the location pin.
if(currentLocationPin == null)
{
currentLocationPin = new Image() { Source = new BitmapImage(new Uri(smallIconURI)) };
MapControl.SetNormalizedAnchorPoint(currentLocationPin, new Point(0.5, 1));
mapControl.Children.Add(currentLocationPin);
}
// Update the pin's location.
var currentGeo = new Geopoint(new BasicGeoposition
{
Latitude = args.Position.Coordinate.Point.Position.Latitude,
Longitude = args.Position.Coordinate.Point.Position.Longitude
});
MapControl.SetLocation(currentLocationPin, currentGeo);
mapControl.LandmarksVisible = false;
mapControl.TrafficFlowVisible = false;
mapControl.PedestrianFeaturesVisible = false;
currentGeopoint = currentGeo;
});