Skip to content

Commit

Permalink
Added MapView.tintColor and NativeUserLocation.iosShowsUserHeadingInd…
Browse files Browse the repository at this point in the history
…icator
  • Loading branch information
mfazekas committed Apr 16, 2020
1 parent 0335ca1 commit 88ba97d
Show file tree
Hide file tree
Showing 18 changed files with 257 additions and 274 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,12 @@ public class RCTMGLCamera extends AbstractMapFeature {
private LocationManager.OnUserLocationChange mLocationChangeListener = new LocationManager.OnUserLocationChange() {
@Override
public void onLocationChange(Location nextLocation) {
if (getMapboxMap() == null || mLocationComponentManager == null || !mLocationComponentManager.hasLocationComponent() || (!mFollowUserLocation)) {
return;
}

float distToNextLocation = mUserLocation.getDistance(nextLocation);
// mLocationComponentManager.forceLocationUpdate(nextLocation);
mUserLocation.setCurrentLocation(nextLocation);
if (getMapboxMap() == null || mLocationComponentManager == null || !mLocationComponentManager.hasLocationComponent() || (!mFollowUserLocation)) {
return;
}

if (mUserTrackingState == UserTrackingState.POSSIBLE || distToNextLocation > 0.0f) {
// updateUserLocation(true);
}
sendUserLocationUpdateEvent(nextLocation);
mUserLocation.setCurrentLocation(nextLocation);
sendUserLocationUpdateEvent(nextLocation);
}
};

Expand Down Expand Up @@ -383,85 +377,14 @@ private void updateLocationLayer(@NonNull Style style) {

mLocationComponentManager.update(style);

/*
if (mLocationComponent == null ) {
MapboxMap mapboxMap = getMapboxMap();
mLocationComponent = mapboxMap.getLocationComponent();
LocationComponentOptions.Builder builder = LocationComponentOptions.builder(mContext);
if (!mShowUserLocation) {
builder = builder
.padding(mapboxMap.getPadding())
.backgroundDrawable(R.drawable.empty)
.backgroundDrawableStale(R.drawable.empty)
.bearingDrawable(R.drawable.empty)
.foregroundDrawable(R.drawable.empty)
.foregroundDrawableStale(R.drawable.empty)
.gpsDrawable(R.drawable.empty)
.accuracyAlpha(0.0f);
}
LocationComponentOptions locationComponentOptions = builder.build();
LocationComponentActivationOptions locationComponentActivationOptions = LocationComponentActivationOptions
.builder(mContext, style)
.locationComponentOptions(locationComponentOptions)
.build();
mLocationComponent.activateLocationComponent(locationComponentActivationOptions);
mLocationComponent.setLocationEngine(mLocationManager.getEngine());
}
*/
// int userLayerMode = UserTrackingMode.getMapLayerMode(mUserLocation.getTrackingMode(), mShowUserLocation);

if (mFollowUserLocation) {
mLocationComponentManager.setCameraMode(UserTrackingMode.getCameraMode(mUserTrackingMode));
}
mLocationComponentManager.setFollowUserLocation(mFollowUserLocation);

/*
if (userLayerMode != -1) {
mLocationComponent.setRenderMode(userLayerMode);
} *
*/

if (mFollowUserLocation) {
mLocationComponentManager.setCameraMode(UserTrackingMode.getCameraMode(mUserTrackingMode));
mLocationComponentManager.addOnCameraTrackingChangedListener(new OnCameraTrackingChangedListener() {
@Override public void onCameraTrackingChanged(int currentMode) {
int userTrackingMode = UserTrackingMode.NONE;
switch (currentMode) {
case CameraMode.NONE:
userTrackingMode = UserTrackingMode.NONE;
break;
case CameraMode.TRACKING:
userTrackingMode = UserTrackingMode.FOLLOW;
break;
case CameraMode.TRACKING_COMPASS:
userTrackingMode = UserTrackingMode.FollowWithHeading;
break;
case CameraMode.TRACKING_GPS:
userTrackingMode = UserTrackingMode.FollowWithCourse;
break;
default:
userTrackingMode = UserTrackingMode.NONE;
}
updateUserTrackingMode(userTrackingMode);
}
@Override public void onCameraTrackingDismissed() {
}
});
} else {
mLocationComponentManager.setCameraMode(CameraMode.NONE);
}
/*
if (mFollowUserLocation) {
if (!mShowUserLocation) {
mLocationComponent.setRenderMode(RenderMode.GPS);
}
mLocationComponent.setCameraMode(UserTrackingMode.getCameraMode(mUserTrackingMode));
mLocationComponent.onStart();
mLocationComponent.addOnCameraTrackingChangedListener(
new OnCameraTrackingChangedListener() {
mLocationComponentManager.setCameraMode(UserTrackingMode.getCameraMode(mUserTrackingMode));
mLocationComponentManager.addOnCameraTrackingChangedListener(new OnCameraTrackingChangedListener() {
@Override public void onCameraTrackingChanged(int currentMode) {
int userTrackingMode = UserTrackingMode.NONE;
switch (currentMode) {
Expand All @@ -484,11 +407,10 @@ private void updateLocationLayer(@NonNull Style style) {
}
@Override public void onCameraTrackingDismissed() {
}
}
);
});
} else {
mLocationComponent.setCameraMode(CameraMode.NONE);
} */
mLocationComponentManager.setCameraMode(CameraMode.NONE);
}
}

