Skip to content

Commit fe79c5d

Browse files
committed
feat: getCamera ref implementation
1 parent 0e3fac7 commit fe79c5d

File tree

6 files changed

+65
-48
lines changed

6 files changed

+65
-48
lines changed

android/build.gradle

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,27 @@ android {
5353
}
5454

5555
repositories {
56-
mavenCentral()
57-
google()
56+
repositories {
57+
maven {
58+
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
59+
url("$rootDir/../node_modules/react-native/android")
60+
}
61+
maven {
62+
// Android JSC is installed from npm
63+
url("$rootDir/../node_modules/jsc-android/dist")
64+
}
65+
mavenCentral {
66+
// We don't want to fetch react-native from Maven Central as there are
67+
// older versions over there.
68+
content {
69+
excludeGroup "com.facebook.react"
70+
}
71+
}
72+
73+
google()
74+
maven { url "https://maven.google.com" }
75+
maven { url 'https://www.jitpack.io' }
76+
}
5877
}
5978

6079

android/src/main/java/com/osmdroid/OsmMapModule.java

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import com.facebook.react.uimanager.UIManagerModule;
1616

1717

18+
import org.osmdroid.api.IGeoPoint;
19+
1820
import java.io.Closeable;
1921
import java.io.IOException;
2022

@@ -132,39 +134,34 @@ public static void closeQuietly(Closeable closeable) {
132134
// });
133135
// }
134136

135-
// @ReactMethod
136-
// public void getCamera(final int tag, final Promise promise) {
137-
// final ReactApplicationContext context = getReactApplicationContext();
138-
//
139-
// UIManagerModule uiManager = context.getNativeModule(UIManagerModule.class);
140-
// uiManager.addUIBlock(new UIBlock()
141-
// {
142-
// @Override
143-
// public void execute(NativeViewHierarchyManager nvhm)
144-
// {
145-
// OsmMapView view = (OsmMapView) nvhm.resolveView(tag);
146-
// if (view == null) {
147-
// promise.reject("AirMapView not found");
148-
// return;
149-
// }
150-
//
151-
//
152-
// CameraPosition position = view.map.getCameraPosition();
153-
//
154-
// WritableMap centerJson = new WritableNativeMap();
155-
// centerJson.putDouble("latitude", position.target.latitude);
156-
// centerJson.putDouble("longitude", position.target.longitude);
157-
//
158-
// WritableMap cameraJson = new WritableNativeMap();
159-
// cameraJson.putMap("center", centerJson);
160-
// cameraJson.putDouble("heading", (double)position.bearing);
161-
// cameraJson.putDouble("zoom", (double)position.zoom);
162-
// cameraJson.putDouble("pitch", (double)position.tilt);
163-
//
164-
// promise.resolve(cameraJson);
165-
// }
166-
// });
167-
// }
137+
@ReactMethod
138+
public void getCamera(final int tag, final Promise promise) {
139+
final ReactApplicationContext context = getReactApplicationContext();
140+
141+
UIManagerModule uiManager = context.getNativeModule(UIManagerModule.class);
142+
uiManager.addUIBlock(new UIBlock()
143+
{
144+
@Override
145+
public void execute(NativeViewHierarchyManager nvhm)
146+
{
147+
OsmMapView view = (OsmMapView) nvhm.resolveView(tag);
148+
if (view == null) {
149+
promise.reject("AirMapView not found", "AirMapView not found");
150+
return;
151+
}
152+
153+
IGeoPoint position = view.getCameraPosition();
154+
155+
WritableMap centerJson = new WritableNativeMap();
156+
centerJson.putDouble("latitude", position.getLatitude());
157+
centerJson.putDouble("longitude", position.getLongitude());
158+
159+
WritableMap cameraJson = new WritableNativeMap();
160+
cameraJson.putMap("center", centerJson);
161+
promise.resolve(cameraJson);
162+
}
163+
});
164+
}
168165

169166
// @ReactMethod
170167
// public void pointForCoordinate(final int tag, ReadableMap coordinate, final Promise promise) {
@@ -253,7 +250,7 @@ public void getMapBoundaries(final int tag, final Promise promise) {
253250
public void execute(NativeViewHierarchyManager nvhm) {
254251
OsmMapView view = (OsmMapView) nvhm.resolveView(tag);
255252
if (view == null) {
256-
promise.reject("AirMapView not found");
253+
promise.reject("AirMapView not found", "AirMapView not found");
257254
return;
258255
}
259256

android/src/main/java/com/osmdroid/OsmMapView.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,11 @@ public double[][] getMapBoundaries() {
524524
{bounds.getLonWest(), bounds.getLatSouth()}
525525
};
526526
}
527+
public IGeoPoint getCameraPosition() {
528+
OsmMapView mapView = OsmMapView.this;
529+
IGeoPoint pos = mapView.getMapCenter();
530+
return pos;
531+
}
527532

528533
@Override
529534
public boolean dispatchTouchEvent(MotionEvent ev) {

example/ios/Podfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,4 +575,4 @@ SPEC CHECKSUMS:
575575

576576
PODFILE CHECKSUM: 6da59bf95a93cd9a20a1f04c65ca4e2d60d0f706
577577

578-
COCOAPODS: 1.11.3
578+
COCOAPODS: 1.12.0

example/src/App.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ export default function App() {
3535
ref={mapRef}
3636
style={styles.container}
3737
initialRegion={initialRegion}
38+
onRegionChange={async () => {
39+
const camera = await mapRef.current?.getCamera();
40+
console.log(
41+
'🚀 ~ file: App.tsx:40 ~ onRegionChange={ ~ camera:',
42+
camera
43+
);
44+
}}
3845
>
3946
<Marker
4047
coordinate={{

src/MapView.types.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,7 @@ import type { NativeSyntheticEvent } from 'react-native';
55
// use.
66

77
export type Camera = {
8-
/**
9-
* Apple Maps
10-
*/
11-
altitude?: number;
128
center: LatLng;
13-
heading: number;
14-
pitch: number;
15-
16-
/**
17-
* Google Maps
18-
*/
19-
zoom?: number;
209
};
2110

2211
export type MapStyleElement = {

0 commit comments

Comments
 (0)