i'm trying to creat an app that tracks my current location. Here is my code:
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;
});
}
This function will be called when my current location changes. It works quite good but this problem. Because i remove previous pin then add a new one so when i see it on the map, there is a delay between each 2 pins. I tried with MapIcon but it didn't work, too. I tried not to remove pin and updated its location only, but there would be a new pin, next to the old one. Thank you.
You shouldn't have to remove-recreate-add the overlay each time the location changes. Updating the existing overlay's location should do the trick (although it sounds like you tried this and it didn't work?)
I haven't tested this, but I adapted your code to update the existing overlay's position rather than recreating it.
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;
});
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.