Skip to content

Commit ba7b1d9

Browse files
authored
Added maxPitch support (#4108)
* Add maxPitch prop support * MaxPitch docs * Add maxPitch prop handler for MapView
1 parent a0888e9 commit ba7b1d9

File tree

10 files changed

+86
-1
lines changed

10 files changed

+86
-1
lines changed

android/src/main/java/com/rnmapbox/rnmbx/components/mapview/RNMBXMapView.kt

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ open class RNMBXMapView(private val mContext: Context, var mManager: RNMBXMapVie
160160
private var mQueuedFeatures: MutableList<AbstractMapFeature>? = ArrayList()
161161
private val mCameraChangeTracker = CameraChangeTracker()
162162
private var mPreferredFrameRate: Int? = null
163+
private var mMaxPitch: Double? = null
163164
private lateinit var mMap: MapboxMap
164165

165166
private lateinit var mMapView: MapView
@@ -513,7 +514,8 @@ open class RNMBXMapView(private val mContext: Context, var mManager: RNMBXMapVie
513514
ATTRIBUTION(RNMBXMapView::applyAttribution),
514515
LOGO(RNMBXMapView::applyLogo),
515516
SCALEBAR(RNMBXMapView::applyScaleBar),
516-
COMPASS(RNMBXMapView::applyCompass),;
517+
COMPASS(RNMBXMapView::applyCompass),
518+
MAX_PITCH(RNMBXMapView::applyMaxPitch),;
517519

518520
override fun apply(mapView: RNMBXMapView) {
519521
_apply(mapView)
@@ -582,6 +584,28 @@ open class RNMBXMapView(private val mContext: Context, var mManager: RNMBXMapVie
582584
}
583585
}
584586

587+
fun setReactMaxPitch(maxPitch: Double?) {
588+
mMaxPitch = maxPitch
589+
changes.add(Property.MAX_PITCH)
590+
}
591+
592+
private fun applyMaxPitch() {
593+
val maxPitch = mMaxPitch ?: return
594+
if (!this::mMap.isInitialized) {
595+
return
596+
}
597+
598+
val currentBounds = mMap.getBounds()
599+
val builder = CameraBoundsOptions.Builder()
600+
.bounds(currentBounds.bounds)
601+
.maxZoom(currentBounds.maxZoom)
602+
.minZoom(currentBounds.minZoom)
603+
.minPitch(currentBounds.minPitch)
604+
.maxPitch(maxPitch)
605+
606+
mMap.setBounds(builder.build())
607+
}
608+
585609
fun setReactStyleURL(styleURL: String) {
586610
mStyleURL = styleURL
587611
changes.add(Property.STYLE_URL)

android/src/main/java/com/rnmapbox/rnmbx/components/mapview/RNMBXMapViewManager.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,12 @@ open class RNMBXMapViewManager(context: ReactApplicationContext, val viewTagReso
252252
}
253253
}
254254

255+
@ReactProp(name = "maxPitch")
256+
override fun setMaxPitch(map: RNMBXMapView, maxPitch: Dynamic) {
257+
// Allow clearing the limit by passing null from JS
258+
map.setReactMaxPitch(if (maxPitch.type == ReadableType.Null) null else maxPitch.asDouble())
259+
}
260+
255261
@ReactProp(name = "rotateEnabled")
256262
override fun setRotateEnabled(map: RNMBXMapView, rotateEnabled: Dynamic) {
257263
map.withMapView {

android/src/main/old-arch/com/facebook/react/viewmanagers/RNMBXMapViewManagerDelegate.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ public void setProperty(T view, String propName, @Nullable Object value) {
6868
case "pitchEnabled":
6969
mViewManager.setPitchEnabled(view, new DynamicFromObject(value));
7070
break;
71+
case "maxPitch":
72+
mViewManager.setMaxPitch(view, new DynamicFromObject(value));
73+
break;
7174
case "deselectAnnotationOnTap":
7275
mViewManager.setDeselectAnnotationOnTap(view, new DynamicFromObject(value));
7376
break;

android/src/main/old-arch/com/facebook/react/viewmanagers/RNMBXMapViewManagerInterface.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public interface RNMBXMapViewManagerInterface<T extends View> {
2727
void setScrollEnabled(T view, Dynamic value);
2828
void setRotateEnabled(T view, Dynamic value);
2929
void setPitchEnabled(T view, Dynamic value);
30+
void setMaxPitch(T view, Dynamic value);
3031
void setDeselectAnnotationOnTap(T view, Dynamic value);
3132
void setRequestDisallowInterceptTouchEvent(T view, Dynamic value);
3233
void setProjection(T view, Dynamic value);

docs/MapView.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ Enable/Disable pitch on map
9494
_defaults to:_ `true`
9595

9696

97+
### maxPitch
98+
99+
```tsx
100+
number
101+
```
102+
Maximum allowed pitch in degrees. Mirrors the Mapbox map option `maxPitch`.
103+
104+
105+
97106
### rotateEnabled
98107

99108
```tsx

docs/docs.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4870,6 +4870,13 @@
48704870
"default": "true",
48714871
"description": "Enable/Disable pitch on map"
48724872
},
4873+
{
4874+
"name": "maxPitch",
4875+
"required": false,
4876+
"type": "number",
4877+
"default": "none",
4878+
"description": "Maximum allowed pitch in degrees. Mirrors the Mapbox map option `maxPitch`."
4879+
},
48734880
{
48744881
"name": "rotateEnabled",
48754882
"required": false,

ios/RNMBX/RNMBXMapView.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ open class RNMBXMapView: UIView, RCTInvalidating {
440440
case scrollEnabled
441441
case rotateEnabled
442442
case pitchEnabled
443+
case maxPitch
443444
case onMapChange
444445
case styleURL
445446
case gestureSettings
@@ -476,6 +477,8 @@ open class RNMBXMapView: UIView, RCTInvalidating {
476477
map.applyLocalizeLabels()
477478
case .pitchEnabled:
478479
map.applyPitchEnabled()
480+
case .maxPitch:
481+
map.applyMaxPitch()
479482
case .gestureSettings:
480483
map.applyGestureSettings()
481484
case .preferredFramesPerSecond:
@@ -538,6 +541,30 @@ open class RNMBXMapView: UIView, RCTInvalidating {
538541
}
539542
}
540543

544+
var maxPitch: Double? = nil
545+
546+
@objc public func setReactMaxPitch(_ value: NSNumber?) {
547+
maxPitch = value?.doubleValue
548+
changed(.maxPitch)
549+
}
550+
551+
func applyMaxPitch() {
552+
guard let maxPitch = maxPitch else { return }
553+
554+
withMapboxMap { mapboxMap in
555+
logged("RNMBXMapView.applyMaxPitch") {
556+
let current = mapboxMap.cameraBounds
557+
var options = CameraBoundsOptions()
558+
options.bounds = current.bounds
559+
options.maxZoom = current.maxZoom
560+
options.minZoom = current.minZoom
561+
options.minPitch = current.minPitch
562+
options.maxPitch = maxPitch
563+
try mapboxMap.setCameraBounds(with: options)
564+
}
565+
}
566+
}
567+
541568
var locale: (layerIds: [String]?, locale: Locale)? = nil
542569

543570
@objc public func setReactLocalizeLabels(_ value: NSDictionary?) {

ios/RNMBX/RNMBXMapViewManager.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ @interface RCT_EXTERN_REMAP_MODULE(RNMBXMapView, RNMBXMapViewManager, RCTViewMan
2626
RCT_REMAP_VIEW_PROPERTY(scrollEnabled, reactScrollEnabled, BOOL)
2727
RCT_REMAP_VIEW_PROPERTY(rotateEnabled, reactRotateEnabled, BOOL)
2828
RCT_REMAP_VIEW_PROPERTY(pitchEnabled, reactPitchEnabled, BOOL)
29+
RCT_REMAP_VIEW_PROPERTY(maxPitch, reactMaxPitch, NSNumber)
2930
RCT_REMAP_VIEW_PROPERTY(preferredFramesPerSecond, reactPreferredFramesPerSecond, NSInteger)
3031
RCT_EXPORT_VIEW_PROPERTY(deselectAnnotationOnTap, BOOL)
3132

src/components/MapView.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,11 @@ type Props = ViewProps & {
206206
*/
207207
pitchEnabled?: boolean;
208208

209+
/**
210+
* Maximum allowed pitch in degrees. Mirrors the Mapbox map option `maxPitch`.
211+
*/
212+
maxPitch?: number;
213+
209214
/**
210215
* Enable/Disable rotation on map
211216
*/

src/specs/RNMBXMapViewNativeComponent.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
BubblingEventHandler,
55
DirectEventHandler,
66
Int32,
7+
Double,
78
// @ts-ignore - CI environment type resolution issue for CodegenTypes
89
} from 'react-native/Libraries/Types/CodegenTypes';
910

@@ -53,6 +54,7 @@ export interface NativeProps extends ViewProps {
5354
scrollEnabled?: OptionalProp<boolean>;
5455
rotateEnabled?: OptionalProp<boolean>;
5556
pitchEnabled?: OptionalProp<boolean>;
57+
maxPitch?: OptionalProp<Double>;
5658

5759
deselectAnnotationOnTap?: OptionalProp<boolean>;
5860

0 commit comments

Comments
 (0)