diff options
author | Minh Nguyễn <mxn@1ec5.org> | 2015-08-21 16:33:07 -0700 |
---|---|---|
committer | Minh Nguyễn <mxn@1ec5.org> | 2015-08-26 13:17:44 -0700 |
commit | 704702a6e0abb10dd3d4f4db4dda718c43ac1a07 (patch) | |
tree | 2015af14da7a2c83acb9019464be945b8bde7c0a | |
parent | 705d2bf76f3998aace48268b7b8e7f65bb04aad6 (diff) | |
download | qtlocation-mapboxgl-704702a6e0abb10dd3d4f4db4dda718c43ac1a07.tar.gz |
Refined -camera
Now within 1% of MKMapCamera output. -setCamera: still needs work. Also added -[MGLMapCamera description], which avoids the typographical faux pas found in `-[MKMapCamera description], of which I shall not speak.
-rw-r--r-- | ios/app/MBXViewController.mm | 19 | ||||
-rw-r--r-- | platform/ios/MGLMapCamera.mm | 12 | ||||
-rw-r--r-- | platform/ios/MGLMapView.mm | 23 |
3 files changed, 44 insertions, 10 deletions
diff --git a/ios/app/MBXViewController.mm b/ios/app/MBXViewController.mm index 99c207d24d..19422cd9cf 100644 --- a/ios/app/MBXViewController.mm +++ b/ios/app/MBXViewController.mm @@ -138,11 +138,26 @@ mbgl::Settings_NSUserDefaults *settings = nullptr; { if (buttonIndex == actionSheet.firstOtherButtonIndex) { - [self.mapView resetNorth]; +// [self.mapView resetNorth]; +// [self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(29.95, -90.066667) zoomLevel:14 direction:90 animated:YES]; + CLLocationCoordinate2D coordinates[] = { + CLLocationCoordinate2DMake(37, -109+2/60+48/(60*60)), + CLLocationCoordinate2DMake(37, -102.05), + CLLocationCoordinate2DMake(41, -102.05), + CLLocationCoordinate2DMake(41, -109+2/60+48/(60*60)), + }; + [self.mapView setVisibleCoordinates:coordinates count:4 edgePadding:UIEdgeInsetsMake(32, 0, 0, 0) animated:NO]; +// self.mapView.camera = [MGLMapCamera cameraLookingAtCenterCoordinate:CLLocationCoordinate2DMake(29.95, -90.066667) fromDistance:1837212.607578 heading:90]; + // 1982881.568373 + NSLog(@"Camera: %@", self.mapView.camera); + NSLog(@"Altitude: %f", self.mapView.camera.altitude); } else if (buttonIndex == actionSheet.firstOtherButtonIndex + 1) { - [self.mapView resetPosition]; +// [self.mapView resetPosition]; + MGLMapCamera *camera = [self.mapView.camera copy]; + camera.pitch = 45; + [self.mapView setCamera:camera animated:YES]; } else if (buttonIndex == actionSheet.firstOtherButtonIndex + 2) { diff --git a/platform/ios/MGLMapCamera.mm b/platform/ios/MGLMapCamera.mm index 6af747174d..c877e3ebb5 100644 --- a/platform/ios/MGLMapCamera.mm +++ b/platform/ios/MGLMapCamera.mm @@ -23,12 +23,12 @@ mbgl::ProjectedMeters centerMeters = mbgl::Projection::projectedMetersForLatLng(centerLatLng); mbgl::ProjectedMeters eyeMeters = mbgl::Projection::projectedMetersForLatLng(eyeLatLng); - CLLocationDirection heading = std::tan((centerMeters.northing - eyeMeters.northing) / - (centerMeters.easting - eyeMeters.easting)); + CLLocationDirection heading = std::atan((centerMeters.northing - eyeMeters.northing) / + (centerMeters.easting - eyeMeters.easting)); double groundDistance = std::sqrt(std::pow(centerMeters.northing - eyeMeters.northing, 2) + std::pow(centerMeters.easting - eyeMeters.easting, 2)); - CGFloat pitch = std::tan(eyeAltitude / groundDistance); + CGFloat pitch = std::atan(eyeAltitude / groundDistance); return [[self alloc] initWithCenterCoordinate:centerCoordinate altitude:eyeAltitude @@ -92,4 +92,10 @@ heading:_heading]; } +- (NSString *)description +{ + return [NSString stringWithFormat:@"<MKMapCamera %p centerCoordinate:%f, %f altitude:%.0fm heading:%.0f° pitch:%.0f°>", + self, _centerCoordinate.latitude, _centerCoordinate.longitude, _altitude, _heading, _pitch]; +} + @end diff --git a/platform/ios/MGLMapView.mm b/platform/ios/MGLMapView.mm index 055db6ef81..10501d9840 100644 --- a/platform/ios/MGLMapView.mm +++ b/platform/ios/MGLMapView.mm @@ -48,6 +48,7 @@ NSUInteger const MGLStyleVersion = 8; const NSTimeInterval MGLAnimationDuration = 0.3; const CGSize MGLAnnotationUpdateViewportOutset = {150, 150}; const CGFloat MGLMinimumZoom = 3; +const CLLocationDegrees MGLAngularFieldOfView = M_PI / 6; NSString *const MGLAnnotationIDKey = @"MGLAnnotationIDKey"; NSString *const MGLAnnotationSymbolKey = @"MGLAnnotationSymbolKey"; @@ -1756,9 +1757,15 @@ mbgl::LatLngBounds MGLLatLngBoundsFromCoordinateBounds(MGLCoordinateBounds coord - (MGLMapCamera *)camera { - CLLocationDistance altitude = 6.53e7 * exp(-0.6931 * self.zoomLevel); -// landscape: CLLocationDistance altitude = 1.092e7 * exp(-0.6931 * self.zoomLevel); + mbgl::ProjectedMeters neMeters = _mbglMap->projectedMetersForLatLng(self.viewportBounds.ne); + mbgl::ProjectedMeters centerMeters = _mbglMap->projectedMetersForLatLng(_mbglMap->getLatLng()); + CLLocationDistance longitudinalDistance = neMeters.easting - centerMeters.easting; + CLLocationDistance latitudinalDistance = neMeters.northing - centerMeters.northing; + CLLocationDistance distance = (latitudinalDistance + longitudinalDistance) / 2; + CLLocationDistance altitude = distance / std::tan(MGLAngularFieldOfView / 2); + CGFloat pitch = MGLDegreesFromRadians(_mbglMap->getPitch()); + return [MGLMapCamera cameraLookingAtCenterCoordinate:self.centerCoordinate fromDistance:altitude pitch:pitch @@ -1772,9 +1779,15 @@ mbgl::LatLngBounds MGLLatLngBoundsFromCoordinateBounds(MGLCoordinateBounds coord - (void)setCamera:(MGLMapCamera *)camera animated:(BOOL)animated { - mbgl::CameraOptions options; - options.center = MGLLatLngFromLocationCoordinate2D(camera.centerCoordinate); - options.zoom = log(camera.altitude / 6.53e7) / -0.6931; + mbgl::LatLng center = MGLLatLngFromLocationCoordinate2D(camera.centerCoordinate); + mbgl::ProjectedMeters centerMeters = _mbglMap->projectedMetersForLatLng(center); + CLLocationDistance distance = camera.altitude * std::tan(MGLAngularFieldOfView / 2); + mbgl::LatLngBounds bounds = { + { centerMeters.northing - distance, centerMeters.easting - distance }, + { centerMeters.northing + distance, centerMeters.easting + distance }, + }; + mbgl::CameraOptions options = _mbglMap->cameraForLatLngBounds(bounds, {}); + options.angle = MGLRadiansFromDegrees(-camera.heading); options.pitch = MGLRadiansFromDegrees(camera.pitch); if (animated) |