forked from maplibre/maplibre-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.
Add iOS example for constraining map to certain area (maplibre#2392)
- Loading branch information
Showing
7 changed files
with
136 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Blocking Gestures | ||
|
||
Constrain the map to a certain area. | ||
|
||
## Overview | ||
|
||
To constrain the map to a certain area, you need to implement the ``MLNMapViewDelegate/mapView:shouldChangeFromCamera:toCamera:`` method of ``MLNMapViewDelegate``. By returning a boolean you can either allow or disallow a camera change. | ||
|
||
<!-- include-example(BlockingGesturesExample) --> | ||
|
||
```swift | ||
// Denver, Colorado | ||
private let center = CLLocationCoordinate2D(latitude: 39.748947, longitude: -104.995882) | ||
|
||
// Colorado’s bounds | ||
private let colorado = MLNCoordinateBounds( | ||
sw: CLLocationCoordinate2D(latitude: 36.986207, longitude: -109.049896), | ||
ne: CLLocationCoordinate2D(latitude: 40.989329, longitude: -102.062592) | ||
) | ||
|
||
struct BlockingGesturesExample: UIViewRepresentable { | ||
class Coordinator: NSObject, MLNMapViewDelegate { | ||
func mapView(_ mapView: MLNMapView, shouldChangeFrom _: MLNMapCamera, to newCamera: MLNMapCamera) -> Bool { | ||
// Get the current camera to restore it after. | ||
let currentCamera = mapView.camera | ||
|
||
// From the new camera obtain the center to test if it’s inside the boundaries. | ||
let newCameraCenter = newCamera.centerCoordinate | ||
|
||
// Set the map’s visible bounds to newCamera. | ||
mapView.camera = newCamera | ||
let newVisibleCoordinates = mapView.visibleCoordinateBounds | ||
|
||
// Revert the camera. | ||
mapView.camera = currentCamera | ||
|
||
// Test if the newCameraCenter and newVisibleCoordinates are inside self.colorado. | ||
let inside = MLNCoordinateInCoordinateBounds(newCameraCenter, colorado) | ||
let intersects = MLNCoordinateInCoordinateBounds(newVisibleCoordinates.ne, colorado) && MLNCoordinateInCoordinateBounds(newVisibleCoordinates.sw, colorado) | ||
|
||
return inside && intersects | ||
} | ||
} | ||
|
||
func makeUIView(context: Context) -> MLNMapView { | ||
let mapView = MLNMapView(frame: .zero, styleURL: VERSATILES_COLORFUL_STYLE) | ||
mapView.setCenter(center, zoomLevel: 10, direction: 0, animated: false) | ||
mapView.delegate = context.coordinator | ||
|
||
return mapView | ||
} | ||
|
||
func updateUIView(_: MLNMapView, context _: Context) {} | ||
|
||
func makeCoordinator() -> Coordinator { | ||
Coordinator() | ||
} | ||
} | ||
``` | ||
|
||
The style used in this example can be found here: <doc:ExampleStyles>. |
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,9 @@ | ||
# Styles in Examples | ||
|
||
The following styles are used in the examples: | ||
|
||
<!-- include-example(ExampleStyles) --> | ||
|
||
```swift | ||
let VERSATILES_COLORFUL_STYLE = URL(string: "https://tiles.versatiles.org/assets/styles/colorful.json") | ||
``` |
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
54 changes: 54 additions & 0 deletions
54
platform/ios/app-swift/Sources/BlockingGesturesExample.swift
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,54 @@ | ||
import MapLibre | ||
import SwiftUI | ||
import UIKit | ||
|
||
// #-example-code(BlockingGesturesExample) | ||
// Denver, Colorado | ||
private let center = CLLocationCoordinate2D(latitude: 39.748947, longitude: -104.995882) | ||
|
||
// Colorado’s bounds | ||
private let colorado = MLNCoordinateBounds( | ||
sw: CLLocationCoordinate2D(latitude: 36.986207, longitude: -109.049896), | ||
ne: CLLocationCoordinate2D(latitude: 40.989329, longitude: -102.062592) | ||
) | ||
|
||
struct BlockingGesturesExample: UIViewRepresentable { | ||
class Coordinator: NSObject, MLNMapViewDelegate { | ||
func mapView(_ mapView: MLNMapView, shouldChangeFrom _: MLNMapCamera, to newCamera: MLNMapCamera) -> Bool { | ||
// Get the current camera to restore it after. | ||
let currentCamera = mapView.camera | ||
|
||
// From the new camera obtain the center to test if it’s inside the boundaries. | ||
let newCameraCenter = newCamera.centerCoordinate | ||
|
||
// Set the map’s visible bounds to newCamera. | ||
mapView.camera = newCamera | ||
let newVisibleCoordinates = mapView.visibleCoordinateBounds | ||
|
||
// Revert the camera. | ||
mapView.camera = currentCamera | ||
|
||
// Test if the newCameraCenter and newVisibleCoordinates are inside self.colorado. | ||
let inside = MLNCoordinateInCoordinateBounds(newCameraCenter, colorado) | ||
let intersects = MLNCoordinateInCoordinateBounds(newVisibleCoordinates.ne, colorado) && MLNCoordinateInCoordinateBounds(newVisibleCoordinates.sw, colorado) | ||
|
||
return inside && intersects | ||
} | ||
} | ||
|
||
func makeUIView(context: Context) -> MLNMapView { | ||
let mapView = MLNMapView(frame: .zero, styleURL: VERSATILES_COLORFUL_STYLE) | ||
mapView.setCenter(center, zoomLevel: 10, direction: 0, animated: false) | ||
mapView.delegate = context.coordinator | ||
|
||
return mapView | ||
} | ||
|
||
func updateUIView(_: MLNMapView, context _: Context) {} | ||
|
||
func makeCoordinator() -> Coordinator { | ||
Coordinator() | ||
} | ||
} | ||
|
||
// #-end-example-code |
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,7 @@ | ||
import Foundation | ||
|
||
// #-example-code(ExampleStyles) | ||
|
||
let VERSATILES_COLORFUL_STYLE = URL(string: "https://tiles.versatiles.org/assets/styles/colorful.json") | ||
|
||
// #-end-example-code |
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