summaryrefslogtreecommitdiff
path: root/platform
diff options
context:
space:
mode:
authorJustin R. Miller <incanus@users.noreply.github.com>2015-02-16 10:19:43 -0800
committerJustin R. Miller <incanus@users.noreply.github.com>2015-02-16 10:19:43 -0800
commit1444e6f2208aaced4ceebbe184d8aa42b848c36c (patch)
treea70192b174ea6dc69b1fa0b13f9a33358a9110e5 /platform
parentea31ad99a56f1e0afbcc7862ddc62dffd16bc5bb (diff)
parent6e41664cb033ee5edf6ae5ac66ed518d9f0d1f89 (diff)
downloadqtlocation-mapboxgl-1444e6f2208aaced4ceebbe184d8aa42b848c36c.tar.gz
Merge pull request #886 from mapbox/conversions-squash
fixes #476 & #853: pixel/meter/latlng conversion routines in core & iOS
Diffstat (limited to 'platform')
-rw-r--r--platform/ios/MGLMapView.mm91
1 files changed, 60 insertions, 31 deletions
diff --git a/platform/ios/MGLMapView.mm b/platform/ios/MGLMapView.mm
index 7c1f9eb657..a83fcdb93b 100644
--- a/platform/ios/MGLMapView.mm
+++ b/platform/ios/MGLMapView.mm
@@ -12,6 +12,7 @@
#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/storage/default/sqlite_cache.hpp>
#include <mbgl/storage/network_status.hpp>
+#include <mbgl/util/geo.hpp>
#import "MGLTypes.h"
#import "MGLStyleFunctionValue.h"
@@ -92,7 +93,7 @@ NSTimeInterval const MGLAnimationDuration = 0.3;
@implementation MGLMapView
-#pragma mark - Setup -
+#pragma mark - Setup & Teardown -
@dynamic debugActive;
@@ -323,7 +324,7 @@ mbgl::DefaultFileSource *mbglFileSource = nullptr;
// set initial position
//
- mbglMap->setLonLatZoom(0, 0, mbglMap->getMinZoom());
+ mbglMap->setLatLngZoom(mbgl::LatLng(0, 0), mbglMap->getMinZoom());
// setup change delegate queue
//
@@ -498,18 +499,6 @@ mbgl::DefaultFileSource *mbglFileSource = nullptr;
[super layoutSubviews];
}
-#pragma mark - Conversions -
-
-+ (CGFloat)degreesToRadians:(CGFloat)degrees
-{
- return degrees * M_PI / 180;
-}
-
-+ (CGFloat)radiansToDegrees:(CGFloat)radians
-{
- return radians * 180 / M_PI;
-}
-
#pragma mark - Life Cycle -
#pragma clang diagnostic push
@@ -529,6 +518,17 @@ mbgl::DefaultFileSource *mbglFileSource = nullptr;
mbglMap->start();
}
+- (void)tintColorDidChange
+{
+ for (UIView *subview in self.subviews)
+ {
+ if ([subview respondsToSelector:@selector(setTintColor:)])
+ {
+ subview.tintColor = self.tintColor;
+ }
+ }
+}
+
#pragma mark - Gestures -
- (void)handleCompassTapGesture:(id)sender
@@ -755,18 +755,7 @@ mbgl::DefaultFileSource *mbglFileSource = nullptr;
return ([validSimultaneousGestures containsObject:gestureRecognizer] && [validSimultaneousGestures containsObject:otherGestureRecognizer]);
}
-#pragma mark - Settings -
-
-- (void)tintColorDidChange
-{
- for (UIView *subview in self.subviews)
- {
- if ([subview respondsToSelector:@selector(setTintColor:)])
- {
- subview.tintColor = self.tintColor;
- }
- }
-}
+#pragma mark - Properties -
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-parameter"
@@ -827,11 +816,13 @@ mbgl::DefaultFileSource *mbglFileSource = nullptr;
mbglMap->toggleDebug();
}
+#pragma mark - Geography -
+
- (void)setCenterCoordinate:(CLLocationCoordinate2D)coordinate animated:(BOOL)animated
{
CGFloat duration = (animated ? MGLAnimationDuration : 0);
- mbglMap->setLonLat(coordinate.longitude, coordinate.latitude, secondsAsDuration(duration));
+ mbglMap->setLatLng(mbgl::LatLng(coordinate.latitude, coordinate.longitude), secondsAsDuration(duration));
}
- (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate
@@ -841,17 +832,16 @@ mbgl::DefaultFileSource *mbglFileSource = nullptr;
- (CLLocationCoordinate2D)centerCoordinate
{
- double lon, lat;
- mbglMap->getLonLat(lon, lat);
+ mbgl::LatLng latLng = mbglMap->getLatLng();
- return CLLocationCoordinate2DMake(lat, lon);
+ return CLLocationCoordinate2DMake(latLng.latitude, latLng.longitude);
}
- (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(double)zoomLevel animated:(BOOL)animated
{
CGFloat duration = (animated ? MGLAnimationDuration : 0);
- mbglMap->setLonLatZoom(centerCoordinate.longitude, centerCoordinate.latitude, zoomLevel, secondsAsDuration(duration));
+ mbglMap->setLatLngZoom(mbgl::LatLng(centerCoordinate.latitude, centerCoordinate.longitude), zoomLevel, secondsAsDuration(duration));
[self unrotateIfNeededAnimated:animated];
}
@@ -899,6 +889,35 @@ mbgl::DefaultFileSource *mbglFileSource = nullptr;
[self setDirection:direction animated:NO];
}
+- (CLLocationCoordinate2D)convertPoint:(CGPoint)point toCoordinateFromView:(UIView *)view
+{
+ CGPoint convertedPoint = [self convertPoint:point fromView:view];
+
+ // flip y coordinate for iOS view origin top left
+ //
+ convertedPoint.y = self.bounds.size.height - convertedPoint.y;
+
+ mbgl::LatLng latLng = mbglMap->latLngForPixel(mbgl::vec2<double>(convertedPoint.x, convertedPoint.y));
+
+ return CLLocationCoordinate2DMake(latLng.latitude, latLng.longitude);
+}
+
+- (CGPoint)convertCoordinate:(CLLocationCoordinate2D)coordinate toPointToView:(UIView *)view
+{
+ mbgl::vec2<double> pixel = mbglMap->pixelForLatLng(mbgl::LatLng(coordinate.latitude, coordinate.longitude));
+
+ // flip y coordinate for iOS view origin in top left
+ //
+ pixel.y = self.bounds.size.height - pixel.y;
+
+ return [self convertPoint:CGPointMake(pixel.x, pixel.y) toView:view];
+}
+
+- (CLLocationDistance)metersPerPixelAtLatitude:(CLLocationDegrees)latitude
+{
+ return mbglMap->getMetersPerPixelAtLatitude(latitude, self.zoomLevel);
+}
+
#pragma mark - Styling -
- (NSDictionary *)getRawStyle
@@ -1337,6 +1356,16 @@ mbgl::DefaultFileSource *mbglFileSource = nullptr;
#pragma mark - Utility -
++ (CGFloat)degreesToRadians:(CGFloat)degrees
+{
+ return degrees * M_PI / 180;
+}
+
++ (CGFloat)radiansToDegrees:(CGFloat)radians
+{
+ return radians * 180 / M_PI;
+}
+
- (void)animateWithDelay:(NSTimeInterval)delay animations:(void (^)(void))animations
{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), animations);