From bb129c396ad3ac8e4913c96ed156898cd548eb6f Mon Sep 17 00:00:00 2001 From: bsudekum Date: Thu, 17 Dec 2015 11:47:22 -0800 Subject: [PATCH] Use constants allow for custom annotations --- android/example.js | 16 +++-- android/src/main/AndroidManifest.xml | 1 + .../ReactNativeMapboxGLManager.java | 61 ++++++++++++++----- .../ReactNativeMapboxGLModule.java | 31 +++++++++- index.android.js | 6 +- 5 files changed, 90 insertions(+), 25 deletions(-) diff --git a/android/example.js b/android/example.js index 70bebc0..a2b1f55 100644 --- a/android/example.js +++ b/android/example.js @@ -23,7 +23,12 @@ var MapExample = React.createClass({ type: 'point', title: 'Important!', subtitle: 'Neat, this is a custom annotation image', - id: 'marker2' + id: 'marker2', + annotationImage: { + url: 'https://cldup.com/7NLZklp8zS.png', + height: 25, + width: 25 + } }, { coordinates: [40.7923, -73.9178], type: 'point', @@ -51,7 +56,6 @@ var MapExample = React.createClass({ console.log(location); }, render() { - console.log(this); return ( this.setDirectionAnimated(mapRef, 0)}> @@ -74,7 +78,7 @@ var MapExample = React.createClass({ }])}> Add new marker - this.setUserTrackingMode(mapRef, 'FOLLOW')}> + this.setUserTrackingMode(mapRef, this.userTrackingMode.follow)}> Set userTrackingMode to follow this.removeAllAnnotations(mapRef)}> @@ -91,15 +95,15 @@ var MapExample = React.createClass({ accessToken={'your-mapbox.com-access-token'} centerCoordinate={this.state.center} debugActive={false} - direction={0} + direction={10} ref={mapRef} onRegionChange={this.onRegionChange} rotationEnabled={true} scrollEnabled={true} style={styles.map} showsUserLocation={true} - styleUrl={'mapbox://styles/mapbox/streets-v8'} - UserLocationTrackingMode={'NONE'} + styleUrl={this.mapStyles.emerald} + userTrackingMode={this.userTrackingMode.none} zoomEnabled={true} zoomLevel={10} compassIsHidden={true} diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml index 7daade7..9fd8743 100644 --- a/android/src/main/AndroidManifest.xml +++ b/android/src/main/AndroidManifest.xml @@ -1,6 +1,7 @@ + \ No newline at end of file diff --git a/android/src/main/java/com/mapbox/reactnativemapboxgl/ReactNativeMapboxGLManager.java b/android/src/main/java/com/mapbox/reactnativemapboxgl/ReactNativeMapboxGLManager.java index 38931c2..d77de08 100644 --- a/android/src/main/java/com/mapbox/reactnativemapboxgl/ReactNativeMapboxGLManager.java +++ b/android/src/main/java/com/mapbox/reactnativemapboxgl/ReactNativeMapboxGLManager.java @@ -3,6 +3,7 @@ package com.mapbox.reactnativemapboxgl; import android.graphics.Color; import android.util.Log; +import android.os.StrictMode; import android.location.Location; @@ -24,10 +25,25 @@ import com.mapbox.mapboxsdk.annotations.PolylineOptions; import com.mapbox.mapboxsdk.constants.MyLocationTracking; import com.mapbox.mapboxsdk.geometry.LatLng; import com.mapbox.mapboxsdk.views.MapView; +import com.mapbox.mapboxsdk.annotations.Sprite; +import com.mapbox.mapboxsdk.annotations.SpriteFactory; +import android.support.v4.content.ContextCompat; +import android.graphics.drawable.Drawable; +import android.graphics.BitmapFactory; +import android.graphics.Bitmap; +import java.io.InputStream; +import java.io.IOException; +import java.net.URL; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import android.graphics.drawable.BitmapDrawable; import javax.annotation.Nullable; + + public class ReactNativeMapboxGLManager extends SimpleViewManager { + public static final String REACT_CLASS = "RCTMapbox"; public static final String PROP_ACCESS_TOKEN = "accessToken"; @@ -41,13 +57,11 @@ public class ReactNativeMapboxGLManager extends SimpleViewManager { public static final String PROP_SCROLL_ENABLED = "scrollEnabled"; public static final String PROP_USER_LOCATON = "showsUserLocation"; public static final String PROP_STYLE_URL = "styleUrl"; - public static final String PROP_USER_TRACKING_MODE = "UserLocationTrackingMode"; + public static final String PROP_USER_TRACKING_MODE = "userTrackingMode"; public static final String PROP_ZOOM_ENABLED = "zoomEnabled"; public static final String PROP_ZOOM_LEVEL = "zoomLevel"; public static final String PROP_SET_TILT = "tilt"; public static final String PROP_COMPASS_IS_HIDDEN = "compassIsHidden"; - public static final String PROP_LOGO_IS_HIDDEN = "logoIsHidden"; - public static final String PROP_ATTRIBUTION_IS_HIDDEN = "attributionButtonIsHidden"; private MapView mapView; @@ -60,6 +74,8 @@ public class ReactNativeMapboxGLManager extends SimpleViewManager { public MapView createViewInstance(ThemedReactContext context) { mapView = new MapView(context, "pk.foo"); mapView.onCreate(null); + StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); + StrictMode.setThreadPolicy(policy); return mapView; } @@ -77,6 +93,17 @@ public class ReactNativeMapboxGLManager extends SimpleViewManager { mapView.setTilt(pitch, 1L); } + public static Drawable drawableFromUrl(String url) throws IOException { + Bitmap x; + + HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); + connection.connect(); + InputStream input = connection.getInputStream(); + + x = BitmapFactory.decodeStream(input); + return new BitmapDrawable(x); + } + @ReactProp(name = PROP_ANNOTATIONS) public void setAnnotations(MapView view, @Nullable ReadableArray value) { if (value == null || value.size() < 1) { @@ -100,6 +127,19 @@ public class ReactNativeMapboxGLManager extends SimpleViewManager { String subtitle = annotation.getString("subtitle"); marker.snippet(subtitle); } + if (annotation.hasKey("annotationImage")) { + ReadableMap annotationImage = annotation.getMap("annotationImage"); + String annotationURL = annotationImage.getString("url"); + try { + Drawable image = drawableFromUrl(annotationURL); + image.setBounds(0, 0, 100, 100); + SpriteFactory spriteFactory = new SpriteFactory(view); + Sprite icon = spriteFactory.fromDrawable(image); + marker.icon(icon); + } catch (Exception e) { + e.printStackTrace(); + } + } view.addMarker(marker); } else if (type.equals("polyline")) { int coordSize = annotation.getArray("coordinates").size(); @@ -228,18 +268,9 @@ public class ReactNativeMapboxGLManager extends SimpleViewManager { } } - @ReactProp(name = PROP_USER_TRACKING_MODE) - public void setMyLocationTrackingMode(MapView view, @Nullable String mode) { - if (mode != null && !mode.isEmpty()) { - if (mode.equals("NONE")) { - view.setMyLocationTrackingMode(MyLocationTracking.TRACKING_NONE); - } else if (mode.equals("FOLLOW")) { - view.setMyLocationTrackingMode(MyLocationTracking.TRACKING_FOLLOW); - } - } else { - view.setMyLocationTrackingMode(MyLocationTracking.TRACKING_NONE); - Log.w(REACT_CLASS, "ErrorTracking mode not found. Setting to NONE."); - } + @ReactProp(name = PROP_USER_TRACKING_MODE, defaultInt = 0) + public void setMyLocationTrackingMode(MapView view, @Nullable int mode) { + view.setMyLocationTrackingMode(mode); } @ReactProp(name = PROP_ZOOM_ENABLED, defaultBoolean = true) diff --git a/android/src/main/java/com/mapbox/reactnativemapboxgl/ReactNativeMapboxGLModule.java b/android/src/main/java/com/mapbox/reactnativemapboxgl/ReactNativeMapboxGLModule.java index 0983c34..a0a2c96 100644 --- a/android/src/main/java/com/mapbox/reactnativemapboxgl/ReactNativeMapboxGLModule.java +++ b/android/src/main/java/com/mapbox/reactnativemapboxgl/ReactNativeMapboxGLModule.java @@ -3,7 +3,10 @@ package com.mapbox.reactnativemapboxgl; import android.content.Context; import android.util.Log; +import java.util.HashMap; +import java.util.Map; +import com.mapbox.mapboxsdk.constants.Style; import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; @@ -11,6 +14,7 @@ import com.facebook.react.bridge.ReactMethod; import com.facebook.react.bridge.ReadableArray; import com.facebook.react.bridge.ReadableMap; import com.facebook.react.bridge.WritableMap; +import com.mapbox.mapboxsdk.constants.MyLocationTracking; import com.mapbox.mapboxsdk.geometry.LatLng; import com.mapbox.mapboxsdk.views.MapView; @@ -35,6 +39,31 @@ public class ReactNativeMapboxGLModule extends ReactContextBaseJavaModule { return "MapboxGLManager"; } + @Override + public @Nullable Map getConstants() { + HashMap constants = new HashMap(); + + HashMap userTrackingMode = new HashMap(); + HashMap mapStyles = new HashMap(); + + // User tracking constants + userTrackingMode.put("none", MyLocationTracking.TRACKING_NONE); + userTrackingMode.put("follow", MyLocationTracking.TRACKING_FOLLOW); + + // Style constants + mapStyles.put("light", Style.LIGHT); + mapStyles.put("dark", Style.DARK); + mapStyles.put("streets", Style.MAPBOX_STREETS); + mapStyles.put("emerald", Style.EMERALD); + mapStyles.put("satellite", Style.SATELLITE); + mapStyles.put("hybrid", Style.SATELLITE_STREETS); + + constants.put("userTrackingMode", userTrackingMode); + constants.put("mapStyles", mapStyles); + + return constants; + } + @ReactMethod public void setDirectionAnimated(String mapRef, int direction) { aPackage.getManager().setDirection(aPackage.getManager().getMapView(), direction); @@ -63,7 +92,7 @@ public class ReactNativeMapboxGLModule extends ReactContextBaseJavaModule { } @ReactMethod - public void setUserTrackingMode(String mapRef, String mode) { + public void setUserTrackingMode(String mapRef, int mode) { aPackage.getManager().setMyLocationTrackingMode(aPackage.getManager().getMapView(), mode); } diff --git a/index.android.js b/index.android.js index ba61e9f..3091dcc 100644 --- a/index.android.js +++ b/index.android.js @@ -27,7 +27,7 @@ var ReactMapView = requireNativeComponent('RCTMapbox', { scrollEnabled: React.PropTypes.bool, showsUserLocation: React.PropTypes.bool, styleUrl: React.PropTypes.string, - UserLocationTrackingMode: React.PropTypes.oneOf(['NONE', 'FOLLOW']), + userTrackingMode: React.PropTypes.number, zoomEnabled: React.PropTypes.bool, zoomLevel: React.PropTypes.number, tilt: React.PropTypes.number, @@ -60,8 +60,8 @@ var ReactMapView = requireNativeComponent('RCTMapbox', { rotationEnabled: true, scrollEnabled: true, showsUserLocation: false, - styleUrl: 'mapbox://styles/mapbox/streets-v8', - UserLocationTrackingMode: 'NONE', + styleUrl: NativeModules.MapboxGLManager.mapStyles.streets, + userTrackingMode: NativeModules.MapboxGLManager.userTrackingMode.none, zoomEnabled: true, zoomLevel: 0, tilt: 0,