From fd06c0b4ac64826b225ea4a5e416e9d4dd204efc Mon Sep 17 00:00:00 2001 From: Max Schedin Date: Mon, 7 Mar 2016 12:14:16 +0100 Subject: [PATCH] Added method for getting current bounds Using recommended apporach until more native call is available. --- android/API.md | 1 + .../ReactNativeMapboxGLManager.java | 16 ++++++++++++++++ .../ReactNativeMapboxGLModule.java | 6 ++++++ index.android.js | 3 +++ 4 files changed, 26 insertions(+) diff --git a/android/API.md b/android/API.md index 56b8db7..01e58cf 100644 --- a/android/API.md +++ b/android/API.md @@ -29,6 +29,7 @@ | `getDirection` | `mapViewRef`, `callback` | Gets the current direction. Returns a single callback object. | | `onOpenAnnotation` | `{title: null, subtitle: null, latitude: 0, longitude: 0}` | Fired when focusing a an annotation. If the annotation is opened already, the event will not fire. | `onLongPress` | `{latitude: 0, longitude: 0}` | Fired when the user taps and holds the map. +| `getBounds` | `mapViewRef`, `callback` | Returns current bounds for view (NE & SW). ## Methods for Modifying the Map State diff --git a/android/src/main/java/com/mapbox/reactnativemapboxgl/ReactNativeMapboxGLManager.java b/android/src/main/java/com/mapbox/reactnativemapboxgl/ReactNativeMapboxGLManager.java index 6d23208..a564515 100644 --- a/android/src/main/java/com/mapbox/reactnativemapboxgl/ReactNativeMapboxGLManager.java +++ b/android/src/main/java/com/mapbox/reactnativemapboxgl/ReactNativeMapboxGLManager.java @@ -36,6 +36,7 @@ import java.net.URL; import java.net.HttpURLConnection; import android.graphics.drawable.BitmapDrawable; import javax.annotation.Nullable; +import android.graphics.PointF; public class ReactNativeMapboxGLManager extends SimpleViewManager { @@ -397,6 +398,21 @@ public class ReactNativeMapboxGLManager extends SimpleViewManager { return callbackDict; } + public WritableMap getBounds(MapView view) { + WritableMap callbackDict = Arguments.createMap(); + int viewportWidth = view.getWidth(); + int viewportHeight = view.getHeight(); + if (viewportWidth > 0 && viewportHeight > 0) { + LatLng ne = view.fromScreenLocation(new PointF(viewportWidth, 0)); + LatLng sw = view.fromScreenLocation(new PointF(0, viewportHeight)); + callbackDict.putDouble("latNE", ne.getLatitude()); + callbackDict.putDouble("lngNE", ne.getLongitude()); + callbackDict.putDouble("latSW", sw.getLatitude()); + callbackDict.putDouble("lngSW", sw.getLongitude()); + } + return callbackDict; + } + public MapView getMapView() { return mapView; } diff --git a/android/src/main/java/com/mapbox/reactnativemapboxgl/ReactNativeMapboxGLModule.java b/android/src/main/java/com/mapbox/reactnativemapboxgl/ReactNativeMapboxGLModule.java index 87a92f9..ef8a109 100644 --- a/android/src/main/java/com/mapbox/reactnativemapboxgl/ReactNativeMapboxGLModule.java +++ b/android/src/main/java/com/mapbox/reactnativemapboxgl/ReactNativeMapboxGLModule.java @@ -133,6 +133,12 @@ public class ReactNativeMapboxGLModule extends ReactContextBaseJavaModule { successCallback.invoke(location); } + @ReactMethod + public void getBounds(int mapRef, Callback successCallback) { + WritableMap bounds = aPackage.getManager().getBounds(aPackage.getManager().getMapView()); + successCallback.invoke(bounds); + } + public void setPackage(ReactNativeMapboxGLPackage aPackage) { this.aPackage = aPackage; } diff --git a/index.android.js b/index.android.js index d5f1999..7f8a45f 100644 --- a/index.android.js +++ b/index.android.js @@ -44,6 +44,9 @@ var MapMixins = { getDirection(mapRef, callback) {; NativeModules.MapboxGLManager.getDirection(React.findNodeHandle(this.refs[mapRef]), callback); }, + getBounds(mapRef, callback) { + NativeModules.MapboxGLManager.getBounds(React.findNodeHandle(this.refs[mapRef]), callback); + }, mapStyles: NativeModules.MapboxGLManager.mapStyles, userTrackingMode: NativeModules.MapboxGLManager.userTrackingMode };