• Like
Android App Development 04 : Location API
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Android App Development 04 : Location API



  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads


Total Views
On SlideShare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


  • 1. 4 Location API Anuchit Chalothorn anoochit@gmail.comLicensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  • 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: http://developer.android.com/guide/topics/location/index.html
  • 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. 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. 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. 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. PermissionIf you want to access the GPS sensor, youneed the ACCESS_FINE_LOCATIONpermission. Otherwise you need theACCESS_COARSE_LOCATION permission.
  • 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. 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. Install Google Play services
  • 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: https://developers.google.com/maps/documentation/android/
  • 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. SHA-1 for your signature keykeytool -list -v -alias androiddebugkey -keystore ~/.android/debug.keystore -storepass android -keypass android
  • 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. MapViewThe library provides the com.google.android.gms.maps.MapFragment class and theMapView class for displaying the mapcomponent.<fragment android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" class="com.google.android.gms.maps.MapFragment" />
  • 16. Permission
  • 17. API Key
  • 18. Map & Markerstatic final LatLng SIPA = new LatLng(13.885245,100.565586);GoogleMap map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();if (map!=null) { Marker sipa = map.addMarker(new MarkerOptions().position(SIPA).title("SIPA"));}
  • 19. End