用户移动地图时更新Xamarin表单地图上的图钉位置

时间:2018-09-28 01:40:34

标签: xamarin.forms

我正在尝试在用户移动地图时更新图钉位置,以使其始终位于屏幕中央,但图钉不会刷新。(类似于Uber应用) 如果删除并添加它,则可以正常工作,但按预期会出现Pin闪烁的情况。 您能建议一下如何修复和设置地图的默认位置吗?

public partial class LocationPage : ContentPage
{

    public event EventHandler LocationSet;

    private Location location;

    public LocationPage(Location location = null)
    {

        InitializeComponent();

        BindingContext = this;

        this.location = location;


        map.PropertyChanged += (sender, e) => {
            if(e.PropertyName == "VisibleRegion" && map.VisibleRegion != null)
            {
                Device.BeginInvokeOnMainThread(() =>
                {
                    if (map.Pins.Count > 0)
                    {
                        map.Pins[0].Position = map.VisibleRegion.Center;
                        //Pin pin = map.Pins[0];
                        //map.Pins.RemoveAt(0);
                        //map.Pins.Add(pin);
                    }
                    else
                    {
                        var pin = new Pin
                        {
                            Type = PinType.Place,
                            Position = map.VisibleRegion.Center,
                            Label = "Select location"
                        };
                        map.Pins.Add(pin);
                    }
                });
            }
        };
    }

    protected async override void OnAppearing()
    {
        base.OnAppearing();
        if (location != null)
        {
            var pin = new Pin
            {
                Type = PinType.Place,
                Position = new Position(location.Latitude, location.Longitude),
                Label = "Select location"
            };
            map.Pins.Add(pin);

            map.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(location.Latitude, location.Longitude), Distance.FromMiles(1)));
        }
        else
        {
            var location = await GeoLocationHelper.GetCurrentLocation();
            if (location != null)
            {
                map.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(location.Latitude, location.Longitude), Distance.FromMiles(0.3)));
            }
        }

    }

}

0 个答案:

没有答案