Location based services

advertisement

Location based services

Using Google Maps v2 etc. in Android apps

Location based services 1

Some location-based services available in Android

• Google maps

– Showing and annotating maps

• Geo-coding

– Address -> location

• Reverse geo-coding

– Location -> address(es)

• Getting live location information

– Through GPS, mobile phone cell tower triangulation, or Wi-Fi positioning

Location based services 2

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 3

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/and roid/

• Google Maps for JavaScript v2

– Deprecated

• Google Maps for JavaScript v3

– https://developers.google.com/maps/documentation/java script/

Location based services 4

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 5

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 6

How to make it work … when programming

• Google Maps API v2 is part of Google Play services SDK

– Use the Android SDK Manager to obtain the

”Google Play Services” API

• It’s in “Extra” at the bottom of the screen

– http://www.vogella.com/articles/AndroidGoogleM aps/article.html#installmaps

• Android Studio can create a special Map activity

– This is quite helpful

Location based services 7

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#m aps_key

– 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

• Genymotion: Has no access to GooglePlay

– It should be possible to upgrade GenyMotion, but I can’t make it work

– It run the app on my Android phone

Location based services 8

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-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

– <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Location based services 9

Getting location updates

• locationManager.requestLocationUpdates( … )

• http://developer.android.com/reference/android/locati on/LocationManager.html

Example: Maps3

Location based services 10

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-userlocation.html

Location based services 11

Geocoding

• Getting real addresses from a position

• GeoCoder

– List<Address> getFromLocationName(name, howMany)

• Geocoding

• Address includes ordinary address + (lat, long)

– List<Address> getFromLocation(lat, long, howMany)

• Reverse geocoding

– Example: Maps3

Location based services 12

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.ht

ml

• George Mathew: Remove a single marker from Google

Maps Android API V2 …

– http://www.vogella.com/articles/AndroidGoogleMaps/article.ht

ml

• Making your app location-aware

– http://developer.android.com/training/location/index.html

Location based services 13

Download