Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

[android] Add a way to use a custom location source #8710

Merged
merged 2 commits into from
Apr 20, 2017
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 @@ -37,10 +37,12 @@
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.location.LocationSource;
import com.mapbox.mapboxsdk.maps.widgets.MyLocationViewSettings;
import com.mapbox.mapboxsdk.style.layers.Filter;
import com.mapbox.mapboxsdk.style.layers.Layer;
import com.mapbox.mapboxsdk.style.sources.Source;
import com.mapbox.services.android.telemetry.location.LocationEngine;
import com.mapbox.services.commons.geojson.Feature;

import java.lang.reflect.ParameterizedType;
Expand Down Expand Up @@ -1747,6 +1749,18 @@ public void setOnMyLocationChangeListener(@Nullable MapboxMap.OnMyLocationChange
trackingSettings.setOnMyLocationChangeListener(listener);
}

/**
* Replaces the location source of the my-location layer.
*
* @param locationSource A {@link LocationEngine} location source to use in the my-location layer.
* Set to null to use the default {@link LocationSource}
* location source.
*/
@UiThread
public void setLocationSource(@Nullable LocationEngine locationSource) {
Copy link
Member

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.

trackingSettings.setLocationSource(locationSource);
}

/**
* Sets a callback that's invoked when the location tracking mode changes.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -45,6 +47,7 @@ public final class TrackingSettings {
}

void initialise(MapboxMapOptions options) {
locationSource = LocationSource.getLocationEngine(myLocationView.getContext());
setMyLocationEnabled(options.getLocationEnabled());
}

Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -362,6 +365,11 @@ void setMyLocationEnabled(boolean locationEnabled) {
myLocationView.setEnabled(locationEnabled);
}

void setLocationSource(LocationEngine locationSource) {
Copy link
Member

Choose a reason for hiding this comment

The 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public class MyLocationView extends View {

private LatLng latLng;
private Location location;
private LocationEngine locationSource;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather call it locationEngine even if LocationSource is our default implementation.

private long locationUpdateTimestamp;
private float previousDirection;

Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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()) {
Expand Down Expand Up @@ -369,7 +368,8 @@ protected void onDetachedFromWindow() {
}

if (userLocationListener != null) {
LocationSource.getLocationEngine(getContext()).removeLocationEngineListener(userLocationListener);
locationSource.removeLocationEngineListener(userLocationListener);
locationSource = null;
userLocationListener = null;
}
}
Expand Down Expand Up @@ -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() {
Expand All @@ -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();
Expand Down Expand Up @@ -561,22 +563,28 @@ public void setContentPadding(int[] padding) {
contentPaddingY = (padding[1] - padding[3]) / 2;
}

public void setLocationSource(LocationEngine locationSource) {
Copy link
Member

Choose a reason for hiding this comment

The 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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, let's call the variable locationEngine.


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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,17 @@
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.FeatureOverviewActivity"/>
</activity>
<activity
android:name=".activity.userlocation.CustomLocationEngineActivity"
android:description="@string/description_custom_location_engine"
android:label="@string/activity_custom_location_engine">
<meta-data
android:name="@string/category"
android:value="@string/category_userlocation"/>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.FeatureOverviewActivity"/>
</activity>
<activity
android:name=".activity.annotation.PolygonActivity"
android:description="@string/description_polygon"
Expand Down
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);
}
}
}
}
Loading