diff --git a/app/src/androidTest/java/com/mapbox/mapboxsdk/plugins/locationlayer/IsolatedActivityPluginTest.kt b/app/src/androidTest/java/com/mapbox/mapboxsdk/plugins/locationlayer/IsolatedActivityPluginTest.kt index 64609494d..b0a52dfd7 100644 --- a/app/src/androidTest/java/com/mapbox/mapboxsdk/plugins/locationlayer/IsolatedActivityPluginTest.kt +++ b/app/src/androidTest/java/com/mapbox/mapboxsdk/plugins/locationlayer/IsolatedActivityPluginTest.kt @@ -76,6 +76,24 @@ class IsolatedActivityPluginTest { location!!.longitude = 2.0 } + @Test + fun locationLayerPlugin_initializesLocationEngineCorrectlyWhenOnesNotProvided() { + val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { + 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. diff --git a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPlugin.java b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPlugin.java index c9b4eeaa2..e41d6d7a8 100644 --- a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPlugin.java +++ b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPlugin.java @@ -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; @@ -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; @@ -78,6 +81,24 @@ public final class LocationLayerPlugin implements LifecycleObserver { private final CopyOnWriteArrayList onCameraTrackingChangedListeners = new CopyOnWriteArrayList<>(); + /** + * Construct a LocationLayerPlugin + *

+ * Note: This constructor will initialize and use an internal {@link LocationEngine}. + *

+ * + * @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 * @@ -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; } @@ -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(); @@ -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