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

Return (-1, -1) if point for coordinate is out of map's bounds #1490

Merged
merged 4 commits into from
Jul 28, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Mapbox welcomes participation and contributions from everyone.
* Remove experimental ModelLayer API. ([#1486](https://github.com/mapbox/mapbox-maps-ios/pull/1486))
* Fix NaN latitude crash rarely happening in `CameraAnimationsManager.fly(to:duration:completion)`. ([#1485](https://github.com/mapbox/mapbox-maps-ios/pull/1485))
* Update to MapboxCoreMaps 10.7.0 and MapboxCommon 22.1.0. ([#1492](https://github.com/mapbox/mapbox-maps-ios/pull/1492))
* Limit `MapboxMap.points(for:)` to the bounds of the map view, if the coordinate's point is beyond then return (-1, -1) for its corresponding point.([#1490](https://github.com/mapbox/mapbox-maps-ios/pull/1490))

## 10.7.0-rc.1 - July 14, 2022

Expand Down
6 changes: 4 additions & 2 deletions Sources/MapboxMaps/Foundation/MapboxMap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -489,11 +489,13 @@ public final class MapboxMap: MapboxMapProtocol {
/// This API isn't supported by Globe projection.
///
/// - Parameter coordinates: The coordinate to convert.
/// - Returns: An array of `CGPoint` relative to the `UIView`.
/// - Returns: An array of `CGPoint` relative to the `UIView.
/// If a coordinate's point is outside of map view's bounds, it will be `(-1, -1)`
public func points(for coordinates: [CLLocationCoordinate2D]) -> [CGPoint] {
let bounds = CGRect(origin: .zero, size: size)
let locations = coordinates.map { CLLocation(latitude: $0.latitude, longitude: $0.longitude) }
let screenCoords = __map.pixelsForCoordinates(forCoordinates: locations)
return screenCoords.map { $0.point }
return screenCoords.map { bounds.contains($0.point) ? $0.point : CGPoint(x: -1.0, y: -1.0) }
}

/// Converts points in the mapView's coordinate system to geographic coordinates.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,33 @@ class MapboxMapsFoundationTests: XCTestCase {
XCTAssertEqual(originalPoint.y, point.y, accuracy: CGFloat(accuracy))
}

func testCoordinatesToPoints() {
let centerCoordinate = CLLocationCoordinate2D(latitude: 0, longitude: 0)

let maxPoint = CGPoint(x: mapView.bounds.maxX, y: mapView.bounds.maxY)
let boundaryCoordinate = mapView.mapboxMap.coordinate(for: maxPoint)

let outOfBoundsCoordinate = CLLocationCoordinate2D(
latitude: boundaryCoordinate.latitude + 1,
longitude: boundaryCoordinate.longitude + 1)

let convertedPoints = mapView.mapboxMap.points(for: [
centerCoordinate,
boundaryCoordinate,
outOfBoundsCoordinate,
])

// Center point.
XCTAssertEqual(convertedPoints[0].x, mapView.bounds.midX, accuracy: 0.01)
XCTAssertEqual(convertedPoints[0].y, mapView.bounds.midY, accuracy: 0.01)
// Edges.
XCTAssertEqual(convertedPoints[1].x, maxPoint.x, accuracy: 0.01)
XCTAssertEqual(convertedPoints[1].y, maxPoint.y, accuracy: 0.01)
// Out of bounds.
XCTAssertEqual(convertedPoints[2].x, -1.0)
XCTAssertEqual(convertedPoints[2].y, -1.0)
}

// MARK: Converting between CGRect and coordinate bounds

func testRectExtend() {
Expand Down