mirror of
https://github.com/status-im/react-native-mapbox-gl.git
synced 2026-08-30 20:41:08 +00:00
Use constants allow for custom annotations
This commit is contained in:
+46
-15
@@ -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<MapView> {
|
||||
|
||||
public static final String REACT_CLASS = "RCTMapbox";
|
||||
|
||||
public static final String PROP_ACCESS_TOKEN = "accessToken";
|
||||
@@ -41,13 +57,11 @@ public class ReactNativeMapboxGLManager extends SimpleViewManager<MapView> {
|
||||
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<MapView> {
|
||||
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> {
|
||||
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<MapView> {
|
||||
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<MapView> {
|
||||
}
|
||||
}
|
||||
|
||||
@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)
|
||||
|
||||
+30
-1
@@ -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<String, Object> getConstants() {
|
||||
HashMap<String, Object> constants = new HashMap<String, Object>();
|
||||
|
||||
HashMap<String, Object> userTrackingMode = new HashMap<String, Object>();
|
||||
HashMap<String, Object> mapStyles = new HashMap<String, Object>();
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user