Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[ios, macos] Fail gracefully on invalid coordinates
Browse files Browse the repository at this point in the history
Invalid coordinates no longer cause an exception to be raised immediately when used in conversion methods and model objects. Instead, the appropriate invalid values are used, consistent with MapKit. Exceptions are still raised when invalid model objects are used with the map.
  • Loading branch information
1ec5 committed Apr 6, 2017
1 parent 11e0c1c commit ea20dd5
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 13 deletions.
26 changes: 15 additions & 11 deletions platform/darwin/src/MGLMapCamera.mm
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,21 @@ + (instancetype)cameraLookingAtCenterCoordinate:(CLLocationCoordinate2D)centerCo
fromEyeCoordinate:(CLLocationCoordinate2D)eyeCoordinate
eyeAltitude:(CLLocationDistance)eyeAltitude
{
mbgl::LatLng centerLatLng = MGLLatLngFromLocationCoordinate2D(centerCoordinate);
mbgl::LatLng eyeLatLng = MGLLatLngFromLocationCoordinate2D(eyeCoordinate);

mbgl::ProjectedMeters centerMeters = mbgl::Projection::projectedMetersForLatLng(centerLatLng);
mbgl::ProjectedMeters eyeMeters = mbgl::Projection::projectedMetersForLatLng(eyeLatLng);
CLLocationDirection heading = std::atan((centerMeters.northing - eyeMeters.northing) /
(centerMeters.easting - eyeMeters.easting));

double groundDistance = std::hypot(centerMeters.northing - eyeMeters.northing,
centerMeters.easting - eyeMeters.easting);
CGFloat pitch = std::atan(eyeAltitude / groundDistance);
CLLocationDirection heading = -1;
CGFloat pitch = -1;
if (CLLocationCoordinate2DIsValid(centerCoordinate) && CLLocationCoordinate2DIsValid(eyeCoordinate)) {
mbgl::LatLng centerLatLng = MGLLatLngFromLocationCoordinate2D(centerCoordinate);
mbgl::LatLng eyeLatLng = MGLLatLngFromLocationCoordinate2D(eyeCoordinate);

mbgl::ProjectedMeters centerMeters = mbgl::Projection::projectedMetersForLatLng(centerLatLng);
mbgl::ProjectedMeters eyeMeters = mbgl::Projection::projectedMetersForLatLng(eyeLatLng);
heading = std::atan((centerMeters.northing - eyeMeters.northing) /
(centerMeters.easting - eyeMeters.easting));

double groundDistance = std::hypot(centerMeters.northing - eyeMeters.northing,
centerMeters.easting - eyeMeters.easting);
pitch = std::atan(eyeAltitude / groundDistance);
}

return [[self alloc] initWithCenterCoordinate:centerCoordinate
altitude:eyeAltitude
Expand Down
4 changes: 4 additions & 0 deletions platform/darwin/src/MGLMultiPoint.mm
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ - (MGLCoordinateBounds)overlayBounds
if (!_bounds) {
mbgl::LatLngBounds bounds = mbgl::LatLngBounds::empty();
for (auto coordinate : _coordinates) {
if (!CLLocationCoordinate2DIsValid(coordinate)) {
bounds = mbgl::LatLngBounds::empty();
break;
}
bounds.extend(MGLLatLngFromLocationCoordinate2D(coordinate));
}
_bounds = bounds;
Expand Down
9 changes: 7 additions & 2 deletions platform/darwin/src/MGLPointCollection.mm
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ - (MGLCoordinateBounds)overlayBounds {
if (!_bounds) {
mbgl::LatLngBounds bounds = mbgl::LatLngBounds::empty();
for (auto coordinate : _coordinates) {
if (!CLLocationCoordinate2DIsValid(coordinate)) {
bounds = mbgl::LatLngBounds::empty();
break;
}
bounds.extend(MGLLatLngFromLocationCoordinate2D(coordinate));
}
_bounds = bounds;
Expand Down Expand Up @@ -119,8 +123,9 @@ - (NSDictionary *)geoJSONDictionary

- (NSString *)description
{
return [NSString stringWithFormat:@"<%@: %p; count = %lu>",
NSStringFromClass([self class]), (void *)self, (unsigned long)[self pointCount]];
return [NSString stringWithFormat:@"<%@: %p; count = %lu; bounds = %@>",
NSStringFromClass([self class]), (void *)self, (unsigned long)[self pointCount],
MGLStringFromCoordinateBounds(self.overlayBounds)];
}

@end
Expand Down
8 changes: 8 additions & 0 deletions platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2843,6 +2843,10 @@ - (CLLocationCoordinate2D)convertPoint:(CGPoint)point toCoordinateFromView:(null

- (CGPoint)convertCoordinate:(CLLocationCoordinate2D)coordinate toPointToView:(nullable UIView *)view
{
if ( ! CLLocationCoordinate2DIsValid(coordinate))
{
return CGPointMake(NAN, NAN);
}
return [self convertLatLng:MGLLatLngFromLocationCoordinate2D(coordinate) toPointToView:view];
}

Expand All @@ -2860,6 +2864,10 @@ - (MGLCoordinateBounds)convertRect:(CGRect)rect toCoordinateBoundsFromView:(null

- (CGRect)convertCoordinateBounds:(MGLCoordinateBounds)bounds toRectToView:(nullable UIView *)view
{
if ( ! CLLocationCoordinate2DIsValid(bounds.sw) || ! CLLocationCoordinate2DIsValid(bounds.ne))
{
return CGRectNull;
}
return [self convertLatLngBounds:MGLLatLngBoundsFromCoordinateBounds(bounds) toRectToView:view];
}

Expand Down
6 changes: 6 additions & 0 deletions platform/macos/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2618,6 +2618,9 @@ - (void)prepareForInterfaceBuilder {
#pragma mark Geometric methods

- (NSPoint)convertCoordinate:(CLLocationCoordinate2D)coordinate toPointToView:(nullable NSView *)view {
if (!CLLocationCoordinate2DIsValid(coordinate)) {
return NSMakePoint(NAN, NAN);
}
return [self convertLatLng:MGLLatLngFromLocationCoordinate2D(coordinate) toPointToView:view];
}

Expand All @@ -2644,6 +2647,9 @@ - (CLLocationCoordinate2D)convertPoint:(NSPoint)point toCoordinateFromView:(null
}

- (NSRect)convertCoordinateBounds:(MGLCoordinateBounds)bounds toRectToView:(nullable NSView *)view {
if (!CLLocationCoordinate2DIsValid(bounds.sw) || !CLLocationCoordinate2DIsValid(bounds.ne)) {
return CGRectNull;
}
return [self convertLatLngBounds:MGLLatLngBoundsFromCoordinateBounds(bounds) toRectToView:view];
}

Expand Down

0 comments on commit ea20dd5

Please sign in to comment.