2016-06-30 16:59:10 +03:00
|
|
|
package com.mapbox.reactnativemapboxgl;
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
2016-06-30 20:25:01 +03:00
|
|
|
import android.widget.LinearLayout;
|
2016-06-30 16:59:10 +03:00
|
|
|
|
2016-06-30 21:31:59 +03:00
|
|
|
import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
|
2016-06-30 16:59:10 +03:00
|
|
|
import com.facebook.react.bridge.LifecycleEventListener;
|
2016-06-30 20:25:01 +03:00
|
|
|
import com.mapbox.mapboxsdk.camera.CameraPosition;
|
|
|
|
|
import com.mapbox.mapboxsdk.geometry.LatLng;
|
2016-06-30 16:59:10 +03:00
|
|
|
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;
|
2016-06-30 16:59:10 +03:00
|
|
|
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
|
|
|
|
|
|
2016-06-30 20:25:01 +03:00
|
|
|
public class ReactNativeMapboxGLView extends LinearLayout implements OnMapReadyCallback, LifecycleEventListener {
|
2016-06-30 16:59:10 +03:00
|
|
|
|
|
|
|
|
private MapboxMap _map = null;
|
2016-06-30 20:25:01 +03:00
|
|
|
private MapView _mapView = null;
|
2016-06-30 16:59:10 +03:00
|
|
|
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;
|
|
|
|
|
private boolean _showsUserLocation;
|
2016-06-30 20:48:01 +03:00
|
|
|
private int _paddingTop, _paddingRight, _paddingBottom, _paddingLeft;
|
2016-06-30 16:59:10 +03:00
|
|
|
|
|
|
|
|
public ReactNativeMapboxGLView(Context context, ReactNativeMapboxGLManager manager) {
|
|
|
|
|
super(context);
|
|
|
|
|
_manager = manager;
|
2016-06-30 20:25:01 +03:00
|
|
|
_mapOptions = MapboxMapOptions.createFromAttributes(context, null);
|
2016-06-30 16:59:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Lifecycle methods
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onAttachedToWindow () {
|
|
|
|
|
super.onAttachedToWindow();
|
2016-06-30 20:25:01 +03:00
|
|
|
if (_mapView == null) {
|
|
|
|
|
setupMapView();
|
|
|
|
|
}
|
|
|
|
|
_mapView.onCreate(null);
|
2016-06-30 16:59:10 +03:00
|
|
|
_paused = false;
|
2016-06-30 20:25:01 +03:00
|
|
|
_mapView.onResume();
|
2016-06-30 16:59:10 +03:00
|
|
|
_manager.getContext().addLifecycleEventListener(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onDetachedFromWindow () {
|
|
|
|
|
super.onDetachedFromWindow();
|
|
|
|
|
if (!_paused) {
|
|
|
|
|
_paused = true;
|
2016-06-30 20:25:01 +03:00
|
|
|
_mapView.onPause();
|
2016-06-30 16:59:10 +03:00
|
|
|
}
|
2016-06-30 20:25:01 +03:00
|
|
|
_mapView.onDestroy();
|
2016-06-30 16:59:10 +03:00
|
|
|
_manager.getContext().removeLifecycleEventListener(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onHostResume() {
|
|
|
|
|
_paused = false;
|
2016-06-30 20:25:01 +03:00
|
|
|
_mapView.onResume();
|
2016-06-30 16:59:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onHostPause() {
|
|
|
|
|
_paused = true;
|
2016-06-30 20:25:01 +03:00
|
|
|
_mapView.onPause();
|
2016-06-30 16:59:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onHostDestroy() {
|
2016-06-30 20:25:01 +03:00
|
|
|
_mapView.onDestroy();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Initialization
|
|
|
|
|
|
|
|
|
|
private void setupMapView() {
|
|
|
|
|
_mapOptions.camera(_initialCamera.build());
|
|
|
|
|
_mapView = new MapView(this.getContext(), _mapOptions);
|
|
|
|
|
_mapView.getMapAsync(this);
|
|
|
|
|
this.addView(_mapView);
|
2016-06-30 16:59:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onMapReady(MapboxMap mapboxMap) {
|
|
|
|
|
_map = mapboxMap;
|
|
|
|
|
_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-06-30 20:25:01 +03:00
|
|
|
|
|
|
|
|
// If these settings changed between setupMapView() and onMapReady(), coerce them to their right values
|
|
|
|
|
if (_map.isDebugActive() != _mapOptions.getDebugActive()) {
|
|
|
|
|
_map.setDebugActive(_mapOptions.getDebugActive());
|
|
|
|
|
}
|
|
|
|
|
if (!_map.getStyleUrl().equals(_mapOptions.getStyle())) {
|
|
|
|
|
_map.setStyleUrl(_mapOptions.getStyle());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Utils
|
|
|
|
|
|
2016-06-30 21:31:59 +03:00
|
|
|
private void assertPropNotChangeable(String propName) {
|
2016-06-30 20:25:01 +03:00
|
|
|
if (_mapView != null) {
|
2016-06-30 21:31:59 +03:00
|
|
|
throw new JSApplicationIllegalArgumentException("Changing prop MapView." + propName +
|
|
|
|
|
" after component has been mounted is not currently supported");
|
2016-06-30 20:25:01 +03:00
|
|
|
}
|
2016-06-30 16:59:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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));
|
2016-06-30 16:59:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setShowsUserLocation(boolean value) {
|
2016-06-30 20:25:01 +03:00
|
|
|
if (_showsUserLocation == value) { return; }
|
2016-06-30 16:59:10 +03:00
|
|
|
_showsUserLocation = value;
|
|
|
|
|
if (_map != null) { _map.setMyLocationEnabled(value); }
|
|
|
|
|
}
|
2016-06-30 20:25:01 +03:00
|
|
|
|
|
|
|
|
public void setRotateEnabled(boolean value) {
|
|
|
|
|
if (_mapOptions.getRotateGesturesEnabled() == value) { return; }
|
|
|
|
|
_mapOptions.rotateGesturesEnabled(value);
|
2016-06-30 21:31:59 +03:00
|
|
|
assertPropNotChangeable("rotateEnabled");
|
2016-06-30 20:25:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setScrollEnabled(boolean value) {
|
|
|
|
|
if (_mapOptions.getScrollGesturesEnabled() == value) { return; }
|
|
|
|
|
_mapOptions.scrollGesturesEnabled(value);
|
2016-06-30 21:31:59 +03:00
|
|
|
assertPropNotChangeable("scrollEnabled");
|
2016-06-30 20:25:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setZoomEnabled(boolean value) {
|
|
|
|
|
if (_mapOptions.getZoomGesturesEnabled() == value) { return; }
|
|
|
|
|
_mapOptions.zoomGesturesEnabled(value);
|
2016-06-30 21:31:59 +03:00
|
|
|
assertPropNotChangeable("zoomEnabled");
|
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-06-30 21:31:59 +03:00
|
|
|
assertPropNotChangeable("attributionButtonIsHidden");
|
2016-06-30 20:25:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setLogoIsHidden(boolean value) {
|
|
|
|
|
if (_mapOptions.getLogoEnabled() == !value) { return; }
|
|
|
|
|
_mapOptions.logoEnabled(!value);
|
2016-06-30 21:31:59 +03:00
|
|
|
assertPropNotChangeable("logoIsHidden");
|
2016-06-30 20:25:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setCompassIsHidden(boolean value) {
|
|
|
|
|
if (_mapOptions.getCompassEnabled() == !value) { return; }
|
|
|
|
|
_mapOptions.compassEnabled(!value);
|
2016-06-30 21:31:59 +03:00
|
|
|
assertPropNotChangeable("compassIsHidden");
|
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-06-30 16:59:10 +03:00
|
|
|
}
|