android app development 04 : location api

27
Location API Anuchit Chalothorn [email protected] 4 Licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

Upload: anuchit-chalothorn

Post on 14-May-2015

549 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Android App Development 04 : Location API

Location APIAnuchit [email protected]

4

Licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

Page 2: Android App Development 04 : Location API

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

Page 3: Android App Development 04 : Location API

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.

Page 4: Android App Development 04 : Location API

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.

Page 5: Android App Development 04 : Location API

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

Page 6: Android App Development 04 : Location API

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.

Page 7: Android App Development 04 : Location API

Permission

If you want to access the GPS sensor, you need the ACCESS_FINE_LOCATION permission. Otherwise you need the ACCESS_COARSE_LOCATION permission.

Page 8: Android App Development 04 : Location API

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);}

Page 10: Android App Development 04 : Location API

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();

Page 12: Android App Development 04 : Location API

Install Google Play services

Page 13: Android App Development 04 : Location API

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/

Page 14: Android App Development 04 : Location API

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.

Page 15: Android App Development 04 : Location API
Page 16: Android App Development 04 : Location API
Page 17: Android App Development 04 : Location API
Page 18: Android App Development 04 : Location API

SHA-1 for your signature key

keytool -list -v -alias androiddebugkey -keystore ~/.android/debug.keystore -storepass android -keypass android

Page 19: Android App Development 04 : Location API
Page 20: Android App Development 04 : Location API
Page 21: Android App Development 04 : Location API

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

Page 22: Android App Development 04 : Location API

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" />

Page 23: Android App Development 04 : Location API

Permission

Page 24: Android App Development 04 : Location API

API Key

Page 25: Android App Development 04 : Location API

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"));}

Page 27: Android App Development 04 : Location API

End