28
Location Based Services CPE 490/590 Smartphone Programming by Michael T. Shrove (UAHuntsville)

Location based services 10

Embed Size (px)

Citation preview

Location Based ServicesCPE 490/590 Smartphone Programmingby Michael T. Shrove (UAHuntsville)

Things you going to learn in Android

Displaying Google Maps

Track location via GPS or other services.

Displaying Maps

Download Google Play Services

Google Maps Android API V2 is part of Google’s Play services SDK, which you must download and configure to work with your existing Android SDK installation in Eclipse to use the mapping functions.

In Eclipse, choose Window > Android SDK Manager. In the list of packages that appears scroll down to theExtras folder and expand it.

Select the Google Play services checkbox and install the package.

Import Google Play Services into your workspace

Once Eclipse downloads and installs the Google Play services package, you can import it into your workspace.

Select File > Import > Android > Existing Android Code into Workspace then browse to the location of the downloaded Google Play services package on your computer.

It should be inside your downloaded Android SDK directory, at the following location:extras/google/google_play_services/libproject/google-play-services_lib.

Generate API keyTo access the Google Maps tools, you need an API key, which you can obtain through a Google account.

The key is based on your Android app debug or release certificate.

If you have released apps before, you might have used the keytool resource to sign them.

In that case, you can use the keystore you generated at that time to get your API key.

Obtain Google Maps Android API v2 Key

Navigate to https://code.google.com/apis/console

Create Project

Turn on Google Maps Android API v2

Select Services from the list on the left of the APIs console.

You should see a list of Google services, scroll down to Google Maps Android API V2 and click to turn it on for your account.

Create KeyNow we can get a key for the app.

Select API Access on the left-hand-side of the API console.

You may already see a key for browser apps, but we need one specifically for Android apps.

Near the bottom of the page, select Create new Android key.

Enter API Information

Enter you SHA1 certificate fingerprint

semicolon

Package name

Include Google Play Services lib into your project

Although we added the Google Play services package to the Eclipse workspace, we still need to setup this particular app to use it.

Select your new project in the Eclipse Package Explorer and configure its Properties(right-click > Properties or Window > Properties with the project selected).

Select the Android tab and scroll to the Library section, then choose Add.

Select the Google Play Services library to add it to your project.

Manifest Changes

Add the below changes to the manifest before application tag.

Add key to manifest

Add the api key to your application tag in the manifest.

Add the map to the app

<fragment xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:map="http://schemas.android.com/apk/res-auto"    android:id="@+id/the_map"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:name="com.google.android.gms.maps.MapFragment"    map:cameraTilt="45"    map:cameraZoom="14" />

GPS

Create the Classes

Create a LocationManager

Create a LocationListener

Location Manager

What is a Location Manager?

This class provides access to the system location services.

These services allow applications to obtain periodic updates of the device's geographical location, or to fire an application-specified Intent when the device enters the proximity of a given geographical location.

Location Listener

What is a Location Listener?

Used for receiving notifications from the LocationManager when the location has changed.

These methods are called if the LocationListener has been registered with the location manager service using the requestLocationUpdates(String, long, float, LocationListener) method.

Location StrategiesKnowing where the user is allows your application to be smarter and deliver better information to the user.

When developing a location-aware application for Android, you can utilize GPS and Android's Network Location Provider to acquire the user location.

Although GPS is most accurate, it only works outdoors, it quickly consumes battery power, and doesn't return the location as quickly as users want.

Android's Network Location Provider determines user location using cell tower and Wi-Fi signals, providing location information in a way that works indoors and outdoors, responds faster, and uses less battery power.

To obtain the user location in your application, you can use both GPS and the Network Location Provider, or just one.

Example of Obtaining User Location

// Acquire a reference to the system Location ManagerLocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

// Define a listener that responds to location updatesLocationListener locationListener = new LocationListener() {    public void onLocationChanged(Location location) {      // Called when a new location is found by the network location provider.      makeUseOfNewLocation(location);    }

    public void onStatusChanged(String provider, int status, Bundle extras) {}

    public void onProviderEnabled(String provider) {}

    public void onProviderDisabled(String provider) {}  };

// Register the listener with the Location Manager to receive location updateslocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

Edit the AndroidManifest.xml

<uses-sdk android:minSdkVersion="13" /> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

<application android:icon="@drawable/ic_launcher" android:label="Where Am I" > <uses-library android:name="com.google.android.maps" /> <activity android:label="@string/app_name" android:name=".LBSActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>

Getting a fast fix with the last known location

String locationProvider = LocationManager.NETWORK_PROVIDER;

// Or use LocationManager.GPS_PROVIDER

Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);

Stop listening for updates// Remove the listener you previously added

locationManager.removeUpdates(locationListener);

iOS

Adding a Map

Add the MapKit framework to your project

Drag and Drop MKMapView onto your view.

Drink Coffee!

Example of Adding MapKit framework