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

Slides:



Advertisements
Similar presentations
Android Application Development Tutorial. Topics Lecture 4 Overview Overview of Sensors Programming Tutorial 1: Tracking location with GPS and Google.
Advertisements

CSE2102 Introduction to Software Engineering Lab 2 Sept/4/2013.
Programming with Android: Activities
All About Android Introduction to Android 1. Creating a New App “These aren’t the droids we’re looking for.” Obi-wan Kenobi 1. Bring up Eclipse. 2. Click.
Android Programming Beomjoo Seo Sep., 12 CS5248 Fall 2012.
Location Services: Part 1 (Location and Geocoding)
Software Architecture of Android Yaodong Bi, Ph.D. Department of Computing Sciences University of Scranton.
Mobile Programming Lecture 9 Bound Service, Location, Sensors, IntentFilter.
Mobile Programming Lecture 16 The Facebook API. Agenda The Setup Hello, Facebook User Facebook Permissions Access Token Logging Out Graph API.
CSS216 MOBILE PROGRAMMING Android, Chapter 8 Book: “Professional Android™ 2 Application Development” by Reto Meier, 2010 by: Andrey Bogdanchikov (
Cosc 5/4730 Introduction: Threads, Android Activities, and MVC.
Android Accessing GPS Ken Nguyen Clayton State University 2012.
1 CMSC 628: Introduction to Mobile Computing Nilanjan Banerjee Introduction to Mobile Computing University of Maryland Baltimore County
Android Programming-Activity Lecture 4. Activity Inside java folder Public class MainActivity extends ActionBarActivity(Ctrl + Click will give you the.
Services A Service is an application component that can perform long-running operations in the background and does not provide a user interface. An application.
Maps Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License.
Android Boot Camp Demo Application – Part 1. Development Environment Set Up Download and install Java Development Kit (JDK) Download and unzip Android.
Android - Location Based Services. Google Play services facilitates adding location awareness to your app with automated location tracking Geo fencing.
Android Application Lifecycle and Menus
TCS Internal Maps. 2 TCS Internal Objective Objective :  MAPS o Integration of Maps.
Android Alert Dialog. Alert Dialog Place Button to open the dialog. public class MainActivity extends ActionBarActivity { private static Button button_sbm;
Lecture 4: Sensors Topics: Motion, Position, and Environmental Sensors Date: Feb 11, 2016.
Cosc 4735 LocationAware API. Previous on … Before we looked at GPS location. – d-gpslocation.pptx
Location Based Services. Android location APIs make it easy for you to build location-aware applications, without needing to focus on the details of the.
Lecture 5: Location Topics: Google Play Services, Location API Date: Feb 16, 2016.
Location based services 1. Some location-based services available in Android Geo-coding – Address -> location Reverse geo-coding – Location -> address(es)
USING ANDROID WITH THE INTERNET. Slide 2 Lecture Summary Getting network permissions Working with the HTTP protocol Sending HTTP requests Getting results.
3 rd -party APIs Kalin Kadiev Astea Solutions AD.
CS378 - Mobile Computing Location (Location, Location, Location)
1. 2 Android location services Determining a device’s current location Tracking device movements Proximity alerts.
CS499 – Mobile Application Development Fall 2013 Location & Maps.
Introduction to Android Programming
Lecture 4: Sensors Topics: Motion, Position, and Environmental Sensors
Lecture 5: Location Topics: Google Play Services, Location API
Location Services: Part 1 (Location and Geocoding)
Randy Dalrymple HillyRoad, LLC
Introduction to android
Google VR (gvr) CardBoard and DayDream With OpenGL
Android Application -Architecture.
Concurrency in Android
Android Application Maps 1.
Location-Based Services: Part 2 (Google Maps)
Broadcast receivers.
Android – Event Handling
Lecture 4: Sensors Topics: Motion, Position, and Environmental Sensors.
Activities and Intents
Android Location Based Services
Android 20: Location Kirk Scott.
Sensors, maps and fragments:
Reactive Android Development
Picasso Revisted.
CIS 470 Mobile App Development
CIS 470 Mobile App Development
Activities and Intents
CIS 470 Mobile App Development
Lecture 4: Sensors Topics: Motion, Position, and Environmental Sensors.
CIS 470 Mobile App Development
Lecture 5: Location Topics: Google Play Services, Location API.
Android Developer Fundamentals V2 Lesson 5
Service Services.
Mobile Programming Dr. Mohsin Ali Memon.
SE4S701 Mobile Application Development
Cosc 4730 An Introduction.
Activities and Fragments
Activities, Fragments, and Intents
CIS 470 Mobile App Development
External Services CSE 5236: Mobile Application Development
Mobile Programming Broadcast Receivers.
CIS 694/EEC 693 Android Sensor Programming
External Services CSE 5236: Mobile Application Development
Presentation transcript:

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

Location Estimation GPS Cellular WiFi

Location Estimation 12 10 7

Location Estimation 12 10 7

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

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

0. Setup Google Play Location Services1 Install Google Play Services SDK Manager > SDK Tools Select Google Play Services, Check, Apply. Edit build.gradle (Module:app) Add new dependency and Sync. Specify permission in AndroidManifest.xml Goes inside <manifest> but outside <application> tag. compile 'com.google.android.gms:play-services:8.4.0' <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 1For 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 add() build() add() BobTheChairBuilder .addSawTool() .addLog() .build()

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

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

1. Getting Last Known Location Step 1.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) {} }

1. Getting Last Known Location Step 1.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(); } }

1. Getting Last Known Location Step 1.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(); } } Note: Manually setting permissions (using Settings> Apps> MyApp> permissions) vs. using programs to ask for permissions.

Code Practice 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 Preferred Rate Max Rate Priority Your Location App (lat, long) Question: What do you think Android does? App #1 Pref: 100 ms Max: 20 ms App #2 Pref: 50 ms Max: 30 ms

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

2. Set Up a Location Request Step 2.1: Create a LocationRequest object Step 2.2: Check current settings. Step 2.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);

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

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() Sitterson Hall 0:"UNC Sitterson Hall", 1:"Chapel Hill, NC 27514", ….

4. Displaying a Location Address Goecoder converts Lat/Long to an Address Geocoder g = new Geocoder(this, Locale.getDefault()); try { List<Address> 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.

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 Get location updates (lat, long) Print the Address!

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