forked from mapbox/mapbox-gl-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement map projection functionality (mapbox#254)
* Implement map projection functionality * Add missing MGL_EXPORT * Remove the automatic copyright header
- Loading branch information
1 parent
ea234ed
commit 6db27f5
Showing
15 changed files
with
397 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include <mbgl/map/camera.hpp> | ||
#include <mbgl/map/map.hpp> | ||
#include <mbgl/util/noncopyable.hpp> | ||
#include <mbgl/util/geo.hpp> | ||
|
||
#include <memory> | ||
|
||
namespace mbgl { | ||
|
||
class Transform; | ||
|
||
class MapProjection : private util::noncopyable { | ||
public: | ||
explicit MapProjection(const Map&); | ||
~MapProjection(); | ||
|
||
ScreenCoordinate pixelForLatLng(const LatLng&) const; | ||
LatLng latLngForPixel(const ScreenCoordinate&) const; | ||
|
||
void setCamera(const CameraOptions&); | ||
CameraOptions getCamera() const; | ||
|
||
/// Set the underneath camera so the requested coordinates are visible with the inset. | ||
void setVisibleCoordinates(const std::vector<LatLng>&, const EdgeInsets&); | ||
|
||
private: | ||
std::unique_ptr<Transform> transform; | ||
}; | ||
|
||
} // namespace mbgl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#import <Mapbox/Mapbox.h> | ||
#import <XCTest/XCTest.h> | ||
#import <TargetConditionals.h> | ||
|
||
#if TARGET_OS_IPHONE | ||
#define MGLEdgeInsets UIEdgeInsets | ||
#define MGLEdgeInsetsMake UIEdgeInsetsMake | ||
#else | ||
#define MGLEdgeInsets NSEdgeInsets | ||
#define MGLEdgeInsetsMake NSEdgeInsetsMake | ||
#endif | ||
|
||
@interface MGLMapProjectionTests : XCTestCase | ||
|
||
@property (nonatomic, retain) MGLMapView *mapView; | ||
@property (nonatomic, retain) MGLMapProjection *mapProjection; | ||
|
||
@end | ||
|
||
@implementation MGLMapProjectionTests | ||
|
||
- (void)setUp { | ||
[super setUp]; | ||
|
||
[MGLSettings setApiKey:@"pk.feedcafedeadbeefbadebede"]; | ||
NSURL *styleURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"one-liner" withExtension:@"json"]; | ||
self.mapView = [[MGLMapView alloc] initWithFrame:CGRectMake(0, 0, 200, 200) styleURL:styleURL]; | ||
|
||
[self.mapView setVisibleCoordinateBounds:MGLCoordinateBoundsMake(CLLocationCoordinate2DMake(1.0, 1.0), | ||
CLLocationCoordinate2DMake(2.0, 2.0))]; | ||
|
||
self.mapProjection = self.mapView.mapProjection; | ||
} | ||
|
||
- (void)tearDown { | ||
self.mapView = nil; | ||
self.mapProjection = nil; | ||
[MGLSettings setApiKey:nil]; | ||
|
||
[super tearDown]; | ||
} | ||
|
||
- (void)testMapProjectionCamera { | ||
XCTAssertTrue([self.mapProjection.camera isEqualToMapCamera:self.mapView.camera], | ||
@"Map projection camera must be equal to the map view's one"); | ||
} | ||
|
||
- (void)testMapProjectionCameraSet { | ||
MGLCoordinateBounds newBounds = MGLCoordinateBoundsMake(CLLocationCoordinate2DMake(3.0, 3.0), | ||
CLLocationCoordinate2DMake(4.0, 4.0)); | ||
MGLEdgeInsets paddings = MGLEdgeInsetsMake(10.0, 10.0, 10.0, 10.0); | ||
|
||
MGLMapCamera *newCamera = [self.mapView cameraThatFitsCoordinateBounds:newBounds edgePadding:paddings]; | ||
[self.mapProjection setCamera:newCamera withEdgeInsets:paddings]; | ||
|
||
XCTAssertTrue([self.mapProjection.camera isEqualToMapCamera:newCamera], | ||
@"Map projection camera must be equal to the one just set"); | ||
|
||
CLLocationCoordinate2D topLeftCoordinate = [self.mapProjection convertPoint:CGPointMake(10.0, 10.0)]; | ||
XCTAssertEqualWithAccuracy(topLeftCoordinate.latitude, 4.0, 1e-3); | ||
XCTAssertEqualWithAccuracy(topLeftCoordinate.longitude, 3.0, 1e-3); | ||
|
||
CLLocationCoordinate2D bottomRightCoordinate = [self.mapProjection convertPoint:CGPointMake(190.0, 190.0)]; | ||
XCTAssertEqualWithAccuracy(bottomRightCoordinate.latitude, 3.0, 1e-3); | ||
XCTAssertEqualWithAccuracy(bottomRightCoordinate.longitude, 4.0, 1e-3); | ||
|
||
CLLocationCoordinate2D centerCoordinate = [self.mapProjection convertPoint:CGPointMake(100.0, 100.0)]; | ||
XCTAssertEqualWithAccuracy(centerCoordinate.latitude, 3.5, 1e-3); | ||
XCTAssertEqualWithAccuracy(centerCoordinate.longitude, 3.5, 1e-3); | ||
} | ||
|
||
- (void)testMapProjectionVisibleBoundsSet { | ||
MGLCoordinateBounds newBounds = MGLCoordinateBoundsMake(CLLocationCoordinate2DMake(3.0, 3.0), | ||
CLLocationCoordinate2DMake(4.0, 4.0)); | ||
MGLEdgeInsets paddings = MGLEdgeInsetsMake(10.0, 10.0, 10.0, 10.0); | ||
|
||
[self.mapProjection setVisibleCoordinateBounds:newBounds edgePadding:paddings]; | ||
|
||
CLLocationCoordinate2D topLeftCoordinate = [self.mapProjection convertPoint:CGPointMake(10.0, 10.0)]; | ||
XCTAssertEqualWithAccuracy(topLeftCoordinate.latitude, 4.0, 1e-3); | ||
XCTAssertEqualWithAccuracy(topLeftCoordinate.longitude, 3.0, 1e-3); | ||
|
||
CLLocationCoordinate2D bottomRightCoordinate = [self.mapProjection convertPoint:CGPointMake(190.0, 190.0)]; | ||
XCTAssertEqualWithAccuracy(bottomRightCoordinate.latitude, 3.0, 1e-3); | ||
XCTAssertEqualWithAccuracy(bottomRightCoordinate.longitude, 4.0, 1e-3); | ||
|
||
CLLocationCoordinate2D centerCoordinate = [self.mapProjection convertPoint:CGPointMake(100.0, 100.0)]; | ||
XCTAssertEqualWithAccuracy(centerCoordinate.latitude, 3.5, 1e-3); | ||
XCTAssertEqualWithAccuracy(centerCoordinate.longitude, 3.5, 1e-3); | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#import "MGLFoundation.h" | ||
#import "MGLMapView.h" | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
/** | ||
The aim of this class is to provide the functionality of changing the camera state and | ||
converting between map view screen coordinates and geographical coordinates without | ||
changing the actual map view camera state. | ||
*/ | ||
MGL_EXPORT | ||
@interface MGLMapProjection : NSObject | ||
|
||
/** | ||
Initializes and returns the new projection object with the current | ||
camera state from the provided map view. | ||
@param mapView The map view the camera state to use for the initialization. | ||
@return An initialized map projection. | ||
*/ | ||
- (instancetype)initWithMapView:(MGLMapView*)mapView; | ||
|
||
/** | ||
A camera representing the current projection state | ||
*/ | ||
@property (readonly, copy) MGLMapCamera *camera; | ||
|
||
/** | ||
Change the projection state with camera and padding values. | ||
@param camera The new camera to be used in the projection calculation. | ||
@param insets The insets applied on top of the camera be used in the projection calculation. | ||
@note `MGLMapView` instance frame must not be changed since this projection is initialized, | ||
otherwise the calculation may be wrong. | ||
*/ | ||
- (void)setCamera:(MGLMapCamera * _Nonnull)camera withEdgeInsets:(UIEdgeInsets)insets; | ||
|
||
/** | ||
Change the projection state to make the provided bounds visible with the specified inset. | ||
@param bounds The bounds that the viewport should fit. | ||
@param insets The insets applied on top of the viewport to be used in the projection calculation. | ||
@note `MGLMapView` instance frame must not be changed since this projection is initialized, | ||
otherwise the calculation may be wrong. | ||
*/ | ||
- (void)setVisibleCoordinateBounds:(MGLCoordinateBounds)bounds edgePadding:(UIEdgeInsets)insets; | ||
|
||
/** | ||
Converts a point in the coordinate system of the map view the projection | ||
was initialized with to the geographical coordinate. | ||
@param point The point to convert. | ||
@return The geographic coordinate at the given point. | ||
*/ | ||
- (CLLocationCoordinate2D)convertPoint:(CGPoint)point; | ||
|
||
/** | ||
Converts a geographic coordinate to a point in the map view's the projection | ||
was initialized with coordinate system. | ||
@param coordinate The geographic coordinate to convert. | ||
@return The point corresponding to the given geographic coordinate. | ||
*/ | ||
- (CGPoint)convertCoordinate:(CLLocationCoordinate2D)coordinate; | ||
|
||
/** | ||
The distance in meters spanned by a single point for the current camera. | ||
*/ | ||
@property (readonly) CLLocationDistance metersPerPoint; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
Oops, something went wrong.