Docsity

Setting Up and Using Location Services with Google Maps API for Android, Lecture notes of Mobile Computing

Partial preview of the text

Download Setting Up and Using Location Services with Google Maps API for Android and more Lecture notes Mobile Computing in PDF only on Docsity!

1

Chapter 07

Location and Maps

Tagele B.

2

Android: Localization basics

Geolocalization  Identification of the real-world geographic location of an end-user. 

Geolocalization  Identification of the

real-world geographic location of an the

end-user.

location-based services (LBS): enable

you to find the device’s current location.

How? You can specify which technology to

use

 (^) explicitly by name  (^) LOCALIZATION THROUGH GPS  (^) LOCALIZATION THROUGH WI-FI  (^) LOCALIZATION THROUGH CELLULAR NETWORK  (^) LOCALIZATION THROUGH GPS  (^) LOCALIZATION THROUGH WI-FI  (^) LOCALIZATION THROUGH CELLULAR NETWORK

4

Android: Installing Google APIs

EP -1 : Install and Setup Google Play Service SDK Window  Android SDK Manager Installed packages Check Google Play and Google Repository are installed , or install them otherwise Check Google Play and Google Repository are installed , or install them otherwise http://developer.android.com/google/play- services/setup.html

5

Getting a Google Play API Key

 (^) Note: Existing keys from a Google Maps Android v1 application, commonly known as MapView, will not work with the v2 API. The Google Maps Android API uses a new system of managing keys.  (^) Note: You need an Android API key , not a browser key. You can use the same API key for your Google Maps Android API apps and your Google Places API for Android apps.  (^) Access your project's API keys and other credentials as follows:

1. Go to the Google Developers _Console(https://console.developers.google.com/)

  1. In the sidebar on the left, select Credentials.
  2. If your project doesn't already have an Android API key,_ create one now by selecting Add credentials > API key > _Android key.
  3. In the resulting dialog, enter your app's SHA-1 fingerprint_ and package name. For ample: B:0D:AC:74:D3:21:E1:43:67:71:9B:62:91:AF:A1:66:6E:44:5D: com.example.android.mapexample

7

USING LOCATION-BASED SERVICES

 “ Location-based services ” is an umbrella term that describes the different technologies you can use to find a device’s current location.  (^) The two main LBS elements are:  (^) Location Manager - Provides hooks to the location- based services.  (^) Location Providers -Each of these represents a different location-fi nding technology used to determine the device’s current location.  (^) Using the Location Manager, you can do the following:  (^) Obtain your current location  (^) Follow movement  (^) Set proximity alerts for detecting movement into and out of a specified area  (^) Find available Location Providers

8  Access to the location-based services is provided by the Location Manager.  (^) To access the Location Manager , request an instance of the LOCATION_SERVICE using the getSystemService method, as  (^) Before you can use the location-based services, you need to add one or more uses-permission tags to your manifest.  (^) The following snippet shows how to request the fi

10

SELECTING A LOCATION PROVIDER

 Depending on the device, you can use several technologies to determine the current location. Each technology, available as a Location Provider , offers different capabilities including differences in power consumption, accuracy, and the ability to determine altitude, speed, or heading information.  (^) Finding Location Providers  The LocationManager class includes static string constants that return the provider name for three  (^) Location Providers:  (^) LocationManager.GPS_PROVIDER  (^) LocationManager.NETWORK_PROVIDER  (^) LocationManager.PASSIVE_PROVIDER  The GPS and passive provider requires fine

11  To get a list of the names of all the providers available (based on hardware available on the device,and the permissions granted the application), call getProviders , using a Boolean to indicate if you want all, or only the enabled , providers to be returned:  (^) Finding Location Providers by Specifying Criteria  (^) Use the Criteria class to dictate the requirements of a provider in terms of accuracy, power use(low, medium, high), financial cost, and the ability to return values for altitude , speed , and heading.  (^) Specifies Criteria requiring

13  In terms of horizontal and vertical accuracy ,  (^) high accuracy represents a requirement for results correct to within 100m.  (^) Low accuracy Providers are correct to more than 500m,  (^) Medium accuracy Providers represent accuracy between 100 and 500 meters.  (^) When specifying accuracy requirements for bearing and speed , only ACCURACY_LOW and ACCURACY_HIGH are valid parameters.  (^) Having defined the required Criteria, you can use  (^) getBestProvider to return the best matching Location Provider or  (^) getProviders to return all the possible matches.  (^) getBestProvider to return the best Provider for

14  If more than one Location Provider matches your Criteria,  The one with the greatest accuracy is returned. 

If no Location Providers meet your

requirements, the Criteria are loosened, in

the following order, until a provide

 (^) Power use 

Accuracy of returned location

Accuracy of bearing, speed, and altitude

Availability of bearing, speed, and

altitude r is found:

 (^) The criterion for allowing a device with monetary cost is never implicitly relaxed. If no provider is

16

Finding the Last Known Location

 You can find the last location fix obtained by a particular Location Provider using the get Last- Known Location method , passing in the name of the Location Provider. The following example finds the last location fix taken by the GPS provider:  getLastKnownLocation does not ask the Location Provider to update the current position. If the device has not recently updated the current position, this value may not exist or be out of date.  (^) The Location object returned includes all the position information available from the provider that supplied it. This can include

17

Where Am I Example

 finds the device’s last known location using the GPS Location Provider.  (^) This example assumes that  (^) you have enabled the GPS_PROVIDER Location Provider using the techniques or that you’re running it on a device that supports GPS and has that hardware enabled.  (^) In order to work, the device or Emulator must have recorded at least one location update. In the case of a device, this is most easily achieved by starting the Google Maps application; on an Emulator, enable location updates as described

  1. Create a new Where Am I project with a WhereAmI Activity. This example uses the GPS provider,
  2. so you need to include the uses-permission tag for ACCESS_FINE_LOCATION in your application

19

  1. Modify the main.xml layout resource to include an android:ID attribute for the TextView control so that you can access it from within the Activity.

20  (^) Override the onCreate method of the WhereAmI Activity to get a reference to the Location Manager.  (^) Define LocationManager and Provider Class  (^) Call getLastKnownLocation to get the last known location , and pass it in to an updateWithNewLocation method stub.