android app development 04 : location api
TRANSCRIPT
Location APIAnuchit [email protected]
4
Licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
Determine the current location
Most Android devices allow to determine the current geolocation. This can be done via a GPS (Global Positioning System) module, via cell tower triangulation or via wifi networks.
Ref: http://developer.android.com/guide/topics/location/index.html
Location Manager
The LocationManager class provides access to the Android location service. This services allows to access location providers, to register location update listeners and proximity alerts and more.
LocationProvider
The LocationProvider class is the superclass of the different location providers which deliver the information about the current location. This information is stored in the Location class.
LocationProvider
LocationProvider Description
network 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
Selecting LocationProvider via Criteria
For a flexible selection of the best location provider use a Criteria object, in which you can define how the provider should be selected.
Permission
If you want to access the GPS sensor, you need the ACCESS_FINE_LOCATION permission. Otherwise you need the ACCESS_COARSE_LOCATION permission.
Prompt the user to Enabled GPS
LocationManager 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);}
Workshop: Get current location
Use criteria find the best provider then get current 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();
Install Google Play services
Google Maps
Google provides via Google play a library for using Google Maps in your application. The following description is based on the Google Maps Android API v2 which provides significant improvements to the older API version.
Ref: https://developers.google.com/maps/documentation/android/
Getting the Google Map key
To use Google Maps you need to create a valid Google Maps API key. The key is free, you can use it with any of your applications that call the Maps API, and it supports an unlimited number of users.
SHA-1 for your signature key
keytool -list -v -alias androiddebugkey -keystore ~/.android/debug.keystore -storepass android -keypass android
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
MapView
The library provides the com.google.android.gms.maps.MapFragment class and the MapView class for displaying the map component.
<fragment android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" class="com.google.android.gms.maps.MapFragment" />
Permission
API Key
Map & Marker
static 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"));}
End