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

Location based services 10

Aug 17, 2015

Download

Documents

Michael Shrove
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Location based services   10

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

Page 2: Location based services   10

Things you going to learn in Android

Displaying Google Maps

Track location via GPS or other services.

Page 3: Location based services   10

Displaying Maps

Page 4: Location based services   10

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.

Page 5: Location based services   10

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.

Page 6: Location based services   10

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.

Page 7: Location based services   10

Obtain Google Maps Android API v2 Key

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

Page 8: Location based services   10

Create Project

Page 9: Location based services   10

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.

Page 10: Location based services   10

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.

Page 11: Location based services   10

Enter API Information

Enter you SHA1 certificate fingerprint

semicolon

Package name

Page 12: Location based services   10

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.

Page 13: Location based services   10

Manifest Changes

Add the below changes to the manifest before application tag.

Page 14: Location based services   10

Add key to manifest

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

Page 15: Location based services   10

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

Page 16: Location based services   10

GPS

Page 17: Location based services   10

Create the Classes

Create a LocationManager

Create a LocationListener

Page 18: Location based services   10

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.

Page 19: Location based services   10

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.

Page 20: Location based services   10

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.

Page 21: Location based services   10

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

Page 22: Location based services   10

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>

Page 23: Location based services   10

Getting a fast fix with the last known location

String locationProvider = LocationManager.NETWORK_PROVIDER;

// Or use LocationManager.GPS_PROVIDER

Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);

Page 24: Location based services   10

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

locationManager.removeUpdates(locationListener);

Page 25: Location based services   10

iOS

Page 26: Location based services   10

Adding a Map

Add the MapKit framework to your project

Drag and Drop MKMapView onto your view.

Drink Coffee!

Page 27: Location based services   10

Example of Adding MapKit framework