140 likes | 348 Views
Location based services. Using Google Maps v2 etc. in Android apps. Some location-based services available in Android. Google maps Showing and annotating maps Geocoding Address -> location Reverse geocoding Location -> address(es) Getting live location information
E N D
Location based services Using Google Maps v2 etc. in Android apps Location based services
Some location-based services available in Android • Google maps • Showing and annotating maps • Geocoding • Address -> location • Reverse geocoding • Location -> address(es) • Getting live location information • Through GPS, mobile phone cell tower triangulation, or Wi-Fi positioning Location based services
Google maps • Google maps are NOT part of the Android API • It’s an add-on • This means that you will have to do a number of things to make it work … Location based services
Google Maps API versions • Google Maps Android API • MapActivity, Overlay, etc. • Deprecated • Google Maps Android API v2 • MapFragment, etc. • https://developers.google.com/maps/documentation/android/ • Google Maps for JavaScript v2 • Deprecated • Google Maps for JavaScript v3 • https://developers.google.com/maps/documentation/javascript/ Location based services
Some Map related classes • Example: maps2013 • GoogleMap • This is the main class • setOnMapClickListener(…) • Hitting the map you can add markers, etc. • MyLocation • Will send you to your current location • Requires permissions: ACCESS_COARSE_LOCATION and ACCESS_FINEL_LOCATION • UISettings • To change the default settings of the map • MapFragment • To show the map as part of an activity • Not used in the layout.xml (only “fragment”) • Used in the Activity (Java code) • Android ≥ 3 (API 11) only • SuppportMapFragment for older devices • MapView • Another way to show a map Location based services
Markers • Marker • An annotation on a map • Map.setOnMarkerClickListener(…) • Event when you hit a marker • MarkerOptions • The “constructor” parameters for Marker objects • Position, title, icon, etc. • Circle, and other annotation • You can add circles (and many other annotation) to a map Location based services
How to make it work …when programming • Google Maps API v2 is part of Google Play services SDK • Use the Android SDK Manager (from Eclipse -> Window) to obtain the ”Google Play Services” API • It’s in “Extra” at the bottom of the screen • http://www.vogella.com/articles/AndroidGoogleMaps/article.html#installmaps • This will download a project which you will have to import into Eclipse • In your app project you must specify the downloaded project as a library. Location based services
How to make it work …when running • Google Maps API key • SHA1 + applications package name gives you a key • http://www.vogella.com/articles/AndroidGoogleMaps/article.html#maps_key • You can find the SHA1 from Eclipse • Window -> Preferences -> Android -> Build • https://code.google.com/apis/console/ • The key must be placed in AndroidManifest.xml • Permissions to use Internet, etc. • Stated in AndroidManifest.xml • Google Maps requires Open GL • Stated in AndroidManifest.xml Location based services
Geocoding and reverse geocoding • Geocoder geocoder = new Geocoder(context, locale) • Geocoding • Address -> position • geocoder.getFromLocation(latitude, longitude, howMany) • Reverse geocoding • Position -> Address(es) • geocoder.getFromLocationName(location, howMany); • I had problems • ”Service not Available” in my emulator • Works on my phone. Location based services
Getting location data:Where am I? • Some applications needs to know the current position of the user’s device. • Three ways to obtain the position • GPS satellite • Most accurate • Mobile phone cell tower triangulation • Works indoors • Wi-Fi • The address of the connected Wi-Fi should be known • Least accurate • AndroidManifest.xml • <uses-permissionandroid:name="android.permission.ACCESS_COARSE_LOCATION" /> • <uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION" /> Location based services
Location related classes and interfaces • locationClient.requestLocationUpdates( LocationRequest, LocationListener) • LocationRequest • Defines how often, how accurate, etc. • LocationListener • Notified when the location has changed • onLocationChanged(Location newLocation) • Geofence • Geographical region • Notifications when device crosses the boundary (entering or leaving) • Example: Maps2013 Location based services
Different location providers:Pros and cons • GPS_PROVIDER • Accurate • Works only outdoors • Device must have satellite connection • Consumes battery • Satellite connection consumes battery • Slow • NETWORK_PROVIDER • Less accurate • Works indoors (Wi-Fi + cell-tower) and outdoors (cell-tower) • Consumes less battery • Faster • Source • http://developer.android.com/guide/topics/location/obtaining-user-location.html Location based services
Geocoding • Getting real addresses from a position • GeoCoder • List<Address> getFromLocationName(name, howMany) • Geocoding • List<Address> getFromLocation(lat, long, howMany) • Reverse geocoding • Example: maps2013 Location based services
References and further readings • Google Maps Android API v2 • https://developers.google.com/maps/documentation/android/ • Lars Vogel: Google Maps Android API v2 – Tutorial • http://www.vogella.com/articles/AndroidGoogleMaps/article.html • George Mathew: Remove a single marker from Google Maps Android API V2 … • http://www.vogella.com/articles/AndroidGoogleMaps/article.html • Making your app location-aware • http://developer.android.com/training/location/index.html Location based services