This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[android] Add a way to use a custom location source #8710
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
import com.mapbox.mapboxsdk.constants.MyLocationTracking; | ||
import com.mapbox.mapboxsdk.location.LocationSource; | ||
import com.mapbox.mapboxsdk.maps.widgets.MyLocationView; | ||
import com.mapbox.services.android.telemetry.location.LocationEngine; | ||
import com.mapbox.services.android.telemetry.location.LocationEngineListener; | ||
import com.mapbox.services.android.telemetry.permissions.PermissionsManager; | ||
|
||
|
@@ -26,6 +27,7 @@ public final class TrackingSettings { | |
private final UiSettings uiSettings; | ||
private final FocalPointChangeListener focalPointChangedListener; | ||
private final CameraZoomInvalidator zoomInvalidator; | ||
private LocationEngine locationSource; | ||
private LocationEngineListener myLocationListener; | ||
|
||
private boolean myLocationEnabled; | ||
|
@@ -45,6 +47,7 @@ public final class TrackingSettings { | |
} | ||
|
||
void initialise(MapboxMapOptions options) { | ||
locationSource = LocationSource.getLocationEngine(myLocationView.getContext()); | ||
setMyLocationEnabled(options.getLocationEnabled()); | ||
} | ||
|
||
|
@@ -328,9 +331,9 @@ public void onLocationChanged(Location location) { | |
} | ||
} | ||
}; | ||
LocationSource.getLocationEngine(myLocationView.getContext()).addLocationEngineListener(myLocationListener); | ||
locationSource.addLocationEngineListener(myLocationListener); | ||
} else { | ||
LocationSource.getLocationEngine(myLocationView.getContext()).removeLocationEngineListener(myLocationListener); | ||
locationSource.removeLocationEngineListener(myLocationListener); | ||
myLocationListener = null; | ||
} | ||
} | ||
|
@@ -362,6 +365,11 @@ void setMyLocationEnabled(boolean locationEnabled) { | |
myLocationView.setEnabled(locationEnabled); | ||
} | ||
|
||
void setLocationSource(LocationEngine locationSource) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly, let's call it setLocationEngine(). |
||
this.locationSource = locationSource; | ||
myLocationView.setLocationSource(locationSource); | ||
} | ||
|
||
void update() { | ||
if (!myLocationView.isEnabled()) { | ||
return; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,7 @@ public class MyLocationView extends View { | |
|
||
private LatLng latLng; | ||
private Location location; | ||
private LocationEngine locationSource; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rather call it |
||
private long locationUpdateTimestamp; | ||
private float previousDirection; | ||
|
||
|
@@ -320,8 +321,7 @@ public void setBearing(double bearing) { | |
if (location != null) { | ||
setCompass(location.getBearing() - bearing); | ||
} | ||
} else if (myBearingTrackingMode == MyBearingTracking.COMPASS | ||
&& compassListener.isSensorAvailable()) { | ||
} else if (myBearingTrackingMode == MyBearingTracking.COMPASS && compassListener.isSensorAvailable()) { | ||
setCompass(magneticHeading - bearing); | ||
} | ||
} | ||
|
@@ -335,8 +335,7 @@ public void setCameraPosition(CameraPosition position) { | |
} | ||
|
||
public void onStart() { | ||
if (myBearingTrackingMode == MyBearingTracking.COMPASS | ||
&& compassListener.isSensorAvailable()) { | ||
if (myBearingTrackingMode == MyBearingTracking.COMPASS && compassListener.isSensorAvailable()) { | ||
compassListener.onResume(); | ||
} | ||
if (isEnabled()) { | ||
|
@@ -369,7 +368,8 @@ protected void onDetachedFromWindow() { | |
} | ||
|
||
if (userLocationListener != null) { | ||
LocationSource.getLocationEngine(getContext()).removeLocationEngineListener(userLocationListener); | ||
locationSource.removeLocationEngineListener(userLocationListener); | ||
locationSource = null; | ||
userLocationListener = null; | ||
} | ||
} | ||
|
@@ -419,29 +419,32 @@ public void onRestoreInstanceState(Parcelable state) { | |
* @param enableGps true if GPS is to be enabled, false if GPS is to be disabled | ||
*/ | ||
private void toggleGps(boolean enableGps) { | ||
LocationEngine locationEngine = LocationSource.getLocationEngine(getContext()); | ||
if (locationSource == null) { | ||
locationSource = LocationSource.getLocationEngine(this.getContext()); | ||
} | ||
|
||
if (enableGps) { | ||
// Set an initial location if one available | ||
Location lastLocation = locationEngine.getLastLocation(); | ||
Location lastLocation = locationSource.getLastLocation(); | ||
|
||
if (lastLocation != null) { | ||
setLocation(lastLocation); | ||
} | ||
|
||
if (userLocationListener == null) { | ||
userLocationListener = new GpsLocationListener(this); | ||
userLocationListener = new GpsLocationListener(this, locationSource); | ||
} | ||
|
||
locationEngine.addLocationEngineListener(userLocationListener); | ||
locationEngine.activate(); | ||
locationSource.addLocationEngineListener(userLocationListener); | ||
locationSource.activate(); | ||
} else { | ||
// Disable location and user dot | ||
location = null; | ||
locationEngine.removeLocationEngineListener(userLocationListener); | ||
locationEngine.deactivate(); | ||
locationSource.removeLocationEngineListener(userLocationListener); | ||
locationSource.deactivate(); | ||
} | ||
|
||
locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY); | ||
locationSource.setPriority(LocationEnginePriority.HIGH_ACCURACY); | ||
} | ||
|
||
public Location getLocation() { | ||
|
@@ -460,8 +463,7 @@ public void setLocation(Location location) { | |
|
||
public void setMyBearingTrackingMode(@MyBearingTracking.Mode int myBearingTrackingMode) { | ||
this.myBearingTrackingMode = myBearingTrackingMode; | ||
if (myBearingTrackingMode == MyBearingTracking.COMPASS | ||
&& compassListener.isSensorAvailable()) { | ||
if (myBearingTrackingMode == MyBearingTracking.COMPASS && compassListener.isSensorAvailable()) { | ||
compassListener.onResume(); | ||
} else { | ||
compassListener.onPause(); | ||
|
@@ -561,22 +563,28 @@ public void setContentPadding(int[] padding) { | |
contentPaddingY = (padding[1] - padding[3]) / 2; | ||
} | ||
|
||
public void setLocationSource(LocationEngine locationSource) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same: setLocationEngine(). |
||
this.locationSource = locationSource; | ||
} | ||
|
||
private static class GpsLocationListener implements LocationEngineListener { | ||
|
||
private WeakReference<MyLocationView> userLocationView; | ||
private WeakReference<LocationEngine> locationSource; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly, let's call the variable |
||
|
||
GpsLocationListener(MyLocationView myLocationView) { | ||
GpsLocationListener(MyLocationView myLocationView, LocationEngine locationEngine) { | ||
userLocationView = new WeakReference<>(myLocationView); | ||
locationSource = new WeakReference<>(locationEngine); | ||
} | ||
|
||
@Override | ||
public void onConnected() { | ||
MyLocationView locationView = userLocationView.get(); | ||
if (locationView != null) { | ||
LocationEngine locationSource = LocationSource.getLocationEngine(locationView.getContext()); | ||
Location location = locationSource.getLastLocation(); | ||
LocationEngine locationEngine = locationSource.get(); | ||
Location location = locationEngine.getLastLocation(); | ||
locationView.setLocation(location); | ||
locationSource.requestLocationUpdates(); | ||
locationEngine.requestLocationUpdates(); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
...java/com/mapbox/mapboxsdk/testapp/activity/userlocation/CustomLocationEngineActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package com.mapbox.mapboxsdk.testapp.activity.userlocation; | ||
|
||
import android.Manifest; | ||
import android.content.pm.PackageManager; | ||
import android.os.Bundle; | ||
import android.support.annotation.NonNull; | ||
import android.support.annotation.UiThread; | ||
import android.support.design.widget.FloatingActionButton; | ||
import android.support.v4.app.ActivityCompat; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.view.View; | ||
|
||
import com.mapbox.mapboxsdk.maps.MapView; | ||
import com.mapbox.mapboxsdk.maps.MapboxMap; | ||
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; | ||
import com.mapbox.mapboxsdk.testapp.R; | ||
import com.mapbox.services.android.telemetry.location.LocationEngine; | ||
import com.mapbox.services.android.telemetry.permissions.PermissionsManager; | ||
|
||
public class CustomLocationEngineActivity extends AppCompatActivity { | ||
|
||
private MapView mapView; | ||
private MapboxMap mapboxMap; | ||
private FloatingActionButton locationToggleFab; | ||
|
||
private LocationEngine locationServices; | ||
|
||
private static final int PERMISSIONS_LOCATION = 0; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_custom_location_engine); | ||
|
||
locationServices = new MockLocationEngine(); | ||
|
||
mapView = (MapView) findViewById(R.id.mapView); | ||
mapView.onCreate(savedInstanceState); | ||
mapView.getMapAsync(new OnMapReadyCallback() { | ||
@Override | ||
public void onMapReady(MapboxMap map) { | ||
mapboxMap = map; | ||
mapboxMap.setLocationSource(locationServices); | ||
} | ||
}); | ||
|
||
locationToggleFab = (FloatingActionButton) findViewById(R.id.fabLocationToggle); | ||
locationToggleFab.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View view) { | ||
if (mapboxMap != null) { | ||
toggleGps(!mapboxMap.isMyLocationEnabled()); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
protected void onStart() { | ||
super.onStart(); | ||
mapView.onStart(); | ||
} | ||
|
||
@Override | ||
protected void onResume() { | ||
super.onResume(); | ||
mapView.onResume(); | ||
} | ||
|
||
@Override | ||
protected void onPause() { | ||
super.onPause(); | ||
mapView.onPause(); | ||
} | ||
|
||
@Override | ||
protected void onStop() { | ||
super.onStop(); | ||
mapView.onStop(); | ||
} | ||
|
||
@Override | ||
protected void onSaveInstanceState(Bundle outState) { | ||
super.onSaveInstanceState(outState); | ||
mapView.onSaveInstanceState(outState); | ||
} | ||
|
||
@Override | ||
protected void onDestroy() { | ||
super.onDestroy(); | ||
mapView.onDestroy(); | ||
} | ||
|
||
@Override | ||
public void onLowMemory() { | ||
super.onLowMemory(); | ||
mapView.onLowMemory(); | ||
} | ||
|
||
@UiThread | ||
public void toggleGps(boolean enableGps) { | ||
if (enableGps) { | ||
if (!PermissionsManager.areLocationPermissionsGranted(this)) { | ||
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.ACCESS_COARSE_LOCATION, | ||
Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSIONS_LOCATION); | ||
} else { | ||
enableLocation(true); | ||
} | ||
} else { | ||
enableLocation(false); | ||
} | ||
} | ||
|
||
private void enableLocation(boolean enabled) { | ||
mapboxMap.setMyLocationEnabled(enabled); | ||
if (enabled) { | ||
locationToggleFab.setImageResource(R.drawable.ic_location_disabled); | ||
} else { | ||
locationToggleFab.setImageResource(R.drawable.ic_my_location); | ||
} | ||
} | ||
|
||
@Override | ||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | ||
if (requestCode == PERMISSIONS_LOCATION) { | ||
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { | ||
enableLocation(true); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's call this
setLocationEngine
.