Files
react-native-mapbox-gl/android/src/main/java/com/mapbox/reactnativemapboxgl/ReactNativeMapboxGLView.java
T

628 lines
22 KiB
Java
Raw Normal View History

package com.mapbox.reactnativemapboxgl;
import android.content.Context;
2016-07-02 18:34:06 +03:00
import android.graphics.PointF;
import android.location.Location;
import android.support.annotation.NonNull;
import android.support.annotation.UiThread;
import android.widget.RelativeLayout;
2016-07-02 18:34:06 +03:00
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.LifecycleEventListener;
2016-07-02 18:34:06 +03:00
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.uimanager.events.RCTEventEmitter;
2016-07-02 21:19:03 +03:00
import com.mapbox.mapboxsdk.annotations.Annotation;
import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.annotations.PolygonOptions;
import com.mapbox.mapboxsdk.annotations.PolylineOptions;
2016-06-30 20:25:01 +03:00
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.camera.CameraUpdate;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
2016-06-30 20:25:01 +03:00
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
2016-06-30 20:25:01 +03:00
import com.mapbox.mapboxsdk.maps.MapboxMapOptions;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
2016-07-01 02:57:35 +03:00
import com.mapbox.mapboxsdk.maps.UiSettings;
2016-07-02 21:19:03 +03:00
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nullable;
2016-07-02 18:34:06 +03:00
public class ReactNativeMapboxGLView extends RelativeLayout implements
OnMapReadyCallback, LifecycleEventListener,
MapboxMap.OnMapClickListener, MapboxMap.OnMapLongClickListener,
MapboxMap.OnMyBearingTrackingModeChangeListener, MapboxMap.OnMyLocationTrackingModeChangeListener,
MapboxMap.OnMyLocationChangeListener,
2016-07-02 21:55:55 +03:00
MapboxMap.OnMarkerClickListener, MapboxMap.OnInfoWindowClickListener,
2016-07-02 18:34:06 +03:00
MapView.OnMapChangedListener
{
private MapboxMap _map = null;
2016-06-30 20:25:01 +03:00
private MapView _mapView = null;
private ReactNativeMapboxGLManager _manager;
private boolean _paused = false;
2016-06-30 20:25:01 +03:00
private CameraPosition.Builder _initialCamera = new CameraPosition.Builder();
private MapboxMapOptions _mapOptions;
private int _locationTrackingMode;
private int _bearingTrackingMode;
2016-07-02 18:34:06 +03:00
private boolean _trackingModeUpdateScheduled = false;
2016-06-30 20:25:01 +03:00
private boolean _showsUserLocation;
2016-07-01 12:57:58 +03:00
private boolean _zoomEnabled = true;
private boolean _scrollEnabled = true;
private boolean _rotateEnabled = true;
private boolean _enableOnRegionWillChange = false;
private boolean _enableOnRegionDidChange = false;
2016-06-30 20:48:01 +03:00
private int _paddingTop, _paddingRight, _paddingBottom, _paddingLeft;
2016-07-02 21:19:03 +03:00
private Map<String, Annotation> _annotations = new HashMap();
2016-07-02 21:55:55 +03:00
private Map<Long, String> _annotationIdsToName = new HashMap();
2016-07-02 21:19:03 +03:00
private Map<String, MarkerOptions> _markerOptions = new HashMap();
private Map<String, PolylineOptions> _polylineOptions = new HashMap();
private Map<String, PolygonOptions> _polygonOptions = new HashMap();
2016-07-02 18:34:06 +03:00
private android.os.Handler _trackingModeHandler;
@UiThread
public ReactNativeMapboxGLView(Context context, ReactNativeMapboxGLManager manager) {
super(context);
2016-07-02 18:34:06 +03:00
_trackingModeHandler = new android.os.Handler();
_manager = manager;
2016-06-30 20:25:01 +03:00
_mapOptions = MapboxMapOptions.createFromAttributes(context, null);
2016-07-01 12:57:58 +03:00
_mapOptions.zoomGesturesEnabled(true);
_mapOptions.rotateGesturesEnabled(true);
_mapOptions.scrollGesturesEnabled(true);
_mapOptions.tiltGesturesEnabled(true);
}
// Lifecycle methods
2016-07-02 18:51:09 +03:00
public void onAfterUpdateTransaction() {
if (_mapView != null) { return; }
setupMapView();
_paused = false;
2016-06-30 20:25:01 +03:00
_mapView.onResume();
_manager.getContext().addLifecycleEventListener(this);
}
2016-07-02 18:51:09 +03:00
public void onDrop() {
if (_mapView == null) { return; }
_manager.getContext().removeLifecycleEventListener(this);
2016-07-02 18:51:09 +03:00
if (!_paused) {
_paused = true;
_mapView.onPause();
}
destroyMapView();
_mapView = null;
}
@Override
public void onHostResume() {
_paused = false;
2016-06-30 20:25:01 +03:00
_mapView.onResume();
}
@Override
public void onHostPause() {
_paused = true;
2016-06-30 20:25:01 +03:00
_mapView.onPause();
}
@Override
public void onHostDestroy() {
2016-07-02 18:51:09 +03:00
onDrop();
2016-06-30 20:25:01 +03:00
}
// Initialization
private void setupMapView() {
_mapOptions.camera(_initialCamera.build());
_mapView = new MapView(this.getContext(), _mapOptions);
this.addView(_mapView);
2016-07-02 18:34:06 +03:00
_mapView.addOnMapChangedListener(this);
_mapView.onCreate(null);
_mapView.getMapAsync(this);
}
@Override
public void onMapReady(MapboxMap mapboxMap) {
2016-07-02 18:51:09 +03:00
if (_mapView == null) { return; }
_map = mapboxMap;
2016-07-02 18:34:06 +03:00
// Configure map
_map.setMyLocationEnabled(_showsUserLocation);
2016-06-30 20:25:01 +03:00
_map.getTrackingSettings().setMyLocationTrackingMode(_locationTrackingMode);
_map.getTrackingSettings().setMyBearingTrackingMode(_bearingTrackingMode);
2016-06-30 20:48:01 +03:00
_map.setPadding(_paddingLeft, _paddingTop, _paddingRight, _paddingBottom);
2016-07-01 12:57:58 +03:00
UiSettings uiSettings = _map.getUiSettings();
uiSettings.setZoomGesturesEnabled(_zoomEnabled);
uiSettings.setScrollGesturesEnabled(_scrollEnabled);
uiSettings.setRotateGesturesEnabled(_rotateEnabled);
2016-06-30 20:25:01 +03:00
// If these settings changed between setupMapView() and onMapReady(), coerce them to their right values
2016-07-01 02:57:35 +03:00
// This doesn't happen in the current implementation of MapView, but let's be future proof
2016-06-30 20:25:01 +03:00
if (_map.isDebugActive() != _mapOptions.getDebugActive()) {
_map.setDebugActive(_mapOptions.getDebugActive());
}
if (!_map.getStyleUrl().equals(_mapOptions.getStyle())) {
_map.setStyleUrl(_mapOptions.getStyle());
}
2016-07-01 02:57:35 +03:00
if (uiSettings.isLogoEnabled() != _mapOptions.getLogoEnabled()) {
uiSettings.setLogoEnabled(_mapOptions.getLogoEnabled());
}
if (uiSettings.isAttributionEnabled() != _mapOptions.getAttributionEnabled()) {
uiSettings.setAttributionEnabled(_mapOptions.getAttributionEnabled());
}
if (uiSettings.isCompassEnabled() != _mapOptions.getCompassEnabled()) {
uiSettings.setCompassEnabled(_mapOptions.getCompassEnabled());
}
2016-07-02 18:34:06 +03:00
// Attach listeners
_map.setOnMapClickListener(this);
_map.setOnMapLongClickListener(this);
_map.setOnMyLocationTrackingModeChangeListener(this);
_map.setOnMyBearingTrackingModeChangeListener(this);
_map.setOnMyLocationChangeListener(this);
2016-07-02 21:55:55 +03:00
_map.setOnMarkerClickListener(this);
_map.setOnInfoWindowClickListener(this);
2016-07-02 21:19:03 +03:00
// Create annotations
for (Map.Entry<String, MarkerOptions> entry : _markerOptions.entrySet()) {
2016-07-02 21:55:55 +03:00
Annotation annotation = _map.addMarker(entry.getValue());
_annotations.put(entry.getKey(), annotation);
_annotationIdsToName.put(annotation.getId(), entry.getKey());
2016-07-02 21:19:03 +03:00
}
_markerOptions.clear();
for (Map.Entry<String, PolylineOptions> entry : _polylineOptions.entrySet()) {
2016-07-02 21:55:55 +03:00
Annotation annotation = _map.addPolyline(entry.getValue());
_annotations.put(entry.getKey(), annotation);
_annotationIdsToName.put(annotation.getId(), entry.getKey());
2016-07-02 21:19:03 +03:00
}
_polylineOptions.clear();
for (Map.Entry<String, PolygonOptions> entry : _polygonOptions.entrySet()) {
2016-07-02 21:55:55 +03:00
Annotation annotation = _map.addPolygon(entry.getValue());
_annotations.put(entry.getKey(), annotation);
_annotationIdsToName.put(annotation.getId(), entry.getKey());
2016-07-02 21:19:03 +03:00
}
_polygonOptions.clear();
2016-06-30 20:25:01 +03:00
}
2016-07-02 18:51:09 +03:00
private void destroyMapView() {
_mapView.removeOnMapChangedListener(this);
if (_map != null) {
_map.setOnMapClickListener(null);
_map.setOnMapLongClickListener(null);
_map.setOnMyLocationTrackingModeChangeListener(null);
_map.setOnMyBearingTrackingModeChangeListener(null);
_map.setOnMyLocationChangeListener(null);
2016-07-02 21:55:55 +03:00
_map.setOnMarkerClickListener(this);
_map.setOnInfoWindowClickListener(this);
2016-07-02 18:51:09 +03:00
_map = null;
}
_mapView.onDestroy();
}
// Props
2016-06-30 20:25:01 +03:00
public void setInitialZoomLevel(double value) {
_initialCamera.zoom(value);
}
public void setInitialDirection(double value) {
_initialCamera.bearing(value);
}
public void setInitialCenterCoordinate(double lat, double lon) {
_initialCamera.target(new LatLng(lat, lon));
}
public void setEnableOnRegionDidChange(boolean value) {
_enableOnRegionDidChange = value;
}
public void setEnableOnRegionWillChange(boolean value) {
_enableOnRegionWillChange = value;
}
public void setShowsUserLocation(boolean value) {
2016-06-30 20:25:01 +03:00
if (_showsUserLocation == value) { return; }
_showsUserLocation = value;
if (_map != null) { _map.setMyLocationEnabled(value); }
}
2016-06-30 20:25:01 +03:00
public void setRotateEnabled(boolean value) {
2016-07-01 12:57:58 +03:00
if (_rotateEnabled == value) { return; }
_rotateEnabled = value;
2016-07-01 02:46:59 +03:00
if (_map != null) {
_map.getUiSettings().setRotateGesturesEnabled(value);
}
2016-06-30 20:25:01 +03:00
}
public void setScrollEnabled(boolean value) {
2016-07-01 12:57:58 +03:00
if (_scrollEnabled == value) { return; }
_scrollEnabled = value;
2016-07-01 02:46:59 +03:00
if (_map != null) {
_map.getUiSettings().setScrollGesturesEnabled(value);
}
2016-06-30 20:25:01 +03:00
}
public void setZoomEnabled(boolean value) {
2016-07-01 12:57:58 +03:00
if (_zoomEnabled == value) { return; }
_zoomEnabled = value;
2016-07-01 02:46:59 +03:00
if (_map != null) {
_map.getUiSettings().setZoomGesturesEnabled(value);
}
2016-06-30 20:25:01 +03:00
}
public void setStyleURL(String styleURL) {
if (styleURL.equals(_mapOptions.getStyle())) { return; }
_mapOptions.styleUrl(styleURL);
if (_map != null) { _map.setStyleUrl(styleURL); }
}
public void setDebugActive(boolean value) {
if (_mapOptions.getDebugActive() == value) { return; }
_mapOptions.debugActive(value);
if (_map != null) { _map.setDebugActive(value); };
}
public void setLocationTracking(int value) {
if (_locationTrackingMode == value) { return; }
_locationTrackingMode = value;
if (_map != null) { _map.getTrackingSettings().setMyLocationTrackingMode(value); };
}
public void setBearingTracking(int value) {
if (_bearingTrackingMode == value) { return; }
_bearingTrackingMode = value;
if (_map != null) { _map.getTrackingSettings().setMyBearingTrackingMode(value); };
}
public void setAttributionButtonIsHidden(boolean value) {
if (_mapOptions.getAttributionEnabled() == !value) { return; }
_mapOptions.attributionEnabled(!value);
2016-07-01 02:46:59 +03:00
if (_map != null) {
_map.getUiSettings().setAttributionEnabled(!value);
}
2016-06-30 20:25:01 +03:00
}
public void setLogoIsHidden(boolean value) {
if (_mapOptions.getLogoEnabled() == !value) { return; }
_mapOptions.logoEnabled(!value);
2016-07-01 02:46:59 +03:00
if (_map != null) {
_map.getUiSettings().setLogoEnabled(!value);
}
2016-06-30 20:25:01 +03:00
}
public void setCompassIsHidden(boolean value) {
if (_mapOptions.getCompassEnabled() == !value) { return; }
_mapOptions.compassEnabled(!value);
2016-07-01 02:46:59 +03:00
if (_map != null) {
_map.getUiSettings().setCompassEnabled(!value);
}
2016-06-30 20:25:01 +03:00
}
2016-06-30 20:48:01 +03:00
public void setContentInset(int top, int right, int bottom, int left) {
if (top == _paddingTop &&
bottom == _paddingBottom &&
left == _paddingLeft &&
right == _paddingRight) { return; }
_paddingTop = top;
_paddingRight = right;
_paddingBottom = bottom;
_paddingLeft = left;
if (_map != null) { _map.setPadding(left, top, right, bottom); }
}
2016-07-02 18:34:06 +03:00
// Events
void emitEvent(String name, @Nullable WritableMap event) {
if (event == null) {
event = Arguments.createMap();
}
((ReactContext)getContext())
.getJSModule(RCTEventEmitter.class)
2016-07-02 19:04:23 +03:00
.receiveEvent(getId(), name, event);
2016-07-02 18:34:06 +03:00
}
2016-07-02 21:55:55 +03:00
WritableMap serializePoint(LatLng point) {
2016-07-02 18:34:06 +03:00
PointF screenCoords = _map.getProjection().toScreenLocation(point);
WritableMap event = Arguments.createMap();
WritableMap src = Arguments.createMap();
src.putDouble("latitude", point.getLatitude());
src.putDouble("longitude", point.getLongitude());
src.putDouble("screenCoordX", screenCoords.x);
src.putDouble("screenCoordY", screenCoords.y);
event.putMap("src", src);
2016-07-02 21:55:55 +03:00
return event;
}
2016-07-02 18:34:06 +03:00
2016-07-02 21:55:55 +03:00
@Override
public void onMapClick(LatLng point) {
emitEvent("onTap", serializePoint(point));
2016-07-02 18:34:06 +03:00
}
@Override
public void onMapLongClick(@NonNull LatLng point) {
2016-07-02 21:55:55 +03:00
emitEvent("onLongPress", serializePoint(point));
2016-07-02 18:34:06 +03:00
}
@Override
public void onMyLocationChange(@Nullable Location location) {
WritableMap event = Arguments.createMap();
WritableMap src = Arguments.createMap();
if (location == null) {
src.putString("message", "Could not get user location");
event.putMap("src", src);
emitEvent("onLocateUserFailed", event);
return;
}
src.putDouble("latitude", location.getLatitude());
src.putDouble("longitude", location.getLongitude());
src.putDouble("magneticHeading", location.getBearing());
src.putDouble("trueHeading", location.getBearing());
src.putDouble("headingAccuracy", 10.0);
src.putBoolean("isUpdating", true);
event.putMap("src", src);
emitEvent("onUpdateUserLocation", event);
}
class TrackingModeChangeRunnable implements Runnable {
ReactNativeMapboxGLView target;
TrackingModeChangeRunnable(ReactNativeMapboxGLView target) {
this.target = target;
}
@Override
public void run() {
target.onTrackingModeChange();
}
}
public void onTrackingModeChange() {
if (!_trackingModeUpdateScheduled) { return; }
_trackingModeUpdateScheduled = false;
for (int mode = 0; mode < ReactNativeMapboxGLModule.locationTrackingModes.length; mode++) {
if (_locationTrackingMode == ReactNativeMapboxGLModule.locationTrackingModes[mode] &&
_bearingTrackingMode == ReactNativeMapboxGLModule.bearingTrackingModes[mode]) {
WritableMap event = Arguments.createMap();
event.putInt("src", mode);
emitEvent("onChangeUserTrackingMode", event);
break;
}
}
}
@Override
@UiThread
public void onMyBearingTrackingModeChange(int myBearingTrackingMode) {
if (_bearingTrackingMode == myBearingTrackingMode) { return; }
_bearingTrackingMode = myBearingTrackingMode;
_trackingModeUpdateScheduled = true;
_trackingModeHandler.post(new TrackingModeChangeRunnable(this));
}
@Override
@UiThread
public void onMyLocationTrackingModeChange(int myLocationTrackingMode) {
if (_locationTrackingMode == myLocationTrackingMode) { return; }
_locationTrackingMode = myLocationTrackingMode;
_trackingModeUpdateScheduled = true;
_trackingModeHandler.post(new TrackingModeChangeRunnable(this));
}
WritableMap serializeCurrentRegion() {
CameraPosition camera = _map == null
? _initialCamera.build()
: _map.getCameraPosition();
WritableMap event = Arguments.createMap();
WritableMap src = Arguments.createMap();
src.putDouble("longitude", camera.target.getLongitude());
src.putDouble("latitude", camera.target.getLatitude());
src.putDouble("zoomLevel", camera.zoom);
src.putDouble("direction", camera.bearing);
src.putDouble("pitch", camera.tilt);
event.putMap("src", src);
return event;
}
@Override
public void onMapChanged(int change) {
switch (change) {
case MapView.REGION_WILL_CHANGE:
case MapView.REGION_WILL_CHANGE_ANIMATED:
if (_enableOnRegionWillChange) {
emitEvent("onRegionWillChange", serializeCurrentRegion()); // TODO: These should be throttled
}
2016-07-02 18:34:06 +03:00
break;
case MapView.REGION_DID_CHANGE:
case MapView.REGION_DID_CHANGE_ANIMATED:
if (_enableOnRegionDidChange) {
emitEvent("onRegionDidChange", serializeCurrentRegion());
}
2016-07-02 18:34:06 +03:00
break;
case MapView.WILL_START_LOADING_MAP:
emitEvent("onStartLoadingMap", null);
break;
case MapView.DID_FINISH_LOADING_MAP:
emitEvent("onFinishLoadingMap", null);
break;
}
}
2016-07-02 21:55:55 +03:00
WritableMap serializeMarker(Marker marker) {
WritableMap event = Arguments.createMap();
WritableMap src = Arguments.createMap();
src.putString("id", _annotationIdsToName.get(marker.getId()));
src.putDouble("longitude", marker.getPosition().getLongitude());
src.putDouble("latitude", marker.getPosition().getLatitude());
src.putString("title", marker.getTitle());
src.putString("subtitle", marker.getSnippet());
event.putMap("src", src);
return event;
}
@Override
public boolean onInfoWindowClick(@NonNull Marker marker) {
emitEvent("onRightAnnotationTapped", serializeMarker(marker));
return false;
}
@Override
public boolean onMarkerClick(@NonNull Marker marker) {
emitEvent("onOpenAnnotation", serializeMarker(marker));
return false;
}
2016-07-01 02:57:35 +03:00
// Getters
public CameraPosition getCameraPosition() {
if (_map == null) { return _initialCamera.build(); }
return _map.getCameraPosition();
}
public LatLngBounds getBounds() {
if (_map == null) { return new LatLngBounds.Builder().build(); };
return _map.getProjection().getVisibleRegion().latLngBounds;
}
2016-07-01 02:57:35 +03:00
// Camera setters
public void setCameraPosition(CameraPosition position, int duration, @Nullable Runnable callback) {
if (_map == null) {
_initialCamera = new CameraPosition.Builder(position);
if (callback != null) { callback.run(); }
return;
}
CameraUpdate update = CameraUpdateFactory.newCameraPosition(position);
setCameraUpdate(update, duration, callback);
}
public void setCameraUpdate(CameraUpdate update, int duration, @Nullable Runnable callback) {
if (_map == null) {
return;
}
if (duration == 0) {
_map.moveCamera(update);
if (callback != null) { callback.run(); }
} else {
// Ugh... Java callbacks suck
class CameraCallback implements MapboxMap.CancelableCallback {
Runnable callback;
CameraCallback(Runnable callback) {
this.callback = callback;
}
@Override
public void onCancel() {
if (callback != null) { callback.run(); }
}
@Override
public void onFinish() {
if (callback != null) { callback.run(); }
}
}
_map.animateCamera(update, duration, new CameraCallback(callback));
}
}
2016-07-02 21:19:03 +03:00
// Annotations
@Nullable Annotation _removeAnnotation(String name, boolean keep) {
if (_map == null) {
_markerOptions.remove(name);
_polylineOptions.remove(name);
_polygonOptions.remove(name);
return null;
}
Annotation annotation = _annotations.remove(name);
if (annotation == null) { return null; }
2016-07-02 21:55:55 +03:00
_annotationIdsToName.remove(annotation.getId());
2016-07-02 21:19:03 +03:00
if (keep) { return annotation; }
_map.removeAnnotation(annotation);
return null;
}
public void removeAnnotation(String name) {
_removeAnnotation(name, false);
}
public void removeAllAnnotations() {
_markerOptions.clear();
_polygonOptions.clear();
_polygonOptions.clear();
_annotations.clear();
2016-07-02 21:55:55 +03:00
_annotationIdsToName.clear();
2016-07-02 21:19:03 +03:00
if (_map != null) {
_map.removeAnnotations();
}
}
public void setMarker(String name, MarkerOptions options) {
Annotation removed = _removeAnnotation(name, true);
if (_map == null) {
_markerOptions.put(name, options);
} else {
Annotation annotation = _map.addMarker(options);
_annotations.put(name, annotation);
2016-07-02 21:55:55 +03:00
_annotationIdsToName.put(annotation.getId(), name);
2016-07-02 21:19:03 +03:00
}
if (removed != null) { _map.removeAnnotation(removed); }
}
public void setPolyline(String name, PolylineOptions options) {
Annotation removed = _removeAnnotation(name, true);
if (_map == null) {
_polylineOptions.put(name, options);
} else {
Annotation annotation = _map.addPolyline(options);
_annotations.put(name, annotation);
2016-07-02 21:55:55 +03:00
_annotationIdsToName.put(annotation.getId(), name);
2016-07-02 21:19:03 +03:00
}
if (removed != null) { _map.removeAnnotation(removed); }
}
public void setPolygon(String name, PolygonOptions options) {
Annotation removed = _removeAnnotation(name, true);
if (_map == null) {
_polygonOptions.put(name, options);
} else {
Annotation annotation = _map.addPolygon(options);
_annotations.put(name, annotation);
2016-07-02 21:55:55 +03:00
_annotationIdsToName.put(annotation.getId(), name);
2016-07-02 21:19:03 +03:00
}
if (removed != null) { _map.removeAnnotation(removed); }
}
public void selectAnnotation(String name, boolean animated) {
if (_map == null) { return; }
Annotation annotation = _annotations.get(name);
if (annotation == null) { return; }
if (!(annotation instanceof Marker)) { return; }
Marker marker = (Marker)annotation;
_map.selectMarker(marker);
}
}