Lecture 5 Location Topics Google Play Services Location

  • Slides: 24
Download presentation
Lecture 5: Location Topics: Google Play Services, Location API

Lecture 5: Location Topics: Google Play Services, Location API

Location Estimation GPS Cellular Wi. Fi

Location Estimation GPS Cellular Wi. Fi

Location Estimation 12 10 7

Location Estimation 12 10 7

Location Estimation 12 10 7

Location Estimation 12 10 7

Two API Options 1. Android API (not rich enough) 2. Google Play Services Location

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

Making Your App Location Aware 0. Setup Google Play/Dependencies/Permissions… 1. Getting Last Known Location

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

0. Setup Google Play Location Services 1 1. Install Google Play Services • SDK

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. compile 'com. google. android. gms: play-services: 8. 4. 0' 3. Specify permission in Android. Manifest. xml • Goes inside <manifest> but outside <application> tag. <uses-permission android: name="android. permission. ACCESS_FINE_LOCATION"/> 1 For more details – Setting Up Google Play Services: https: //developers. google. com/android/guides/setup

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

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

Google Play Services – Basics add() build() add() Bob. The. Chair. Builder. add. Saw.

Google Play Services – Basics add() build() add() Bob. The. Chair. Builder. add. Saw. Tool(). add. Log(). build()

Google Play Services – Basics implements Connection. Callbacks Google. Api. Client implements Connection. Failed.

Google Play Services – Basics implements Connection. Callbacks Google. Api. Client implements Connection. Failed. Listener API Google. Api. Client. Builder. add. Connection. Callback(). add. Connection. Failed. Listener(). add. Api(). build()

Google Play Services – Basics Connection. Callbacks on. Connected() on. Connection. Suspended() Google. Api.

Google Play Services – Basics Connection. Callbacks on. Connected() on. Connection. Suspended() Google. Api. Client connect() On. Connection. Failed. Listener disconnect() on. Connection. Failed() API Google. Api. Client. Builder. add. Connection. Callback(). add. Connection. Failed. Listener(). add. Api(). build()

1. Getting Last Known Location Step 1. 1: Build a Google. Api. Client public

1. Getting Last Known Location Step 1. 1: Build a Google. Api. Client public class Main. Activity extends App. Compat. Activity implements Google. Api. Client. Connection. Callbacks, Google. Api. Client. On. Connection. Failed. Listener{ private Google. Api. Client c = null; @Override protected void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. activity_main); if (c == null) { c = new Google. Api. Client. Builder(this). add. Connection. Callbacks(this). add. On. Connection. Failed. Listener(this). add. Api(Location. Services. API). build(); } } @Override public void on. Connected(Bundle bundle) {} @Override public void on. Connection. Suspended(int i) {} @Override public void on. Connection. Failed(Connection. Result connection. Result) {} }

1. Getting Last Known Location Step 1. 2: connect() and disconnet() Google. Api. Client

1. Getting Last Known Location Step 1. 2: connect() and disconnet() Google. Api. Client public class Main. Activity extends App. Compat. Activity implements Google. Api. Client. Connection. Callbacks, Google. Api. Client. On. Connection. Failed. Listener{ private Google. Api. Client c = null; @Override protected void on. Create(Bundle saved. Instance. State) { // code omitted (see last slide) } @Override public void on. Connected(Bundle bundle) {} @Override public void on. Connection. Suspended(int i) {} @Override public void on. Connection. Failed(Connection. Result connection. Result) {} @Override protected void on. Start() { c. connect(); super. on. Start(); } @Override protected void on. Stop() { c. disconnect(); super. on. Stop(); } }

1. Getting Last Known Location Step 1. 3: Get the location from Location. Services.

1. Getting Last Known Location Step 1. 3: Get the location from Location. Services. Fused. Location. Api @Override public void on. Connected(Bundle bundle) { try { Location loc = Location. Services. Fused. Location. Api. get. Last. Location(c); Log. v(“LOC", "" + loc. get. Latitude() + ", " + loc. get. Longitude()); }catch (Security. Exception ex) { ex. print. Stack. Trace(); } } Note: Manually setting permissions (using Settings> Apps> My. App> permissions) vs. using programs to ask for permissions.

Code Practice 1. 2. 3. 4. Setup App (Google Play Location Services etc. )

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

2. Set Up a Location Request Your Location App Preferred Rate Max Rate Priority

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

2. Set Up a Location Request Preferred Rate Max Rate Priority Your Location App

2. Set Up a Location Request Preferred Rate Max Rate Priority Your Location App (lat, long) Priority Accuracy Power Approach PRIORITY_BALANCED_POWER_ACCURACY 100 m (block) Low Wi. Fi+Cell PRIORITY_HIGH_ACCURACY PRIORITY_LOW_POWER PRIORITY_NO_POWER GPS 10 km (city) - Low Negligible “Potential Research Topic” Other apps

2. Set Up a Location Request Step 2. 1: Create a Location. Request object

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

3. Receive Location Updates Step 3. 1: Implement Location. Listener interface public class Main.

3. Receive Location Updates Step 3. 1: Implement Location. Listener interface public class Main. Activity extends Action. Bar. Activity implements Connection. Callbacks, On. Connection. Failed. Listener, Location. Listener { . . . @Override public void on. Location. Changed(Location location) { //do something with the location } } Step 3. 2: Start and stop listening! Location. Services. Fused. Location. Api. request. Location. Updates( c, req, this); on. Resume() Location. Services. Fused. Location. Api. remove. Location. Updates( c, this); on. Pause()

4. Displaying a Location Address Geocoder get. From. Location() Input: Output: 35. 9001114, -79.

4. Displaying a Location Address Geocoder get. From. Location() Input: Output: 35. 9001114, -79. 0672419 0: ” 1100 N Carolina 54” 1: "Chapel Hill, NC 27516“ 2: "USA" get. From. Location. Name() Input: Output: Sitterson Hall 0: "UNC Sitterson Hall", 1: "Chapel Hill, NC 27514", 2: "USA" ….

4. Displaying a Location Address • Goecoder converts Lat/Long to an Address Geocoder g

4. Displaying a Location Address • Goecoder converts Lat/Long to an Address Geocoder g = new Geocoder(this, Locale. get. Default()); try { List<Address> la = g. get. From. Location(location. get. Latitude(), location. get. Longitude(), 1); Log. v("Address", la. get(0). to. String()); }catch (Exception ex) { } Warning: This may take a long time. Use a background service or Async. Task or a Thread.

5. Geofencing • Geofencing combines awareness of the user's current location with awareness of

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

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

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

References (study these) • http: //developer. android. com/training/location/index. html • http: //developer. android. com/training/location/retrieve-current.

References (study these) • 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 Read Everything Skim Codes Not-shown