amay077 / Xamarin.Forms.GoogleMaps

Map library for Xamarin.Forms using Google maps API
https://www.nuget.org/packages/Xamarin.Forms.GoogleMaps/
MIT License
546 stars 349 forks source link

MoveToRegion fails when longitude > 90 or < -90 on iOS #559

Closed uitmaurik closed 6 years ago

uitmaurik commented 6 years ago

VERSIONS

PLATFORMS

ACTUAL BEHAVIOR

When calling MoveToRegion with a longitude > 90 or < -90, the region is not set on iOS.

The latitude is valid when the value is between -90 and 90 and the longitude is valid when the value is between -180 and 180. Using a position somewhere in the pacific ocean will fail on iOS.

EXPECTED BEHAVIOR

For every possible Position, a call to MoveToRegion must set the appropriate region on the map.

HOW TO REPRODUCE

The following calls will succeed:

MoveToRegion(MapSpan.FromCenterAndRadius(new Position(0, 90), Distance.FromKilometers(10000)), false);
MoveToRegion(MapSpan.FromCenterAndRadius(new Position(0, -90), Distance.FromKilometers(10000)), false);

The following calls will fail:

MoveToRegion(MapSpan.FromCenterAndRadius(new Position(0, 91), Distance.FromKilometers(10000)), false);
MoveToRegion(MapSpan.FromCenterAndRadius(new Position(0, -91), Distance.FromKilometers(10000)), false);
uitmaurik commented 6 years ago

Digging into the issue, I came at the point where CameraLogic.MoveToRegion is creating a CoordinateBounds object. When the center.Latitude > 90 or center.Latitude < 90, the created CoordinateBounds object is not valid. I see no way to work around this when using the constructor with two CLLocationCoordinate2D objects as this constructor does switch the positions itself. The only work around is to use the constructor that uses the VisibleRegion object:

            CoordinateBounds mapRegion = new CoordinateBounds(new VisibleRegion(
               center.Latitude + halfLat,
               center.Longitude + halfLong + (center.Longitude + halfLong > 180 ? -360 : 0),
               center.Latitude + halfLat,
               center.Longitude - halfLong + (center.Longitude - halfLong < -180 ? 360 : 0),
               center.Latitude - halfLat,
               center.Longitude + halfLong + (center.Longitude + halfLong > 180 ? -360 : 0),
               center.Latitude - halfLat,
               center.Longitude - halfLong + (center.Longitude - halfLong < -180 ? 360 : 0)
               ));
amay077 commented 6 years ago

Thanks for your PR! I merged and released new lib. 🎉

https://www.nuget.org/packages/Xamarin.Forms.GoogleMaps/3.0.4