This repository has been archived by the owner on Oct 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 493
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add user tracking location example to wear app (#255)
- Loading branch information
1 parent
180dc26
commit 9f04cdf
Showing
7 changed files
with
253 additions
and
2 deletions.
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
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
128 changes: 128 additions & 0 deletions
128
...oidWearDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/GoogleLocationEngine.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,128 @@ | ||
package com.mapbox.mapboxandroiddemo.examples; | ||
|
||
import android.content.Context; | ||
import android.location.Location; | ||
import android.os.Bundle; | ||
import android.support.annotation.NonNull; | ||
import android.support.annotation.Nullable; | ||
import android.util.Log; | ||
|
||
import com.google.android.gms.common.ConnectionResult; | ||
import com.google.android.gms.common.api.GoogleApiClient; | ||
import com.google.android.gms.location.LocationListener; | ||
import com.google.android.gms.location.LocationRequest; | ||
import com.google.android.gms.location.LocationServices; | ||
import com.mapbox.services.android.telemetry.location.LocationEngine; | ||
import com.mapbox.services.android.telemetry.location.LocationEngineListener; | ||
|
||
import java.lang.ref.WeakReference; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Sample LocationEngine using Google Play Services | ||
*/ | ||
public class GoogleLocationEngine extends LocationEngine implements | ||
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener { | ||
|
||
private static final String LOG_TAG = GoogleLocationEngine.class.getSimpleName(); | ||
|
||
private static LocationEngine instance; | ||
|
||
private WeakReference<Context> context; | ||
private GoogleApiClient googleApiClient; | ||
|
||
public GoogleLocationEngine(Context context) { | ||
super(); | ||
this.context = new WeakReference<>(context); | ||
googleApiClient = new GoogleApiClient.Builder(this.context.get()) | ||
.addConnectionCallbacks(this) | ||
.addOnConnectionFailedListener(this) | ||
.addApi(LocationServices.API) | ||
.build(); | ||
} | ||
|
||
public static synchronized LocationEngine getLocationEngine(Context context) { | ||
if (instance == null) { | ||
instance = new GoogleLocationEngine(context.getApplicationContext()); | ||
} | ||
|
||
return instance; | ||
} | ||
|
||
@Override | ||
public void activate() { | ||
if (googleApiClient != null && !googleApiClient.isConnected()) { | ||
googleApiClient.connect(); | ||
} | ||
} | ||
|
||
@Override | ||
public void deactivate() { | ||
if (googleApiClient != null && googleApiClient.isConnected()) { | ||
googleApiClient.disconnect(); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isConnected() { | ||
return googleApiClient.isConnected(); | ||
} | ||
|
||
@Override | ||
public void onConnected(@Nullable Bundle bundle) { | ||
for (LocationEngineListener listener : locationListeners) { | ||
listener.onConnected(); | ||
} | ||
} | ||
|
||
@Override | ||
public void onConnectionSuspended(int cause) { | ||
Log.d(LOG_TAG, "Connection suspended: " + cause); | ||
} | ||
|
||
@Override | ||
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { | ||
Log.d(LOG_TAG, "Connection failed:" + connectionResult.getErrorMessage()); | ||
} | ||
|
||
@Override | ||
public Location getLastLocation() { | ||
if (googleApiClient.isConnected()) { | ||
//noinspection MissingPermission | ||
return LocationServices.FusedLocationApi.getLastLocation(googleApiClient); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
@Override | ||
public void requestLocationUpdates() { | ||
// Create the LocationRequest object | ||
LocationRequest locationRequest = LocationRequest.create(); | ||
// Use high accuracy | ||
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); | ||
// Set the update interval to 2 seconds | ||
locationRequest.setInterval(TimeUnit.SECONDS.toMillis(2)); | ||
// Set the fastest update interval to 2 seconds | ||
locationRequest.setFastestInterval(TimeUnit.SECONDS.toMillis(2)); | ||
// Set the minimum displacement | ||
locationRequest.setSmallestDisplacement(2); | ||
|
||
// Register listener using the LocationRequest object | ||
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this); | ||
} | ||
|
||
@Override | ||
public void removeLocationUpdates() { | ||
if (googleApiClient.isConnected()) { | ||
LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this); | ||
} | ||
} | ||
|
||
@Override | ||
public void onLocationChanged(Location location) { | ||
for (LocationEngineListener listener : locationListeners) { | ||
listener.onLocationChanged(location); | ||
} | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
...earDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/LocationTrackingActivity.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,112 @@ | ||
package com.mapbox.mapboxandroiddemo.examples; | ||
|
||
|
||
import android.location.Location; | ||
import android.os.Bundle; | ||
import android.support.wearable.activity.WearableActivity; | ||
|
||
import com.mapbox.mapboxandroiddemo.R; | ||
import com.mapbox.mapboxsdk.Mapbox; | ||
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory; | ||
import com.mapbox.mapboxsdk.geometry.LatLng; | ||
import com.mapbox.mapboxsdk.maps.MapView; | ||
import com.mapbox.mapboxsdk.maps.MapboxMap; | ||
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; | ||
import com.mapbox.services.android.telemetry.location.LocationEngine; | ||
import com.mapbox.services.android.telemetry.location.LocationEngineListener; | ||
|
||
public class LocationTrackingActivity extends WearableActivity { | ||
|
||
private MapView mapView; | ||
private MapboxMap map; | ||
private LocationEngine locationEngine; | ||
private LocationEngineListener locationEngineListener; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
|
||
// Mapbox access token is configured here. This needs to be called either in your application | ||
// object or in the same activity which contains the mapview. | ||
Mapbox.getInstance(this, getString(R.string.access_token)); | ||
|
||
// This contains the MapView in XML and needs to be called after the account manager | ||
setContentView(R.layout.activity_simple_mapview); | ||
|
||
mapView = (MapView) findViewById(R.id.mapView); | ||
mapView.onCreate(savedInstanceState); | ||
locationEngine = GoogleLocationEngine.getLocationEngine(this); | ||
locationEngineListener = new LocationEngineListener() { | ||
@Override | ||
public void onConnected() { | ||
locationEngine.requestLocationUpdates(); | ||
} | ||
|
||
@Override | ||
public void onLocationChanged(Location location) { | ||
if (map != null) { | ||
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location), 16)); | ||
} | ||
} | ||
}; | ||
mapView.getMapAsync(new OnMapReadyCallback() { | ||
@Override | ||
public void onMapReady(MapboxMap mapboxMap) { | ||
|
||
// Customize map with markers, polylines, etc. | ||
map = mapboxMap; | ||
Location lastLocation = locationEngine.getLastLocation(); | ||
if (lastLocation != null) { | ||
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lastLocation), 16)); | ||
} | ||
} | ||
}); | ||
locationEngine.addLocationEngineListener(locationEngineListener); | ||
setAmbientEnabled(); | ||
} | ||
|
||
@Override | ||
public void onResume() { | ||
super.onResume(); | ||
mapView.onResume(); | ||
} | ||
|
||
@Override | ||
public void onPause() { | ||
super.onPause(); | ||
mapView.onPause(); | ||
} | ||
|
||
@Override | ||
public void onLowMemory() { | ||
super.onLowMemory(); | ||
mapView.onLowMemory(); | ||
} | ||
|
||
@Override | ||
protected void onStart() { | ||
super.onStart(); | ||
mapView.onStart(); | ||
locationEngine.activate(); | ||
} | ||
|
||
@Override | ||
protected void onStop() { | ||
locationEngine.removeLocationUpdates(); | ||
locationEngine.deactivate(); | ||
super.onStop(); | ||
mapView.onStop(); | ||
} | ||
|
||
@Override | ||
protected void onDestroy() { | ||
super.onDestroy(); | ||
mapView.onDestroy(); | ||
} | ||
|
||
@Override | ||
protected void onSaveInstanceState(Bundle outState) { | ||
super.onSaveInstanceState(outState); | ||
mapView.onSaveInstanceState(outState); | ||
} | ||
} |
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