Android App Development 04 : Location API


Published on

1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Android App Development 04 : Location API

  1. 1. 4 Location API Anuchit Chalothorn anoochit@gmail.comLicensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  2. 2. Determine the current locationMost Android devices allow to determine thecurrent geolocation. This can be done via aGPS (Global Positioning System) module, viacell tower triangulation or via wifi networks.Ref:
  3. 3. Location ManagerThe LocationManager class provides access tothe Android location service. This servicesallows to access location providers, to registerlocation update listeners and proximity alertsand more.
  4. 4. LocationProviderThe LocationProvider class is the superclass ofthe different location providers which deliver theinformation about the current location. Thisinformation is stored in the Location class.
  5. 5. LocationProviderLocationProvider Descriptionnetwork Uses the mobile network or WI-Fi to determine the best location. Might have a higher precision in closed rooms then GPS.gps Use the GPS receiver in the Android device to determine the best location via satellites. Usually better precision than network.passive Allows to participate in location of updates of other components to save energy
  6. 6. Selecting LocationProvider via CriteriaFor a flexible selection of the best locationprovider use a Criteria object, in which you candefine how the provider should be selected.
  7. 7. PermissionIf you want to access the GPS sensor, youneed the ACCESS_FINE_LOCATIONpermission. Otherwise you need theACCESS_COARSE_LOCATION permission.
  8. 8. Prompt the user to Enabled GPSLocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);boolean enabled = service .isProviderEnabled(LocationManager.GPS_PROVIDER);if (!enabled) { Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivity(intent);}
  9. 9. Workshop: Get current locationUse criteria find the best provider then getcurrent position from it.LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);Criteria criteria = new Criteria();String provider = locationManager.getBestProvider(criteria, true);Location location = locationManager.getLastKnownLocation(provider);float lat = (float) location.getLatitude(); float lon = (float) location.getLongitude();
  10. 10. Install Google Play services
  11. 11. Google MapsGoogle provides via Google play a library forusing Google Maps in your application. Thefollowing description is based on the GoogleMaps Android API v2 which provides significantimprovements to the older API version.Ref:
  12. 12. Getting the Google Map keyTo use Google Maps you need to create a validGoogle Maps API key. The key is free, you canuse it with any of your applications that call theMaps API, and it supports an unlimited numberof users.
  13. 13. SHA-1 for your signature keykeytool -list -v -alias androiddebugkey -keystore ~/.android/debug.keystore -storepass android -keypass android
  14. 14. Work with Google Map● Register your app and sha1 for publish keystore and debug keystore● Import Google Play Service library● Add some permission and information● Add MapView to layout● then coding
  15. 15. MapViewThe library provides the class and theMapView class for displaying the mapcomponent.<fragment android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" class="" />
  16. 16. Permission
  17. 17. API Key
  18. 18. Map & Markerstatic final LatLng SIPA = new LatLng(13.885245,100.565586);GoogleMap map = ((MapFragment) getFragmentManager().findFragmentById(;if (map!=null) { Marker sipa = map.addMarker(new MarkerOptions().position(SIPA).title("SIPA"));}
  19. 19. End