public void setMinZoomLevel(double zoomLevel) {
Expand Down Expand Up @@ -552,8 +474,6 @@ public void setUserTrackingMode(int userTrackingMode) {
}

if (getMapboxMap() != null) {
//updateUserLocation
// (false);
updateLocationLayer(getMapboxMap().getStyle());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class LocationComponentManager {

// state
private @CameraMode.Mode int mCameraMode = CameraMode.NONE;

private @RenderMode.Mode int mRenderMode = RenderMode.COMPASS;

public LocationComponentManager(RCTMGLMapView rctmglMapView, Context context) {
mMapView = rctmglMapView;
Expand All @@ -46,13 +46,10 @@ public LocationComponentManager(RCTMGLMapView rctmglMapView, Context context) {

private boolean mShowUserLocation = false;


private boolean mFollowUserLocation = false;

private boolean mShowingUserLocation = false;

// TODO revise refactored from Camera

public void showUserLocation(boolean showUserLocation) {
mShowUserLocation = showUserLocation;
stateChanged();
Expand All @@ -66,6 +63,14 @@ public void setFollowUserLocation(boolean followUserLocation) {
public void setCameraMode(@CameraMode.Mode int cameraMode) {
mLocationComponent.setCameraMode(cameraMode);
}

public void setRenderMode(@RenderMode.Mode int renderMode) {
mRenderMode = renderMode;
if (mShowingUserLocation) {
mLocationComponent.setRenderMode(renderMode);
}
}

public void addOnCameraTrackingChangedListener(OnCameraTrackingChangedListener onCameraTrackingChangedListener) {
mLocationComponent.addOnCameraTrackingChangedListener(onCameraTrackingChangedListener);
}
Expand All @@ -81,9 +86,7 @@ private void stateChanged() {
if (!mShowUserLocation) {
mLocationComponent.setRenderMode(RenderMode.GPS);
} else {
//mLocationComponent.setRenderMode(RenderMode.NORMAL); // circle
mLocationComponent.setRenderMode(RenderMode.COMPASS); // circle with small triangle
// mLocationComponent.setRenderMode(RenderMode.GPS); // big arrow
mLocationComponent.setRenderMode(mRenderMode);
}
mLocationComponent.onStart();
} else {
Expand Down Expand Up @@ -130,7 +133,6 @@ private void updateShowUserLocation(boolean displayUserLocation) {
}
}


LocationComponentOptions options(boolean displayUserLocation) {
LocationComponentOptions.Builder builder = LocationComponentOptions.builder(mContext);
if (!displayUserLocation) {
Expand All @@ -146,121 +148,4 @@ LocationComponentOptions options(boolean displayUserLocation) {
}
return builder.build();
}


/**
* 1. implement custom location tracking
*
*/



/*
public void enableLocation() {
if (!PermissionsManager.areLocationPermissionsGranted(mContext)) {
return;
}
if (!mLocationManager.isActive()) {
mLocationManager.enable();
}
mMapView.getMapboxMap().getStyle(new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
enableLocationComponent(style);
}
});
}
private void enableLocationComponent(@NonNull Style style) {
updateUserLocation(false);
updateLocationLayer(style);
Location lastKnownLocation = mLocationManager.getLastKnownLocation();
mLocationManager.addLocationListener(mLocationChangeListener);
if (lastKnownLocation != null) {
mLocationChangeListener.onLocationChange(lastKnownLocation);
postDelayed(new Runnable() {
@Override
public void run() {
mMapView.sendRegionDidChangeEvent();
}
}, 200);
}
}
private void updateLocationLayer(@NonNull Style style) {
if (mLocationComponent == null) {
mLocationComponent = mMap.getLocationComponent();
LocationComponentOptions.Builder builder = LocationComponentOptions.builder(mContext);
if (!mShowUserLocation) {
builder = builder
.padding(mMap.getPadding())
.backgroundDrawable(R.drawable.empty)
.backgroundDrawableStale(R.drawable.empty)
.bearingDrawable(R.drawable.empty)
.foregroundDrawable(R.drawable.empty)
.foregroundDrawableStale(R.drawable.empty)
.gpsDrawable(R.drawable.empty)
.accuracyAlpha(0.0f);
}
LocationComponentOptions locationComponentOptions = builder.build();
LocationComponentActivationOptions locationComponentActivationOptions = LocationComponentActivationOptions
.builder(mContext, style)
.locationComponentOptions(locationComponentOptions)
.build();
mLocationComponent.activateLocationComponent(locationComponentActivationOptions);
mLocationComponent.setLocationEngine(mLocationManager.getEngine());
}
int userLayerMode = UserTrackingMode.getMapLayerMode(mUserLocation.getTrackingMode(), mShowUserLocation);
mLocationComponent.setLocationComponentEnabled(mFollowUserLocation || mShowUserLocation);
if (userLayerMode != -1) {
mLocationComponent.setRenderMode(userLayerMode);
}
if (mFollowUserLocation) {
if (!mShowUserLocation) {
mLocationComponent.setRenderMode(RenderMode.GPS);
}
mLocationComponent.setCameraMode(UserTrackingMode.getCameraMode(mUserTrackingMode));
mLocationComponent.onStart();
mLocationComponent.addOnCameraTrackingChangedListener(
new OnCameraTrackingChangedListener() {
@Override public void onCameraTrackingChanged(int currentMode) {
int userTrackingMode = UserTrackingMode.NONE;
switch (currentMode) {
case CameraMode.NONE:
userTrackingMode = UserTrackingMode.NONE;
break;
case CameraMode.TRACKING:
userTrackingMode = UserTrackingMode.FOLLOW;
break;
case CameraMode.TRACKING_COMPASS:
userTrackingMode = UserTrackingMode.FollowWithHeading;
break;
case CameraMode.TRACKING_GPS:
userTrackingMode = UserTrackingMode.FollowWithCourse;
break;
default:
userTrackingMode = UserTrackingMode.NONE;
}
updateUserTrackingMode(userTrackingMode);
}
@Override public void onCameraTrackingDismissed() {
}
}
);
} else {
mLocationComponent.setCameraMode(CameraMode.NONE);
}
}
*/

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,16 @@ public void onMapReady(@NonNull MapboxMap mapboxMap) {
@Override
public void onStyleLoaded(@NonNull Style style) {
Context context = getContext();

LocationComponentManager locationComponent = mMapView.getLocationComponentManager();
locationComponent.showUserLocation(mEnabled);
/*
if (!PermissionsManager.areLocationPermissionsGranted(context)) {
return;
}

LocationComponent locationComponent = mMap.getLocationComponent();
if (mEnabled) {
locationComponent.activateLocationComponent(LocationComponentActivationOptions.builder(context, style).build());
locationComponent.setRenderMode(RenderMode.NORMAL);
}
locationComponent.setLocationComponentEnabled(mEnabled);
*/
LocationComponentManager locationComponent = mMapView.getLocationComponentManager();
locationComponent.showUserLocation(mEnabled);
}


public void setRenderMode(@RenderMode.Mode int renderMode) {
LocationComponentManager locationComponent = mMapView.getLocationComponentManager();
locationComponent.setRenderMode(renderMode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.ViewGroupManager;
import com.facebook.react.uimanager.annotations.ReactProp;
import com.mapbox.mapboxsdk.location.modes.RenderMode;

import javax.annotation.Nonnull;

Expand All @@ -14,6 +16,17 @@ public String getName() {
return REACT_CLASS;
}

@ReactProp(name="androidRenderMode")
public void setAndroidRenderMode(RCTMGLNativeUserLocation userLocation, String mode) {
if ("compass".equalsIgnoreCase(mode)) {
userLocation.setRenderMode(RenderMode.COMPASS);
} else if ("gps".equalsIgnoreCase(mode)) {
userLocation.setRenderMode(RenderMode.GPS);
} else {
userLocation.setRenderMode(RenderMode.NORMAL);
}
}

@Nonnull
@Override
protected RCTMGLNativeUserLocation createViewInstance(@Nonnull ThemedReactContext reactContext) {
Expand Down
Loading

0 comments on commit 88ba97d

Please sign in to comment.