summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/MyLocationView.java
diff options
context:
space:
mode:
Diffstat (limited to 'platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/MyLocationView.java')
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/MyLocationView.java58
1 files changed, 40 insertions, 18 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/MyLocationView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/MyLocationView.java
index f648db413d..9740679cf5 100644
--- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/MyLocationView.java
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/MyLocationView.java
@@ -26,6 +26,7 @@ import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
+import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.constants.MyBearingTracking;
@@ -40,6 +41,8 @@ import com.mapbox.services.android.telemetry.location.LocationEnginePriority;
import java.lang.ref.WeakReference;
+import timber.log.Timber;
+
/**
* UI element overlaid on a map to show the user's location.
*/
@@ -148,6 +151,10 @@ public class MyLocationView extends View {
compassListener = new CompassListener(context);
}
+ public void init(LocationSource locationSource) {
+ this.locationSource = locationSource;
+ }
+
public final void setForegroundDrawables(Drawable defaultDrawable, Drawable bearingDrawable) {
if (defaultDrawable == null) {
return;
@@ -391,9 +398,13 @@ public class MyLocationView extends View {
@Override
public void setEnabled(boolean enabled) {
+ setEnabled(enabled, false);
+ }
+
+ public void setEnabled(boolean enabled, boolean isCustomLocationSource) {
super.setEnabled(enabled);
setVisibility(enabled ? View.VISIBLE : View.INVISIBLE);
- toggleGps(enabled);
+ toggleGps(enabled, isCustomLocationSource);
}
@Override
@@ -414,22 +425,23 @@ public class MyLocationView extends View {
super.onRestoreInstanceState(state);
}
+ private void toggleGps(boolean enableGps) {
+ toggleGps(enableGps, mapboxMap != null && mapboxMap.getTrackingSettings().isCustomLocationSource());
+ }
+
/**
* Enabled / Disable GPS location updates along with updating the UI
*
* @param enableGps true if GPS is to be enabled, false if GPS is to be disabled
*/
- private void toggleGps(boolean enableGps) {
- if (locationSource == null) {
- locationSource = LocationSource.getLocationEngine(this.getContext());
- }
-
+ private void toggleGps(boolean enableGps, boolean isCustomLocationSource) {
if (enableGps) {
- // Set an initial location if one available
- Location lastLocation = locationSource.getLastLocation();
-
- if (lastLocation != null) {
- setLocation(lastLocation);
+ if (locationSource == null) {
+ if (!isCustomLocationSource) {
+ locationSource = Mapbox.getLocationSource();
+ } else {
+ return;
+ }
}
if (userLocationListener == null) {
@@ -437,16 +449,18 @@ public class MyLocationView extends View {
}
locationSource.addLocationEngineListener(userLocationListener);
+ locationSource.setPriority(LocationEnginePriority.HIGH_ACCURACY);
locationSource.activate();
} else {
+ if (locationSource == null) {
+ return;
+ }
// Disable location and user dot
location = null;
- locationSource.removeLocationUpdates();
locationSource.removeLocationEngineListener(userLocationListener);
+ locationSource.removeLocationUpdates();
locationSource.deactivate();
}
-
- locationSource.setPriority(LocationEnginePriority.HIGH_ACCURACY);
}
public Location getLocation() {
@@ -564,7 +578,10 @@ public class MyLocationView extends View {
}
public void setLocationSource(LocationEngine locationSource) {
+ toggleGps(false);
this.locationSource = locationSource;
+ this.userLocationListener = null;
+ setEnabled(isEnabled(), locationSource != null);
}
private static class GpsLocationListener implements LocationEngineListener {
@@ -580,10 +597,12 @@ public class MyLocationView extends View {
@Override
public void onConnected() {
MyLocationView locationView = userLocationView.get();
- if (locationView != null) {
- LocationEngine locationEngine = locationSource.get();
- Location location = locationEngine.getLastLocation();
- locationView.setLocation(location);
+ LocationEngine locationEngine = locationSource.get();
+ if (locationView != null && locationEngine != null) {
+ Location lastKnownLocation = locationEngine.getLastLocation();
+ if (lastKnownLocation != null) {
+ locationView.setLocation(lastKnownLocation);
+ }
locationEngine.requestLocationUpdates();
}
}
@@ -627,6 +646,9 @@ public class MyLocationView extends View {
}
public boolean isSensorAvailable() {
+ if (rotationVectorSensor == null) {
+ Timber.e("Sensor.TYPE_ROTATION_VECTOR is missing from this device. Unable to use MyBearingTracking.COMPASS.");
+ }
return rotationVectorSensor != null;
}