Where Am I Example

The following example —Where Am I? —features a new Activity that finds the device's current location using the GPS Location Provider. You will expand on this example throughout the chapter as you learn new geographic functionality.

This example assumes that you have enabled the gps_provider Location Provider using the techniques shown previously in this chapter, or that you're running it on a device that supports GPS and has that hardware enabled.

1. Create a new Where Am I? project with a WhereAmi Activity. This example uses the GPS provider (either mock or real), so modify the manifest file to include the <uses-permission> tags for access_fine_location and internet.

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.paad.whereami"> <application android:icon="@drawable/icon"> <activity android:name=".WhereAmI"

android:label="@string/app_name">

<intent-filter>

<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"

</manifest>

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

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<TextView android:id="@+id/myLocationText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello"

<uses permission android:name="android.permission.INTERNET

</LinearLayout>

3. Override the onCreate method of the WhereAmI Activity to get a reference to the Location Manager. Call getLastKnownLocation to get the last location fix value, and pass it in to the updateWithNewLocation method stub.

package com.paad.whereami;

import android.app.Activity; import android.content.Context; import android.location.Location; import android.location.LocationManager; import android.os.Bundle; import android.widget.TextView;

public class WhereAmI extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

LocationManager locationManager;

String context = Context.LOCATION_SERVICE;

locationManager = (LocationManager)getSystemService(context);

String provider = LocationManager.GPS_PROVIDER; Location location =

locationManager.getLastKnownLocation(provider);

updateWithNewLocation(location);

private void updateWithNewLocation(Location location) {}

4. Fill in the updateWithNewLocation method to display the passed-in Location in the Text View by extracting the latitude and longitude values.

private void updateWithNewLocation(Location location) { String latLongString; TextView myLocationText;

myLocationText = (TextView)findViewById(R.id.myLocationText); if (location != null) {

double lat = location.getLatitude(); double lng = location.getLongitude(); latLongString = "Lat:" + lat + "\nLong:" + lng; } else {

latLongString = "No location found";

myLocationText.setText("Your Current Position is:\n" +

latLongString);

All code snippets in this example are part of the Chapter 8 Where Am I? project, available for download at Wrox.com.

5. When running, your Activity should look like Figure 8-2.

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


Post a comment