Geocoding Where Am I

Using the Geocoder you can determine the street address at your current location. In this example you'll further extend the Where Am I? project to include and update the current street address whenever the device moves.

Start by modifying the manifest to include the Internet uses-permission:

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

Then open the WhereAmI Activity. Modify the updateWithNewLocation method to instantiate a new Geocoder object, and call the getFromLocation method, passing in the newly received location and limiting the results to a single address.

Extract each line in the street address, as well as the locality, postcode, and country, and append this information to an existing Text View string.

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

myLocationText = (TextView)findViewById(R.id.myLocationText);

String addressString = "No address found";

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

double latitude = location.getLatitude(); double longitude = location.getLongitude(); Geocoder gc = new Geocoder(this, Locale.getDefault()); try {

List<Address> addresses = gc.getFromLocation(latitude, longitude, 1); StringBuilder sb = new StringBuilder(); if (addresses.size() >0) {

Address address = addresses.get(O);

for (int i = 0; i < address.getMaxAddressLineIndex(); i++) sb.append(address.getAddressLine(i)).append("\n");

sb.append(address.getLocality()).append("\n"); sb.append(address.getpostalcode()).append("\n");

sb.append(address.getCountryName());

addressString = sb.toString(); } catch (IOException e) {}

} else i latLongString = "No location found",

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

latLongString + "\n" + addressString),

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

If you run the example now, it should appear as shown in Figure 8-3.

Where Am I?

Your Current Position is:

Lat:51.503171

Long:-Q.126477

Westminster, London SW1A 2

London

SW1A 2

United Kingdom

FIGURE 8-3

CREATING MAP-BASED ACTIVITIES

The MapView provides an ideal user interface option for presenting geographical data.

One of the most intuitive ways of providing context for a physical location or address is to display it on a map. Using a MapView, you can create Activities that feature an interactive map.

Map Views support annotation using Overlays and by pinning Views to geographical locations. Map Views offer full programmatic control of the map display, letting you control the zoom, location, and display modes —including the option to display satellite, street, and traffic views.

In the following sections you'll see how to use Overlays and theiapController to create dynamic map-based Activities. Unlike online mashups, your map Activities will run natively on the device, enabling you to leverage its hardware and mobility to provide a more customized and personal user experience.

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