Saket Kumar
Saket Kumar

Reputation: 1197

MoveToRegion in xamarin forms maps behaves strangely

I am using a Map control in my app, and i need to set the visible region in such a way that it should cover all the pins. Irony is same code doesn't work on both the platform, iOS works awkwardly , below code yield almost the same visible region in both platform.

 if(Device.OS == TargetPlatform.iOS)
                customMap.MoveToRegion (MapSpan.FromCenterAndRadius (customMap.CustomPins [0].Pin.Position, Distance.FromMiles (0.20)));
if(Device.OS == TargetPlatform.Android)
                customMap.MoveToRegion (MapSpan.FromCenterAndRadius (customMap.CustomPins [0].Pin.Position, Distance.FromMiles (55.0)));

Can anyone explains it? why I need to code like it?

Upvotes: 1

Views: 2199

Answers (2)

Kevin Able
Kevin Able

Reputation: 371

I was running into a problem where the MovetoRegion was being delayed (15-30 seconds) when trying to center on the users current location using the Xamarin Geolocator Plugin, on both IOS and Android. Things work alot better with Saket Kumar's approach with the 500ms delay. Here is my code snippet, hope this helps someone.

    private void CenterOnMe_Clicked(object sender, EventArgs e)
    {
        var locator = CrossGeolocator.Current;
        var t = Task.Run(async () =>
        {
            var position = await locator.GetPositionAsync(TimeSpan.FromSeconds(10));
            Device.StartTimer(TimeSpan.FromMilliseconds(500), () =>
            {
                AroundMeMap.MoveToRegion(

                    MapSpan.FromCenterAndRadius(

                        new Position(position.Latitude, position.Longitude), Distance.FromMiles(1)));
                return false;
            });
        });
    }

Upvotes: 0

Saket Kumar
Saket Kumar

Reputation: 1197

i have found a workaround , i am waiting for some explanation before accepting my own answer for it

Device.StartTimer(TimeSpan.FromMilliseconds(500), () =>
                    {
                        customMap.MoveToRegion(MapSpan.FromCenterAndRadius(customMap.CustomPins [0].Pin.Position, Distance.FromMiles(55.0)));
                        return false;
                    });

Upvotes: 3

Related Questions