2015-10-11 11:53:56 -07:00
|
|
|
package com.mapbox.reactnativemapboxgl;
|
|
|
|
|
|
|
|
|
|
import android.graphics.Color;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
2015-10-11 20:47:58 -07:00
|
|
|
import com.facebook.react.bridge.ReadableMap;
|
2015-10-24 22:29:40 -07:00
|
|
|
import com.facebook.react.bridge.WritableMap;
|
|
|
|
|
import com.facebook.react.bridge.ReactContext;
|
|
|
|
|
import com.facebook.react.bridge.Arguments;
|
2015-10-11 11:53:56 -07:00
|
|
|
import com.facebook.react.uimanager.CatalystStylesDiffMap;
|
|
|
|
|
import com.facebook.react.uimanager.SimpleViewManager;
|
|
|
|
|
import com.facebook.react.uimanager.ThemedReactContext;
|
|
|
|
|
import com.facebook.react.uimanager.UIProp;
|
|
|
|
|
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
|
|
|
|
|
import com.mapbox.mapboxsdk.annotations.PolygonOptions;
|
2015-10-31 10:19:55 -07:00
|
|
|
import com.mapbox.mapboxsdk.constants.MyLocationTracking;
|
2015-10-11 11:53:56 -07:00
|
|
|
import com.mapbox.mapboxsdk.annotations.PolylineOptions;
|
|
|
|
|
import com.mapbox.mapboxsdk.views.MapView;
|
|
|
|
|
import com.mapbox.mapboxsdk.geometry.LatLng;
|
2015-10-24 22:29:40 -07:00
|
|
|
import com.facebook.react.uimanager.events.RCTEventEmitter;
|
2015-10-11 11:53:56 -07:00
|
|
|
|
2015-10-31 10:19:55 -07:00
|
|
|
import java.util.Map;
|
|
|
|
|
|
2015-10-11 11:53:56 -07:00
|
|
|
public class ReactNativeMapboxGLManager extends SimpleViewManager<MapView> {
|
|
|
|
|
public static final String REACT_CLASS = "RCTMapbox";
|
|
|
|
|
|
|
|
|
|
@UIProp(UIProp.Type.STRING)
|
|
|
|
|
public static final String PROP_ACCESS_TOKEN = "accessToken";
|
|
|
|
|
@UIProp(UIProp.Type.ARRAY)
|
|
|
|
|
public static final String PROP_ANNOTATIONS = "annotations";
|
|
|
|
|
@UIProp(UIProp.Type.MAP)
|
|
|
|
|
public static final String PROP_CENTER_COORDINATE = "centerCoordinate";
|
|
|
|
|
@UIProp(UIProp.Type.BOOLEAN)
|
|
|
|
|
public static final String PROP_DEBUG_ACTIVE = "debugActive";
|
|
|
|
|
@UIProp(UIProp.Type.NUMBER)
|
|
|
|
|
public static final String PROP_DIRECTION = "direction";
|
2015-10-24 22:29:40 -07:00
|
|
|
@UIProp(UIProp.Type.MAP)
|
|
|
|
|
public static final String PROP_ONREGIONCHANGE = "onRegionChange";
|
2015-10-11 11:53:56 -07:00
|
|
|
@UIProp(UIProp.Type.BOOLEAN)
|
|
|
|
|
public static final String PROP_ROTATION_ENABLED = "rotationEnabled";
|
|
|
|
|
@UIProp(UIProp.Type.STRING)
|
|
|
|
|
public static final String PROP_SCROLL_ENABLED = "scrollEnabled";
|
2015-10-11 20:47:58 -07:00
|
|
|
@UIProp(UIProp.Type.BOOLEAN)
|
|
|
|
|
public static final String PROP_USER_LOCATON = "showsUserLocation";
|
2015-10-11 11:53:56 -07:00
|
|
|
@UIProp(UIProp.Type.STRING)
|
|
|
|
|
public static final String PROP_STYLE_URL = "styleUrl";
|
2015-10-11 20:47:58 -07:00
|
|
|
@UIProp(UIProp.Type.STRING)
|
|
|
|
|
public static final String PROP_USER_TRACKING_MODE = "UserLocationTrackingMode";
|
2015-10-11 11:53:56 -07:00
|
|
|
@UIProp(UIProp.Type.BOOLEAN)
|
|
|
|
|
public static final String PROP_ZOOM_ENABLED = "zoomEnabled";
|
|
|
|
|
@UIProp(UIProp.Type.NUMBER)
|
|
|
|
|
public static final String PROP_ZOOM_LEVEL = "zoomLevel";
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String getName() {
|
|
|
|
|
return REACT_CLASS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public MapView createViewInstance(ThemedReactContext context) {
|
2015-10-24 22:29:40 -07:00
|
|
|
MapView mv = new MapView(context, "pk.foo");
|
2015-10-11 11:53:56 -07:00
|
|
|
mv.onCreate(null);
|
|
|
|
|
return mv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void updateView(final MapView view,
|
|
|
|
|
final CatalystStylesDiffMap props) {
|
|
|
|
|
|
2015-10-15 16:05:19 -07:00
|
|
|
if (!props.hasKey(PROP_ACCESS_TOKEN)) {
|
|
|
|
|
Log.e("Error", "No access token provided");
|
|
|
|
|
} else {
|
2015-10-11 11:53:56 -07:00
|
|
|
view.setAccessToken(props.getString(PROP_ACCESS_TOKEN));
|
|
|
|
|
}
|
|
|
|
|
if (props.hasKey(PROP_ANNOTATIONS)) {
|
|
|
|
|
int size = props.getArray(PROP_ANNOTATIONS).size();
|
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
|
ReadableMap annotation = props.getArray(PROP_ANNOTATIONS).getMap(i);
|
|
|
|
|
String type = annotation.getString("type");
|
|
|
|
|
if (type.equals("point")) {
|
|
|
|
|
double latitude = annotation.getArray("coordinates").getDouble(0);
|
|
|
|
|
double longitude = annotation.getArray("coordinates").getDouble(1);
|
|
|
|
|
LatLng markerCenter = new LatLng(latitude, longitude);
|
|
|
|
|
MarkerOptions marker = new MarkerOptions();
|
|
|
|
|
marker.position(markerCenter);
|
2015-10-11 20:47:58 -07:00
|
|
|
if (annotation.hasKey("title")) {
|
|
|
|
|
String title = annotation.getString("title");
|
|
|
|
|
marker.title(title);
|
|
|
|
|
}
|
2015-10-12 18:05:39 -07:00
|
|
|
if (annotation.hasKey("subtitle")) {
|
|
|
|
|
String subtitle = annotation.getString("subtitle");
|
2015-10-11 20:47:58 -07:00
|
|
|
marker.snippet(subtitle);
|
|
|
|
|
}
|
2015-10-11 11:53:56 -07:00
|
|
|
view.addMarker(marker);
|
|
|
|
|
} else if (type.equals("polyline")) {
|
|
|
|
|
int coordSize = annotation.getArray("coordinates").size();
|
|
|
|
|
PolylineOptions polyline = new PolylineOptions();
|
|
|
|
|
for (int p = 0; p < coordSize; p++) {
|
|
|
|
|
double latitude = annotation.getArray("coordinates").getArray(p).getDouble(0);
|
|
|
|
|
double longitude = annotation.getArray("coordinates").getArray(p).getDouble(1);
|
|
|
|
|
polyline.add(new LatLng(latitude, longitude));
|
|
|
|
|
}
|
2015-10-11 20:47:58 -07:00
|
|
|
if (annotation.hasKey("alpha")) {
|
|
|
|
|
double strokeAlpha = annotation.getDouble("alpha");
|
|
|
|
|
polyline.alpha((float) strokeAlpha);
|
|
|
|
|
}
|
|
|
|
|
if (annotation.hasKey("strokeColor")) {
|
|
|
|
|
int strokeColor = Color.parseColor(annotation.getString("strokeColor"));
|
|
|
|
|
polyline.color(strokeColor);
|
|
|
|
|
}
|
|
|
|
|
if (annotation.hasKey("strokeWidth")) {
|
|
|
|
|
float strokeWidth = annotation.getInt("strokeWidth");
|
|
|
|
|
polyline.width(strokeWidth);
|
|
|
|
|
}
|
2015-10-11 11:53:56 -07:00
|
|
|
view.addPolyline(polyline);
|
|
|
|
|
} else if (type.equals("polygon")) {
|
|
|
|
|
int coordSize = annotation.getArray("coordinates").size();
|
|
|
|
|
PolygonOptions polygon = new PolygonOptions();
|
|
|
|
|
for (int p = 0; p < coordSize; p++) {
|
|
|
|
|
double latitude = annotation.getArray("coordinates").getArray(p).getDouble(0);
|
|
|
|
|
double longitude = annotation.getArray("coordinates").getArray(p).getDouble(1);
|
|
|
|
|
polygon.add(new LatLng(latitude, longitude));
|
|
|
|
|
}
|
2015-10-11 20:47:58 -07:00
|
|
|
if (annotation.hasKey("alpha")) {
|
|
|
|
|
double fillAlpha = annotation.getDouble("alpha");
|
|
|
|
|
polygon.alpha((float) fillAlpha);
|
|
|
|
|
}
|
|
|
|
|
if (annotation.hasKey("fillColor")) {
|
|
|
|
|
int fillColor = Color.parseColor(annotation.getString("fillColor"));
|
|
|
|
|
polygon.fillColor(fillColor);
|
|
|
|
|
}
|
|
|
|
|
if (annotation.hasKey("strokeColor")) {
|
|
|
|
|
int strokeColor = Color.parseColor(annotation.getString("strokeColor"));
|
|
|
|
|
polygon.strokeColor(strokeColor);
|
|
|
|
|
}
|
2015-10-11 11:53:56 -07:00
|
|
|
view.addPolygon(polygon);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (props.hasKey(PROP_DEBUG_ACTIVE)) {
|
|
|
|
|
view.setDebugActive(props.getBoolean(PROP_DEBUG_ACTIVE, false));
|
|
|
|
|
}
|
|
|
|
|
if (props.hasKey(PROP_DIRECTION)) {
|
|
|
|
|
view.setDirection(props.getFloat(PROP_DIRECTION, 0));
|
|
|
|
|
}
|
2015-10-24 22:29:40 -07:00
|
|
|
if (props.hasKey(PROP_ONREGIONCHANGE)) {
|
|
|
|
|
view.addOnMapChangedListener(new MapView.OnMapChangedListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onMapChanged(int change) {
|
2015-10-31 10:19:55 -07:00
|
|
|
if (change == MapView.REGION_DID_CHANGE || change == MapView.REGION_DID_CHANGE_ANIMATED) {
|
2015-10-24 22:29:40 -07:00
|
|
|
WritableMap event = Arguments.createMap();
|
|
|
|
|
WritableMap location = Arguments.createMap();
|
|
|
|
|
location.putDouble("latitude", view.getCenterCoordinate().getLatitude());
|
|
|
|
|
location.putDouble("longitude", view.getCenterCoordinate().getLongitude());
|
|
|
|
|
location.putDouble("zoom", view.getZoomLevel());
|
|
|
|
|
event.putMap("src", location);
|
|
|
|
|
ReactContext reactContext = (ReactContext) view.getContext();
|
|
|
|
|
reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(view.getId(), "topChange", event);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2015-10-11 11:53:56 -07:00
|
|
|
if (props.hasKey(PROP_CENTER_COORDINATE)) {
|
|
|
|
|
ReadableMap center = props.getMap(PROP_CENTER_COORDINATE);
|
|
|
|
|
double latitude = center.getDouble("latitude");
|
|
|
|
|
double longitude = center.getDouble("longitude");
|
|
|
|
|
view.setCenterCoordinate(new LatLng(latitude, longitude));
|
|
|
|
|
}
|
|
|
|
|
if (props.hasKey(PROP_ROTATION_ENABLED)) {
|
|
|
|
|
view.setRotateEnabled(props.getBoolean(PROP_ROTATION_ENABLED, true));
|
|
|
|
|
}
|
2015-10-11 20:47:58 -07:00
|
|
|
if (props.hasKey(PROP_USER_LOCATON)) {
|
|
|
|
|
view.setMyLocationEnabled(props.getBoolean(PROP_USER_LOCATON, true));
|
2015-10-11 11:53:56 -07:00
|
|
|
}
|
|
|
|
|
if (props.hasKey(PROP_STYLE_URL)) {
|
|
|
|
|
view.setStyleUrl(props.getString(PROP_STYLE_URL));
|
|
|
|
|
}
|
2015-10-24 22:29:40 -07:00
|
|
|
if (props.hasKey(PROP_USER_TRACKING_MODE)) {
|
|
|
|
|
String mode = props.getString(PROP_USER_TRACKING_MODE);
|
|
|
|
|
if (mode.equals("NONE")) {
|
2015-10-31 10:19:55 -07:00
|
|
|
view.setMyLocationTrackingMode(MyLocationTracking.TRACKING_NONE);
|
2015-10-24 22:29:40 -07:00
|
|
|
} else if (mode.equals("FOLLOW")) {
|
2015-10-31 10:19:55 -07:00
|
|
|
view.setMyLocationTrackingMode(MyLocationTracking.TRACKING_FOLLOW);
|
2015-10-24 22:29:40 -07:00
|
|
|
} else {
|
2015-10-31 10:19:55 -07:00
|
|
|
view.setMyLocationTrackingMode(MyLocationTracking.TRACKING_NONE);
|
2015-10-24 22:29:40 -07:00
|
|
|
Log.w("Error", "Tracking mode not found. Setting to NONE.");
|
|
|
|
|
}
|
2015-10-31 10:19:55 -07:00
|
|
|
}
|
2015-10-24 22:29:40 -07:00
|
|
|
if (props.hasKey(PROP_ZOOM_ENABLED)) {
|
2015-10-11 11:53:56 -07:00
|
|
|
view.setZoomEnabled(props.getBoolean(PROP_ZOOM_ENABLED, true));
|
|
|
|
|
}
|
|
|
|
|
if (props.hasKey(PROP_ZOOM_LEVEL)) {
|
|
|
|
|
view.setZoomLevel(props.getFloat(PROP_ZOOM_LEVEL, 0));
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-24 22:29:40 -07:00
|
|
|
if (props.hasKey(PROP_SCROLL_ENABLED)) {
|
|
|
|
|
view.setScrollEnabled(props.getBoolean(PROP_SCROLL_ENABLED, true));
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-11 11:53:56 -07:00
|
|
|
super.updateView(view, props);
|
2015-10-11 20:47:58 -07:00
|
|
|
}
|
2015-10-11 11:53:56 -07:00
|
|
|
}
|