summaryrefslogtreecommitdiff
path: root/platform/darwin
diff options
context:
space:
mode:
authorJulian Rex <julian.rex@mapbox.com>2019-10-04 13:51:40 -0400
committerGitHub <noreply@github.com>2019-10-04 13:51:40 -0400
commit33a70100b839793311ecc73c873c063c7cf65e31 (patch)
tree8dd577c3fcbd8a3a7064b8a6ef550d835b45b617 /platform/darwin
parent910e34b85527f9caf074da2274ba4fc5d8c87f94 (diff)
downloadqtlocation-mapboxgl-33a70100b839793311ecc73c873c063c7cf65e31.tar.gz
[ios] Adds pointForCoordinate/coordinateForPoint to MGLMapSnapshotOverlay (#15746)
* [ios] Adds `pointForCoordinate:`/`coordinateForPoint:` to MGLMapSnapshotOverlay * [ios] Adds PR # * [ios, macos] Update file lists & mac contexts * [macos] Fix for image scale in overlay. * [ios] Updated change log based on PR feedback.
Diffstat (limited to 'platform/darwin')
-rw-r--r--platform/darwin/src/MGLMapSnapshotter.h25
-rw-r--r--platform/darwin/src/MGLMapSnapshotter.mm66
-rw-r--r--platform/darwin/src/MGLMapSnapshotter_Private.h14
3 files changed, 95 insertions, 10 deletions
diff --git a/platform/darwin/src/MGLMapSnapshotter.h b/platform/darwin/src/MGLMapSnapshotter.h
index 33febe0d0c..98c8e8a375 100644
--- a/platform/darwin/src/MGLMapSnapshotter.h
+++ b/platform/darwin/src/MGLMapSnapshotter.h
@@ -19,6 +19,31 @@ MGL_EXPORT
*/
@property (nonatomic, readonly) CGContextRef context;
+#if TARGET_OS_IPHONE
+/**
+ Converts the specified map coordinate to a point in the coordinate space of the
+ context.
+ */
+- (CGPoint)pointForCoordinate:(CLLocationCoordinate2D)coordinate;
+
+/**
+ Converts the specified context point to a map coordinate.
+ */
+- (CLLocationCoordinate2D)coordinateForPoint:(CGPoint)point;
+
+#else
+/**
+ Converts the specified map coordinate to a point in the coordinate space of the
+ context.
+ */
+- (NSPoint)pointForCoordinate:(CLLocationCoordinate2D)coordinate;
+
+/**
+ Converts the specified context point to a map coordinate.
+ */
+- (CLLocationCoordinate2D)coordinateForPoint:(NSPoint)point;
+#endif
+
@end
/**
diff --git a/platform/darwin/src/MGLMapSnapshotter.mm b/platform/darwin/src/MGLMapSnapshotter.mm
index 85619a780b..0dde94292c 100644
--- a/platform/darwin/src/MGLMapSnapshotter.mm
+++ b/platform/darwin/src/MGLMapSnapshotter.mm
@@ -17,6 +17,8 @@
#import "MGLAttributionInfo_Private.h"
#import "MGLLoggingConfiguration_Private.h"
#import "MGLRendererConfiguration.h"
+#import "MGLMapSnapshotter_Private.h"
+
#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
#import "MGLMapboxEvents.h"
#endif
@@ -33,23 +35,61 @@ const CGPoint MGLLogoImagePosition = CGPointMake(8, 8);
const CGFloat MGLSnapshotterMinimumPixelSize = 64;
-@interface MGLMapSnapshotOverlay()
-
-- (instancetype)initWithContext:(CGContextRef)context;
+@interface MGLMapSnapshotOverlay() <MGLMapSnapshotProtocol>
+@property (nonatomic, assign) CGFloat scale;
+- (instancetype)initWithContext:(CGContextRef)context scale:(CGFloat)scale pointForFn:(mbgl::MapSnapshotter::PointForFn)pointForFn latLngForFn:(mbgl::MapSnapshotter::LatLngForFn)latLngForFn;
@end
-@implementation MGLMapSnapshotOverlay
+@implementation MGLMapSnapshotOverlay {
+ mbgl::MapSnapshotter::PointForFn _pointForFn;
+ mbgl::MapSnapshotter::LatLngForFn _latLngForFn;
+}
-- (instancetype) initWithContext:(CGContextRef)context {
+- (instancetype) initWithContext:(CGContextRef)context scale:(CGFloat)scale pointForFn:(mbgl::MapSnapshotter::PointForFn)pointForFn latLngForFn:(mbgl::MapSnapshotter::LatLngForFn)latLngForFn {
self = [super init];
if (self) {
_context = context;
+ _scale = scale;
+ _pointForFn = pointForFn;
+ _latLngForFn = latLngForFn;
}
return self;
}
+#if TARGET_OS_IPHONE
+
+- (CGPoint)pointForCoordinate:(CLLocationCoordinate2D)coordinate
+{
+ mbgl::ScreenCoordinate sc = _pointForFn(MGLLatLngFromLocationCoordinate2D(coordinate));
+ return CGPointMake(sc.x, sc.y);
+}
+
+- (CLLocationCoordinate2D)coordinateForPoint:(CGPoint)point
+{
+ mbgl::LatLng latLng = _latLngForFn(mbgl::ScreenCoordinate(point.x, point.y));
+ return MGLLocationCoordinate2DFromLatLng(latLng);
+}
+
+#else
+
+- (NSPoint)pointForCoordinate:(CLLocationCoordinate2D)coordinate
+{
+ mbgl::ScreenCoordinate sc = _pointForFn(MGLLatLngFromLocationCoordinate2D(coordinate));
+ CGFloat height = ((CGFloat)CGBitmapContextGetHeight(self.context))/self.scale;
+ return NSMakePoint(sc.x, height - sc.y);
+}
+
+- (CLLocationCoordinate2D)coordinateForPoint:(NSPoint)point
+{
+ CGFloat height = ((CGFloat)CGBitmapContextGetHeight(self.context))/self.scale;
+ auto screenCoord = mbgl::ScreenCoordinate(point.x, height - point.y);
+ mbgl::LatLng latLng = _latLngForFn(screenCoord);
+ return MGLLocationCoordinate2DFromLatLng(latLng);
+}
+
+#endif
@end
@implementation MGLMapSnapshotOptions
@@ -78,7 +118,7 @@ const CGFloat MGLSnapshotterMinimumPixelSize = 64;
@end
-@interface MGLMapSnapshot()
+@interface MGLMapSnapshot() <MGLMapSnapshotProtocol>
- (instancetype)initWithImage:(nullable MGLImage *)image scale:(CGFloat)scale pointForFn:(mbgl::MapSnapshotter::PointForFn)pointForFn latLngForFn:(mbgl::MapSnapshotter::LatLngForFn)latLngForFn;
@property (nonatomic) CGFloat scale;
@@ -93,8 +133,8 @@ const CGFloat MGLSnapshotterMinimumPixelSize = 64;
{
self = [super init];
if (self) {
- _pointForFn = std::move(pointForFn);
- _latLngForFn = std::move(latLngForFn);
+ _pointForFn = pointForFn;
+ _latLngForFn = latLngForFn;
_scale = scale;
_image = image;
}
@@ -328,7 +368,10 @@ const CGFloat MGLSnapshotterMinimumPixelSize = 64;
CGContextRef currentContext = UIGraphicsGetCurrentContext();
if (currentContext && overlayHandler) {
- MGLMapSnapshotOverlay *snapshotOverlay = [[MGLMapSnapshotOverlay alloc] initWithContext:currentContext];
+ MGLMapSnapshotOverlay *snapshotOverlay = [[MGLMapSnapshotOverlay alloc] initWithContext:currentContext
+ scale:scale
+ pointForFn:pointForFn
+ latLngForFn:latLngForFn];
CGContextSaveGState(snapshotOverlay.context);
overlayHandler(snapshotOverlay);
CGContextRestoreGState(snapshotOverlay.context);
@@ -409,7 +452,10 @@ const CGFloat MGLSnapshotterMinimumPixelSize = 64;
NSGraphicsContext *currentContext = [NSGraphicsContext currentContext];
if (currentContext && overlayHandler) {
- MGLMapSnapshotOverlay *snapshotOverlay = [[MGLMapSnapshotOverlay alloc] initWithContext:currentContext.CGContext];
+ MGLMapSnapshotOverlay *snapshotOverlay = [[MGLMapSnapshotOverlay alloc] initWithContext:currentContext.CGContext
+ scale:scale
+ pointForFn:pointForFn
+ latLngForFn:latLngForFn];
[currentContext saveGraphicsState];
overlayHandler(snapshotOverlay);
[currentContext restoreGraphicsState];
diff --git a/platform/darwin/src/MGLMapSnapshotter_Private.h b/platform/darwin/src/MGLMapSnapshotter_Private.h
new file mode 100644
index 0000000000..205e51a6de
--- /dev/null
+++ b/platform/darwin/src/MGLMapSnapshotter_Private.h
@@ -0,0 +1,14 @@
+#import "MGLMapSnapshotter.h"
+
+@protocol MGLMapSnapshotProtocol <NSObject>
+
+#if TARGET_OS_IPHONE
+- (CGPoint)pointForCoordinate:(CLLocationCoordinate2D)coordinate;
+- (CLLocationCoordinate2D)coordinateForPoint:(CGPoint)point;
+
+#else
+- (NSPoint)pointForCoordinate:(CLLocationCoordinate2D)coordinate;
+- (CLLocationCoordinate2D)coordinateForPoint:(NSPoint)point;
+#endif
+
+@end