Skip to content

Commit

Permalink
Return (-1, -1) if point for coordinate is out of map's bounds (#1490)
Browse files Browse the repository at this point in the history
  • Loading branch information
maios committed Jul 28, 2022
1 parent e2d6f0c commit 52b7697
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
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
27 changes: 27 additions & 0 deletions Tests/MapboxMapsTests/Foundation/MapboxMapsFoundationTests.swift
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

0 comments on commit 52b7697

Please sign in to comment.