Skip to content

Commit 238b393

Browse files
authored
feat: add scaleBarUnits prop to MapView (#4211)
1 parent 6c056d9 commit 238b393

8 files changed

Lines changed: 61 additions & 0 deletions

File tree

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,6 +1357,7 @@ open class RNMBXMapView(private val mContext: Context, var mManager: RNMBXMapVie
13571357
}
13581358

13591359
var mScaleBarSettings = OrnamentSettings(enabled = false)
1360+
var mScaleBarUnits: String? = null
13601361

13611362
fun setReactScaleBarEnabled(scaleBarEnabled: Boolean) {
13621363
mScaleBarSettings.enabled = scaleBarEnabled
@@ -1378,9 +1379,21 @@ open class RNMBXMapView(private val mContext: Context, var mManager: RNMBXMapVie
13781379
changes.add(Property.SCALEBAR)
13791380
}
13801381

1382+
fun setReactScaleBarUnits(units: String?) {
1383+
mScaleBarUnits = units
1384+
changes.add(Property.SCALEBAR)
1385+
}
1386+
13811387
private fun applyScaleBar() {
13821388
mapView.scalebar.updateSettings {
13831389
updateOrnament("scaleBar", mScaleBarSettings, this.toGenericOrnamentSettings())
1390+
mScaleBarUnits?.let { units ->
1391+
distanceUnits = when (units) {
1392+
"imperial" -> com.mapbox.maps.plugin.DistanceUnits.IMPERIAL
1393+
"nautical" -> com.mapbox.maps.plugin.DistanceUnits.NAUTICAL
1394+
else -> com.mapbox.maps.plugin.DistanceUnits.METRIC
1395+
}
1396+
}
13841397
}
13851398
workaroundToRelayoutChildOfMapView()
13861399
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,11 @@ open class RNMBXMapViewManager(context: ReactApplicationContext, val viewTagReso
326326
mapView.setReactScaleBarPosition(mapValue)
327327
}
328328

329+
@ReactProp(name = "scaleBarUnits")
330+
override fun setScaleBarUnits(mapView: RNMBXMapView, scaleBarUnits: Dynamic) {
331+
mapView.setReactScaleBarUnits(scaleBarUnits.asString())
332+
}
333+
329334
@ReactProp(name = "compassEnabled")
330335
override fun setCompassEnabled(mapView: RNMBXMapView, compassEnabled: Dynamic) {
331336
mapView.setReactCompassEnabled(compassEnabled.asBoolean())

docs/MapView.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,15 @@ OrnamentPositonProp
246246
247247
[Ornaments](../examples/Map/Ornaments)
248248
249+
### scaleBarUnits
250+
251+
```tsx
252+
'metric' | 'imperial' | 'nautical'
253+
```
254+
Set the scale bar distance units. Defaults to metric.
255+
256+
257+
249258
### surfaceView
250259
251260
```tsx

docs/docs.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5397,6 +5397,13 @@
53975397
"default": "none",
53985398
"description": "[`mapbox` (v10) implementation only] Adds scale bar offset, e.g. `{top: 8, left: 8}` will put the scale bar in top-left corner of the map"
53995399
},
5400+
{
5401+
"name": "scaleBarUnits",
5402+
"required": false,
5403+
"type": "'metric' \\| 'imperial' \\| 'nautical'",
5404+
"default": "none",
5405+
"description": "Set the scale bar distance units. Defaults to metric."
5406+
},
54005407
{
54015408
"name": "surfaceView",
54025409
"required": false,

ios/RNMBX/RNMBXMapView.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,7 @@ open class RNMBXMapView: UIView, RCTInvalidating {
823823
var scaleBarEnabled: Bool? = nil
824824
var scaleBarPosition: OrnamentPosition? = nil
825825
var scaleBarMargins: CGPoint? = nil
826+
var scaleBarUnits: String? = nil
826827

827828
@objc public func setReactScaleBarEnabled(_ value: Bool) {
828829
scaleBarEnabled = value
@@ -836,6 +837,11 @@ open class RNMBXMapView: UIView, RCTInvalidating {
836837
}
837838
}
838839

840+
@objc public func setReactScaleBarUnits(_ value: NSString?) {
841+
scaleBarUnits = value as? String
842+
changed(.scaleBar)
843+
}
844+
839845
func applyScaleBar() {
840846
if let enabled = scaleBarEnabled {
841847
mapView.ornaments.options.scaleBar.visibility = enabled ? .visible : .hidden
@@ -846,6 +852,16 @@ open class RNMBXMapView: UIView, RCTInvalidating {
846852
if let margins = scaleBarMargins {
847853
mapView.ornaments.options.scaleBar.margins = margins
848854
}
855+
if let units = scaleBarUnits {
856+
switch units {
857+
case "imperial":
858+
mapView.ornaments.options.scaleBar.units = .imperial
859+
case "nautical":
860+
mapView.ornaments.options.scaleBar.units = .nautical
861+
default:
862+
mapView.ornaments.options.scaleBar.units = .metric
863+
}
864+
}
849865
}
850866

851867
@objc override public func didSetProps(_ props: [String]) {

ios/RNMBX/RNMBXMapViewComponentView.mm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ - (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &
185185
_view.reactScaleBarPosition = scaleBarPosition;
186186
}
187187

188+
id scaleBarUnits = RNMBXConvertFollyDynamicToId(newViewProps.scaleBarUnits);
189+
if (scaleBarUnits != nil) {
190+
[_view setReactScaleBarUnits:scaleBarUnits];
191+
}
192+
188193
RNMBX_REMAP_OPTIONAL_PROP_BOOL(zoomEnabled, reactZoomEnabled)
189194

190195
RNMBX_REMAP_OPTIONAL_PROP_BOOL(scrollEnabled, reactScrollEnabled)

src/components/MapView.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,11 @@ type Props = ViewProps & {
289289
*/
290290
scaleBarPosition?: OrnamentPositonProp;
291291

292+
/**
293+
* Set the scale bar distance units. Defaults to metric.
294+
*/
295+
scaleBarUnits?: 'metric' | 'imperial' | 'nautical';
296+
292297
/**
293298
* [Android only] Enable/Disable use of GLSurfaceView instead of TextureView.
294299
*/

src/specs/RNMBXMapViewNativeComponent.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export interface NativeProps extends ViewProps {
4949

5050
scaleBarEnabled?: OptionalProp<boolean>;
5151
scaleBarPosition?: UnsafeMixed<any>;
52+
scaleBarUnits?: OptionalProp<string>;
5253

5354
zoomEnabled?: OptionalProp<boolean>;
5455
scrollEnabled?: OptionalProp<boolean>;

0 commit comments

Comments
 (0)