Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 5: Location Topics: Google Play Services, Location API Date: Feb 16, 2016.

Similar presentations


Presentation on theme: "Lecture 5: Location Topics: Google Play Services, Location API Date: Feb 16, 2016."— Presentation transcript:

1 Lecture 5: Location Topics: Google Play Services, Location API Date: Feb 16, 2016

2 http://developer.android.com/training/location/index.html http://developer.android.com/training/location/retrieve-current.html http://developer.android.com/training/location/change-location-settings.html http://developer.android.com/training/location/receive-location-updates.html http://developer.android.com/training/location/display-address.html References (study these) Read Everything Skim Codes Not-shown

3 Two API Options 1.Android API (not rich enough) 2.Google Play Services Location API (better) Tracking Geofencing Activity Recognition (walking, biking, driving …)

4 Making Your App Location Aware 0. Setup Google Play/Dependencies/Permissions… 1.Getting Last Known Location Usually the same as the current location 2.Changing Location Settings Detect and apply system settings for location features 3.Receiving Location Updates Request and received periodic location updates 4.Displaying a Location Address Converting long/lat to an address (reverse geocoding) 5.Creating and Monitoring Geofences Defining and dealing with geofences and user locations

5 0. Setup Google Play Location Services 1 1.Install Google Play Services SDK Manager > SDK Tools Select Google Play Services, Check, Apply. 2.Edit build.gradle (Module:app) Add new dependency and Sync. 3.Specify permission in AndroidManifest.xml Add new dependency and Sync. compile 'com.google.android.gms:play-services:8.4.0' 1 For more details – Setting Up Google Play Services: https://developers.google.com/android/guides/setup https://developers.google.com/android/guides/setup

6 Google Play Services – Basics https://developers.google.com/android/guides/api-client

7 Google Play Services – Basics BobTheChairBuilder.addSawTool().addLog().build() build() add()

8 Google Play Services – Basics GoogleApiClientBuilder.addConnectionCallback().addConnectionFailedListener().addApi().build() implements ConnectionCallbacks implements ConnectionFailedListener API GoogleApiClient

9 Google Play Services – Basics GoogleApiClientBuilder.addConnectionCallback().addConnectionFailedListener().addApi().build() API GoogleApiClient connect() disconnect() ConnectionCallbacks onConnected() onConnectionSuspended() OnConnectionFailedListener onConnectionFailed()

10 1. Getting Last Known Location Step 1: Build a GoogleApiClient public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{ private GoogleApiClient c = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (c == null) { c = new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build(); } } @Override public void onConnected(Bundle bundle) {} @Override public void onConnectionSuspended(int i) {} @Override public void onConnectionFailed(ConnectionResult connectionResult) {} }

11 1. Getting Last Known Location Step 2: connect() and disconnet() GoogleApiClient public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{ private GoogleApiClient c = null; @Override protected void onCreate(Bundle savedInstanceState) { // code omitted (see last slide) } @Override public void onConnected(Bundle bundle) {} @Override public void onConnectionSuspended(int i) {} @Override public void onConnectionFailed(ConnectionResult connectionResult) {} @Override protected void onStart() { c.connect(); super.onStart(); } @Override protected void onStop() { c.disconnect(); super.onStop(); } }

12 1. Getting Last Known Location Step 3: Get the location from LocationServices.FusedLocationApi @Override public void onConnected(Bundle bundle) { try { Location loc = LocationServices.FusedLocationApi.getLastLocation(c); Log.v(“LOC", "" + loc.getLatitude() + ", " + loc.getLongitude()); }catch (SecurityException ex) { ex.printStackTrace(); } }

13 Code Practice 1.Setup App (Google Play Location Services etc.) 2.Connect to the Location Service 3.Print the location: lat/long 4.Show the location on a map (using Intent)

14 2. Set Up a Location Request Your Location App Your Location App Preferred Rate Max Rate Priority App #1 Pref: 100 ms Max: 20 ms App #1 Pref: 100 ms Max: 20 ms App #2 Pref: 50 ms Max: 30 ms App #2 Pref: 50 ms Max: 30 ms Question: What do you think Android does? (lat, long)

15 2. Set Up a Location Request Your Location App Your Location App Preferred Rate Max Rate Priority (lat, long) PriorityAccuracyPowerApproach PRIORITY_BALANCED_POWER_ACCURACY100m (block)LowWiFi+Cell PRIORITY_HIGH_ACCURACYGPS PRIORITY_LOW_POWER10 km (city)Low PRIORITY_NO_POWER-NegligibleOther apps “Potential Research Topic”

16 2. Set Up a Location Request Step 1: Create a LocationRequest object Step 2: Check current settings. Step 3: Prompt the user to change settings. LocationRequest req = new LocationRequest(); req.setInterval(10000); //preferred rate req.setFastestInterval(5000); //max rate it can handle req.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

17 3. Receive Location Updates public class MainActivity extends ActionBarActivity implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener {... @Override public void onLocationChanged(Location location) { //do something with the location } } Step 1: Implement LocationListener interface Step 2: Start and stop listening! LocationServices.FusedLocationApi.requestLocationUpdates( c, req, this); LocationServices.FusedLocationApi.removeLocationUpdates( c, this); onPause() onResume()

18 4. Displaying a Location Address Geocoder getFromLocation()Input: Output: 35.9001114, -79.0672419 0:”1100 N Carolina 54” 1:"Chapel Hill, NC 27516“ 2:"USA" getFromLocationName()Input: Output: Sitterson Hall 0:"UNC Sitterson Hall", 1:"Chapel Hill, NC 27514", 2:"USA" ….

19 Goecoder converts Lat/Long to an Address 4. Displaying a Location Address Geocoder g = new Geocoder(this, Locale.getDefault()); try { List la = g.getFromLocation(location.getLatitude(), location.getLongitude(), 1); Log.v("Address", la.get(0).toString()); }catch (Exception ex) { } Warning: This may take a long time. Use a background service or AsyncTask or a Thread.

20 Geofencing combines awareness of the user's current location with awareness of the user's proximity to locations that may be of interest. 5. Geofencing Lat, Long, and Radius (m) 100 geofences per device user Events: Entry Exit Dwell (specify duration) Expiration (ms) Use: Advertisement Coupons Avoid dangerous area

21 Code Practice 1.Get location updates (lat, long) 2.Print the Address!


Download ppt "Lecture 5: Location Topics: Google Play Services, Location API Date: Feb 16, 2016."

Similar presentations


Ads by Google