Forward Geocoding

Forward geocoding (or just geocoding) determines map coordinates for a given location.

What constitutes a valid location varies depending on the locale (geographic area) within which you're searching. Generally, it will include regular street addresses of varying granularity (from country to street name and number), postcodes, train stations, landmarks, and hospitals. As a general guide, valid search terms will be similar to the addresses and locations you can enter into the Google Maps search bar.

To do a forward-geocoding lookup, call getFromLocationName on a Geocoder instance. Pass in the location you want the coordinates for and the maximum number of results to return:

List<Address> result = geocoder.getFromLocationName(aStreetAddress, maxResults);

The returned list of Addresses can include multiple possible matches for the named location. Each address result will include latitude and longitude and any additional address information available for those coordinates. This is useful to confirm that the correct location was resolved, as well as for providing location specifics in searches for landmarks.

As with reverse geocoding, if no matches are found, null will be returned. The availability, accuracy, and granularity of geocoding results will depend entirely on the database available for the area you're searching.

When you're doing forward lookups, the Locale object specified during the creation of the Geocoder object is particularly important. The Locale provides the geographical context for interpreting your search requests, as the same location names can exist in multiple areas. Where possible, consider selecting a regional Locale to help avoid place-name ambiguity.

Additionally, try to use as many address details as possible, as shown in Listing 8-5.

LISTING 8-5: Geocoding an address

Geocoder fwdGeocoder = new Geocoder(this, Locale.US);

String streetAddress = "160 Riverside Drive, New York, New York"

List<Address> locations = null; try {

locations = fwdGeocoder.getFromLocationName(streetAddress, 10); } catch (IOException e) {}

For even more specific results, use the getFromLocationName overload, which lets you restrict your search to within a geographical bounding box.

List<Address> locations = null; try {

locations = fwdGeocoder.getFromLocationName(streetAddress, 10, n, e, s, w);

This overload is particularly useful in conjunction with a Map View, as you can restrict the search to within the visible map.

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