Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.

Commit

Permalink
add user tracking location example to wear app (#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
Guardiola31337 authored Mar 23, 2017
1 parent 180dc26 commit 9f04cdf
Show file tree
Hide file tree
Showing 7 changed files with 253 additions and 2 deletions.
1 change: 0 additions & 1 deletion MapboxAndroidDemo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
return void;
}

repositories {
Expand Down
2 changes: 1 addition & 1 deletion MapboxAndroidWearDemo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ dependencies {
provided 'com.google.android.wearable:wearable:2.0.0'
compile 'com.google.android.support:wearable:2.0.0'
compile 'com.google.android.gms:play-services-wearable:10.2.0'

compile 'com.google.android.gms:play-services-location:10.2.0'
// Mapbox dependencies
compile('com.mapbox.mapboxsdk:mapbox-android-sdk:5.0.0@aar') {
transitive = true
Expand Down
8 changes: 8 additions & 0 deletions MapboxAndroidWearDemo/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@
android:value="com.mapbox.mapboxandroiddemo.MainActivity"/>
</activity>

<activity
android:name="com.mapbox.mapboxandroiddemo.examples.LocationTrackingActivity"
android:label="@string/activity_location_tracking_title">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mapbox.mapboxandroiddemo.MainActivity"/>
</activity>

<service android:name="com.mapbox.services.android.telemetry.service.TelemetryService"/>

</application>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.support.wearable.view.WearableRecyclerView;

import com.mapbox.mapboxandroiddemo.adapter.ExampleAdapter;
import com.mapbox.mapboxandroiddemo.examples.LocationTrackingActivity;
import com.mapbox.mapboxandroiddemo.examples.MapFragmentActivity;
import com.mapbox.mapboxandroiddemo.examples.OfflineMapActivity;
import com.mapbox.mapboxandroiddemo.examples.SimpleMapViewActivity;
Expand Down Expand Up @@ -38,6 +39,8 @@ protected void onCreate(Bundle savedInstanceState) {
R.drawable.simple_map_view_screen, new Intent(MainActivity.this, MapFragmentActivity.class)));
exampleItemModels.add(new ExampleItemModel(R.string.activity_map_offline_title,
R.drawable.simple_map_view_screen, new Intent(MainActivity.this, OfflineMapActivity.class)));
exampleItemModels.add(new ExampleItemModel(R.string.activity_location_tracking_title,
R.drawable.simple_map_view_screen, new Intent(MainActivity.this, LocationTrackingActivity.class)));

ExampleAdapter exampleAdapter = new ExampleAdapter(MainActivity.this, exampleItemModels);
wearableRecyclerView.setAdapter(exampleAdapter);
Expand Down
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);
}
}
}
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);
}
}
1 change: 1 addition & 0 deletions MapboxAndroidWearDemo/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<string name="activity_simple_mapview_title">A simple map view</string>
<string name="activity_map_fragment_title">Map fragment</string>
<string name="activity_map_offline_title">Offline map</string>
<string name="activity_location_tracking_title">Location tracking</string>
<string name="download">Download</string>
<string name="list">List</string>

Expand Down

0 comments on commit 9f04cdf

Please sign in to comment.