Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,24 @@ class IsolatedActivityPluginTest {
location!!.longitude = 2.0
}

@Test
fun locationLayerPlugin_initializesLocationEngineCorrectlyWhenOnesNotProvided() {
val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction<LocationLayerPlugin> {
override fun onGenericPluginAction(plugin: LocationLayerPlugin?, mapboxMap: MapboxMap?,
uiController: UiController, context: Context) {

mapView = fragment?.view as MapView?
val locationLayerPlugin = LocationLayerPlugin(mapView!!, mapboxMap!!)
val locationEngine = locationLayerPlugin.locationEngine
assertThat(locationEngine, notNullValue())

uiController.loopMainThreadForAtLeast(500)
assertThat(locationEngine?.isConnected, `is`(equalTo(true)))
}
}
executePluginTest(pluginAction)
}

@Test
fun settingMapStyleImmediatelyBeforeLoadingPlugin_doesStillLoadLayersProperly() {
// Using Empty fragment activity to test since Mapbox Style change needs to happen immediately.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

import com.mapbox.android.core.location.LocationEngine;
import com.mapbox.android.core.location.LocationEngineListener;
import com.mapbox.android.core.location.LocationEnginePriority;
import com.mapbox.android.core.location.LocationEngineProvider;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapView;
Expand Down Expand Up @@ -59,6 +61,7 @@ public final class LocationLayerPlugin implements LifecycleObserver {
private LocationLayerOptions options;
private LocationEngine locationEngine;
private CompassManager compassManager;
private boolean usingInternalLocationEngine;

private LocationLayer locationLayer;
private LocationLayerCamera locationLayerCamera;
Expand All @@ -78,6 +81,24 @@ public final class LocationLayerPlugin implements LifecycleObserver {
private final CopyOnWriteArrayList<OnCameraTrackingChangedListener> onCameraTrackingChangedListeners
= new CopyOnWriteArrayList<>();

/**
* Construct a LocationLayerPlugin
* <p>
* <strong>Note</strong>: This constructor will initialize and use an internal {@link LocationEngine}.
* </p>
*
* @param mapView the MapView to apply the LocationLayerPlugin to
* @param mapboxMap the MapboxMap to apply the LocationLayerPlugin with
* @since 0.5.3
*/
public LocationLayerPlugin(@NonNull MapView mapView, @NonNull MapboxMap mapboxMap) {
this.mapboxMap = mapboxMap;
this.mapView = mapView;
options = LocationLayerOptions.createFromAttributes(mapView.getContext(), R.style.mapbox_LocationLayer);
initializeLocationEngine();
initialize();
}

/**
* Construct a LocationLayerPlugin
*
Expand Down Expand Up @@ -263,6 +284,13 @@ public void forceLocationUpdate(@Nullable Location location) {
*/
public void setLocationEngine(@Nullable LocationEngine locationEngine) {
if (this.locationEngine != null) {
// If internal location engines being used, extra steps need to be taken to deconstruct the
// instance.
if (usingInternalLocationEngine) {
this.locationEngine.removeLocationUpdates();
this.locationEngine.deactivate();
usingInternalLocationEngine = false;
}
this.locationEngine.removeLocationEngineListener(locationEngineListener);
this.locationEngine = null;
}
Expand Down Expand Up @@ -483,6 +511,14 @@ private void initialize() {
setCameraMode(CameraMode.NONE);
}

private void initializeLocationEngine() {
usingInternalLocationEngine = true;
locationEngine = new LocationEngineProvider(mapView.getContext()).obtainBestLocationEngineAvailable();
locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY);
locationEngine.setFastestInterval(1000);
locationEngine.activate();
}

private void enableLocationLayerPlugin() {
isEnabled = true;
onLocationLayerStart();
Expand Down Expand Up @@ -643,7 +679,9 @@ public void onCompassAccuracyChange(int compassStatus) {
@Override
@SuppressWarnings( {"MissingPermission"})
public void onConnected() {
// Currently don't handle this inside SDK
if (usingInternalLocationEngine) {
locationEngine.requestLocationUpdates();
}
}

@Override
Expand Down