Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add internal map delegates, create platform Projection class #390

Merged
merged 8 commits into from
May 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ Mapbox welcomes participation and contributions from everyone.
- `MapboxMap.__map` is now private. ([#374](https://github.com/mapbox/mapbox-maps-ios/pull/374))
- Added `CameraManagerProtocol.setCameraBounds`, `MapboxMap.prefetchZoomDelta`, `MapboxMap.options`, `MapboxMap.reduceMemoryUse()`, `MapboxMap.resourceOptions` and `MapboxMap.elevation(at:)`. ([#374](https://github.com/mapbox/mapbox-maps-ios/pull/374))
- Removed `OfflineError.invalidResult` and `OfflineError.typeMismatch`. ([#374](https://github.com/mapbox/mapbox-maps-ios/pull/374))
- Updated `Projection` APIs to be more Swift-like. ([#390](https://github.com/mapbox/mapbox-maps-ios/pull/390))

### Bug fixes 🐞

- Fixed bug with `TileStore.tileRegionGeometry` returning invalid value. ([#390](https://github.com/mapbox/mapbox-maps-ios/pull/390))

### Features ✨ and improvements 🏁

Expand Down Expand Up @@ -50,7 +55,6 @@ Mapbox welcomes participation and contributions from everyone.
- `OfflineRegionManager` (though this API is deprecated)
- Adds `loadStyleURI` and `loadStyleJSON` to `MapboxMap`. ([#354](https://github.com/mapbox/mapbox-maps-ios/pull/354))


### Bug fixes 🐞

- Fixed an issue where the map's scale bar and compass view could trigger `layoutSubviews()` for the map view. ([#338](https://github.com/mapbox/mapbox-maps-ios/pull/338))
Expand Down
22 changes: 22 additions & 0 deletions Mapbox/MapboxMaps.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,10 @@
CAE43EE7265462B7006F43E9 /* MockMapFeatureQueryable.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAE43EE4265462B7006F43E9 /* MockMapFeatureQueryable.swift */; };
CAE43EE8265462B7006F43E9 /* MapboxMapsAnnotationsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAE43EE5265462B7006F43E9 /* MapboxMapsAnnotationsTests.swift */; };
CAE43EEA26546426006F43E9 /* MapViewIntegrationTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA53616D2537ECA600A8AE38 /* MapViewIntegrationTestCase.swift */; };
CAE643E5265D7D4000C9E223 /* MapTransformDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAE643E3265D7D4000C9E223 /* MapTransformDelegate.swift */; };
CAE643E6265D7D4000C9E223 /* MapProjectionDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAE643E4265D7D4000C9E223 /* MapProjectionDelegate.swift */; };
CAE643E9265D7D6400C9E223 /* ProjectionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAE643E8265D7D6400C9E223 /* ProjectionTests.swift */; };
CAE643EA265D7D6400C9E223 /* ProjectionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAE643E8265D7D6400C9E223 /* ProjectionTests.swift */; };
CAED9FB1258C69DB003CCEFE /* MapViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAED9FAF258C69DB003CCEFE /* MapViewTests.swift */; };
CAEDA0B0258E8188003CCEFE /* MapViewIntegrationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAEDA0AE258E8188003CCEFE /* MapViewIntegrationTests.swift */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -760,6 +764,9 @@
CAE43EE12654627E006F43E9 /* TileStore+Tests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "TileStore+Tests.swift"; path = "../../Offline/TileStore+Tests.swift"; sourceTree = "<group>"; };
CAE43EE4265462B7006F43E9 /* MockMapFeatureQueryable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MockMapFeatureQueryable.swift; sourceTree = "<group>"; };
CAE43EE5265462B7006F43E9 /* MapboxMapsAnnotationsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MapboxMapsAnnotationsTests.swift; sourceTree = "<group>"; };
CAE643E3265D7D4000C9E223 /* MapTransformDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MapTransformDelegate.swift; sourceTree = "<group>"; };
CAE643E4265D7D4000C9E223 /* MapProjectionDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MapProjectionDelegate.swift; sourceTree = "<group>"; };
CAE643E8265D7D6400C9E223 /* ProjectionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ProjectionTests.swift; sourceTree = "<group>"; };
CAED9FAF258C69DB003CCEFE /* MapViewTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapViewTests.swift; sourceTree = "<group>"; };
CAEDA0AE258E8188003CCEFE /* MapViewIntegrationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapViewIntegrationTests.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -1222,6 +1229,8 @@
1F11C1102421B05600F8397B /* MapboxMapsFoundation */ = {
isa = PBXGroup;
children = (
CAE643E4265D7D4000C9E223 /* MapProjectionDelegate.swift */,
CAE643E3265D7D4000C9E223 /* MapTransformDelegate.swift */,
CAB77E462654ACE90071D74C /* CameraManagerProtocol.swift */,
CA6245FF26289D6400C79547 /* Cancelable.swift */,
CABCDF362620E03A00D61635 /* CredentialsManager.swift */,
Expand All @@ -1248,6 +1257,7 @@
1F11C11B2421B05600F8397B /* MapboxMapsFoundationTests */ = {
isa = PBXGroup;
children = (
CAE643E7265D7D6400C9E223 /* Projection */,
B5A6921A262754DF00A03412 /* CredentialsManagerTests.swift */,
B5A69219262754DF00A03412 /* DelegatingMapClientTests.swift */,
B5A69216262754DF00A03412 /* DelegatingObserverTests.swift */,
Expand Down Expand Up @@ -1666,6 +1676,14 @@
path = Glyphs;
sourceTree = "<group>";
};
CAE643E7265D7D6400C9E223 /* Projection */ = {
isa = PBXGroup;
children = (
CAE643E8265D7D6400C9E223 /* ProjectionTests.swift */,
);
path = Projection;
sourceTree = "<group>";
};
CAED9FAE258C69A7003CCEFE /* Unit Tests */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -2038,6 +2056,7 @@
0CD62F1024588530006421D1 /* GestureManager.swift in Sources */,
CAE43ED5265460FE006F43E9 /* MapFeatureQueryable.swift in Sources */,
0C708F3124EB1EE2003CE791 /* CircleLayer.swift in Sources */,
CAE643E6265D7D4000C9E223 /* MapProjectionDelegate.swift in Sources */,
0CD62F0724588501006421D1 /* PinchGestureHandler.swift in Sources */,
0782B17B258C236B00D5FCE5 /* MBXGeometry.swift in Sources */,
B529341D2624E9D6003B181C /* DelegatingMapClient.swift in Sources */,
Expand Down Expand Up @@ -2092,6 +2111,7 @@
0CD62F182458865A006421D1 /* CameraView.swift in Sources */,
0C088AC526387EA400107B5E /* DateProvider.swift in Sources */,
CAE43EDA26546192006F43E9 /* OfflineSwitch.swift in Sources */,
CAE643E5265D7D4000C9E223 /* MapTransformDelegate.swift in Sources */,
CA5890DB264B00CE0060987A /* WeakSet.swift in Sources */,
2B8637E62463F36400698135 /* DistanceFormatter.swift in Sources */,
CA9F8CE32641F95C00A8BCB6 /* StyleManagerProtocol.swift in Sources */,
Expand Down Expand Up @@ -2161,6 +2181,7 @@
CA4E2DD8264DC66F0039D80E /* MockLocationStyleDelegate.swift in Sources */,
CAE43EE7265462B7006F43E9 /* MockMapFeatureQueryable.swift in Sources */,
CAE43EE32654627E006F43E9 /* TileStore+Tests.swift in Sources */,
CAE643EA265D7D6400C9E223 /* ProjectionTests.swift in Sources */,
B53B8663260D719100C86BAA /* TestConveniences.swift in Sources */,
B53B8650260D718B00C86BAA /* Stub.swift in Sources */,
CA0B380D25C4C01400B3396E /* IntegrationTestCase.swift in Sources */,
Expand Down Expand Up @@ -2221,6 +2242,7 @@
CA0C43052602BF2D0054D9D0 /* LayerPositionTests.swift in Sources */,
0C32CA2425F982300057ED31 /* GeoJsonSourceTests.swift in Sources */,
0C5CFCF125BB951B0001E753 /* SkyLayerTests.swift in Sources */,
CAE643E9265D7D6400C9E223 /* ProjectionTests.swift in Sources */,
0C32CA2725F982300057ED31 /* VectorSourceTests.swift in Sources */,
CA548FDF251C404B00F829A3 /* MapboxMapsSnapshotTests.swift in Sources */,
CAE43EE22654627E006F43E9 /* TileStore+Tests.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"repositoryURL": "https://github.com/mapbox/mapbox-core-maps-ios.git",
"state": {
"branch": null,
"revision": "a94ee53a29e4fa36d41f9156a9ece6501a6022b6",
"revision": "b293ea5976b2b91d8aceff8096e24107de94afad",
"version": "10.0.0-beta.23"
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ internal struct FlyToInterpolator {
// Unwrap
let sourceCoordUnwrapped = sourceCoord.unwrapForShortestPath(destCoord)

let sourcePointTemp = Projection.project(for: sourceCoordUnwrapped, zoomScale: Double(sourceScale))
let destPointTemp = Projection.project(for: destCoord, zoomScale: Double(sourceScale))
let sourcePointTemp = Projection.project(sourceCoordUnwrapped, zoomScale: sourceScale)
let destPointTemp = Projection.project(destCoord, zoomScale: sourceScale)
sourcePoint = CGPoint(x: sourcePointTemp.x, y: sourcePointTemp.y)
destPoint = CGPoint(x: destPointTemp.x, y: destPointTemp.y)

Expand Down Expand Up @@ -215,7 +215,7 @@ internal struct FlyToInterpolator {

let position = MercatorCoordinate(x: Double(interpolated.x), y: Double(interpolated.y))

return Projection.unproject(for: position, zoomScale: Double(sourceScale))
return Projection.unproject(position, zoomScale: sourceScale)
}

/// Calculates the zoom level given a fraction in [0,1].
Expand Down
75 changes: 75 additions & 0 deletions Sources/MapboxMaps/Foundation/MapProjectionDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import CoreLocation

internal protocol MapProjectionDelegate: AnyObject {
/// Calculate distance spanned by one pixel at the specified latitude and
/// zoom level.
///
/// - Parameters:
/// - latitude: The latitude for which to return the value
/// - zoom: The zoom level
///
/// - Returns: Meters
static func metersPerPoint(for latitude: CLLocationDegrees, zoom: CGFloat) -> Double

/// Calculate Spherical Mercator ProjectedMeters coordinates.
/// - Parameter coordinate: Coordinate at which to calculate the projected
/// meters
///
/// - Returns: Spherical Mercator ProjectedMeters coordinates
static func projectedMeters(for coordinate: CLLocationCoordinate2D) -> ProjectedMeters

/// Calculate a coordinate for a Spherical Mercator projected
/// meters.
///
/// - Parameter projectedMeters: Spherical Mercator ProjectedMeters coordinates
///
/// - Returns: A coordinate
static func coordinate(for projectedMeters: ProjectedMeters) -> CLLocationCoordinate2D

/// Calculate a point on the map in Mercator Projection for a given
/// coordinate at the specified zoom scale.
///
/// - Parameters:
/// - coordinate: The coordinate for which to return the value.
/// - zoomScale: The current zoom factor applied on the map, is used to
/// calculate the world size as tileSize * zoomScale (i.e.
/// 512 * 2 ^ Zoom level) where tileSize is the width of a tile
/// in points.
/// - Returns: Mercator coordinate
static func project(_ coordinate: CLLocationCoordinate2D, zoomScale: CGFloat) -> MercatorCoordinate

/// Calculate a coordinate for a given point on the map in Mercator Projection.
///
/// - Parameters:
/// - mercatorCoordinate: Point on the map in Mercator projection.
/// - zoomScale: The current zoom factor applied on the map, is used to
/// calculate the world size as tileSize * zoomScale (i.e.
/// 512 * 2 ^ Zoom level) where tileSize is the width of a tile in
/// points.
/// - Returns: Unprojected coordinate
static func unproject(_ mercatorCoordinate: MercatorCoordinate, zoomScale: CGFloat) -> CLLocationCoordinate2D
}

public class Projection: MapProjectionDelegate {
Copy link
Member

@pengdev pengdev May 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Android the MapboxMap : MapProjectionDelegate to be more discoverable, where the functions inside MapProjectionDelegate are not static.

gl-native exposed generated static APIs can be directly used as Projection.project(point, zoomScale).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's leave this as a point of difference? At the moment there's no need on iOS because of the lack of plugins. /cc @tobrun

internal init() {}

public static func metersPerPoint(for latitude: CLLocationDegrees, zoom: CGFloat) -> Double {
return MapboxCoreMaps.Projection.getMetersPerPixelAtLatitude(forLatitude: latitude, zoom: Double(zoom))
}

public static func projectedMeters(for coordinate: CLLocationCoordinate2D) -> ProjectedMeters {
return MapboxCoreMaps.Projection.projectedMetersForCoordinate(for: coordinate)
}

public static func coordinate(for projectedMeters: ProjectedMeters) -> CLLocationCoordinate2D {
return MapboxCoreMaps.Projection.coordinateForProjectedMeters(for: projectedMeters)
}

public static func project(_ coordinate: CLLocationCoordinate2D, zoomScale: CGFloat) -> MercatorCoordinate {
return MapboxCoreMaps.Projection.project(for: coordinate, zoomScale: Double(zoomScale))
}

public static func unproject(_ mercatorCoordinate: MercatorCoordinate, zoomScale: CGFloat) -> CLLocationCoordinate2D {
return MapboxCoreMaps.Projection.unproject(for: mercatorCoordinate, zoomScale: Double(zoomScale))
}
}
31 changes: 31 additions & 0 deletions Sources/MapboxMaps/Foundation/MapTransformDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
internal protocol MapTransformDelegate: AnyObject {
/// Gets the size of the map in points
var size: CGSize { get set }
julianrex marked this conversation as resolved.
Show resolved Hide resolved

/// Notify map about gesture being in progress.
var isGestureInProgress: Bool { get set }

/// Tells the map rendering engine that the animation is currently performed
/// by the user (e.g. with a `setCamera()` calls series). It adjusts the
/// engine for the animation use case.
/// In particular, it brings more stability to symbol placement and rendering.
var isUserAnimationInProgress: Bool { get set }

/// Returns the map's options
var options: MapOptions { get }

/// Set the map north orientation
///
/// - Parameter northOrientation: The map north orientation to set
func setNorthOrientation(northOrientation: NorthOrientation)

/// Set the map constrain mode
///
/// - Parameter constrainMode: The map constraint mode to set
func setConstrainMode(_ constrainMode: ConstrainMode)

/// Set the map viewport mode
///
/// - Parameter viewportMode: The map viewport mode to set
func setViewportMode(_ viewportMode: ViewportMode)
}
59 changes: 45 additions & 14 deletions Sources/MapboxMaps/Foundation/MapboxMap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@ public final class MapboxMap {
/// The underlying renderer object responsible for rendering the map
private let __map: Map

internal var size: CGSize {
get {
CGSize(__map.getSize())
}
set {
__map.setSizeFor(Size(newValue))
}
}

/// The `style` object supports run time styling.
public internal(set) var style: Style

Expand Down Expand Up @@ -106,11 +97,6 @@ public final class MapboxMap {
}
}

/// Returns the map's options
public var options: MapOptions {
return __map.getOptions()
}

/// Reduces memory use. Useful to call when the application gets paused or
/// sent to background.
internal func reduceMemoryUse() {
Expand Down Expand Up @@ -174,6 +160,51 @@ public final class MapboxMap {
}
}

extension MapboxMap: MapTransformDelegate {
julianrex marked this conversation as resolved.
Show resolved Hide resolved
internal var size: CGSize {
get {
CGSize(__map.getSize())
}
set {
__map.setSizeFor(Size(newValue))
}
}

internal var isGestureInProgress: Bool {
get {
return __map.isGestureInProgress()
}
set {
__map.setGestureInProgressForInProgress(newValue)
}
}

internal var isUserAnimationInProgress: Bool {
get {
return __map.isUserAnimationInProgress()
}
set {
__map.setUserAnimationInProgressForInProgress(newValue)
}
}

public var options: MapOptions {
return __map.getOptions()
}

internal func setNorthOrientation(northOrientation: NorthOrientation) {
__map.setNorthOrientationFor(northOrientation)
}

internal func setConstrainMode(_ constrainMode: ConstrainMode) {
__map.setConstrainModeFor(constrainMode)
}

internal func setViewportMode(_ viewportMode: ViewportMode) {
__map.setViewportModeFor(viewportMode)
}
}

// MARK: - CameraManagerProtocol -

extension MapboxMap: CameraManagerProtocol {
Expand Down
2 changes: 1 addition & 1 deletion Sources/MapboxMaps/MapView/MapView+Supportable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ extension MapView: LocationSupportableMapView {
}

public func metersPerPointAtLatitude(latitude: CLLocationDegrees) -> CLLocationDistance {
return Projection.getMetersPerPixelAtLatitude(forLatitude: latitude, zoom: Double(cameraState.zoom))
return Projection.metersPerPoint(for: latitude, zoom: cameraState.zoom)
}

public func subscribeRenderFrameHandler(_ handler: @escaping (MapboxCoreMaps.Event) -> Void) {
Expand Down
4 changes: 2 additions & 2 deletions Sources/MapboxMaps/Offline/TileStore+MapboxMaps.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ extension TileStore {
/// user-controlled thread.
public func tileRegionGeometry(forId id: String,
completion: @escaping (Result<Geometry, Error>) -> Void) {
__getTileRegion(forId: id,
callback: tileStoreClosureAdapter(for: completion, type: Geometry.self))
__getTileRegionGeometry(forId: id,
callback: tileStoreClosureAdapter(for: completion, type: Geometry.self))
}

/// Fetch a tile region's associated metadata
Expand Down
6 changes: 3 additions & 3 deletions Sources/MapboxMaps/Ornaments/OrnamentsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ public class OrnamentsManager: NSObject {
view.subscribeCameraChangeHandler { [scalebarView, compassView] (cameraState) in

// Update the scale bar
scalebarView.metersPerPoint = Projection.getMetersPerPixelAtLatitude(
forLatitude: cameraState.center.latitude,
zoom: Double(cameraState.zoom))
scalebarView.metersPerPoint = Projection.metersPerPoint(
for: cameraState.center.latitude,
zoom: cameraState.zoom)

// Update the compass
compassView.currentBearing = Double(cameraState.bearing)
Expand Down
11 changes: 11 additions & 0 deletions Tests/MapboxMapsTests/Foundation/MapboxMapTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,15 @@ final class MapboxMapTests: XCTestCase {
XCTAssertEqual(camera.padding, .zero)
XCTAssertEqual(camera.pitch, 0)
}

func testProtocolConformance() {
let map = MapboxMap(mapClient: MockMapClient(), mapInitOptions: MapInitOptions())

// Compilation check only
_ = map as MapTransformDelegate
_ = map as CameraManagerProtocol
_ = map as MapFeatureQueryable
_ = map as ObservableProtocol
_ = map as MapEventsObservable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, this is an interesting concept as a test I like it

}
}
Loading