Geocoding Locations Android

Determining the latitude and longitude is useful for precise location, tracking, and measurements; however, it’s not usually descriptive to users. The Android SDK provides some helper methods to turn raw location data into addresses and descriptive place names. These methods can also work in reverse, turning place names or addresses into raw location coordinates.

The Geocoder object can be used without any special permissions. The following block of code demonstrates using the Geocoder object to get the location names of a Location object passed in to the onLocationChanged() method of a LocationListener:

Geocoder coder = new Geocoder(this);
try {
Iterator<Address> addresses = coder
.getFromLocation(location.getLatitude(),
location.getLongitude(), 3).iterator();
if (addresses != null) {
while (addresses.hasNext()) {
Address namedLoc = addresses.next();
String placeName = namedLoc.getLocality();
String featureName = namedLoc.getFeatureName();
String country = namedLoc.getCountryName();
String road = namedLoc.getThoroughfare();
locInfo += String.format(" [%s][%s][%s][%s]",
placeName, featureName, road, country);
int addIdx = namedLoc.getMaxAddressLineIndex();
while (addIdx >= 0 ) {
String addLine = namedLoc.getAddressLine(addIdx);
locInfo += String.
format(" Line %d: %s", addIdx, addLine);
addIdx—;
}
}
}
} catch (IOException e) {
Log.e("GPS", "Failed to get address", e);
}

You can extract information from the results of the call to the getFromLocation() method in two ways, both of which are demonstrated. Note that a particular location might have multiple Address results in the form of a List<Address> object. Typically, the first Address is the most detailed, and the subsequent Address objects have less detail and describe a broader region.

The first method is to query for specific information, such as by using the getFeatureName() method or the getLocality() method. These methods are not guaranteed to return useful information for all locations. They are useful, though, when you know you need only a specific piece of general information, such as the country.

The second method for querying information is by “address lines.”This is generally used for displaying the “address” of a location to the user. It might also be useful to use the location in directions and in other cases where a street address is desired. That said, the addresses returned might not be complete. Simply use the getMaxAddressLineIndex() and getAddressLine() methods to iterate through the addresses. Figure shows a sample location with three resulting addresses.

Image showing location geocoded to three “addresses.”

Image showing location geocoded to three “addresses.”

The Geocoder object also supports using named locations or address lines to generate latitude and longitude information. The input is forgiving and returns reasonable results in most cases. For instance, all the following returns valid and correct results: “Eiffel Tower,” “London, UK,”“Iceland,”“BOS,”“Yellowstone,” and “1600 Pennsylvania Ave,DC.”

The following code demonstrates a button handler for computing location data based on user input of this kind:

public void onClick(View v) {
String placeName = name.getText().toString();
try {
List<Address> geocodeResults =
coder.getFromLocationName(placeName, 3);
Iterator<Address> locations = geocodeResults.iterator();
String locInfo = "Results: ";
while (locations.hasNext()) {
Address loc = locations.next();
locInfo += String.format("Location: %f, %f ",
loc.getLatitude(), loc.getLongitude());
}
results.setText(locInfo);
} catch (IOException e) {
Log.e("GeoAddress", "Failed to get location info", e);
}
}

The result of the call to the getFromLocationName() method is a List of Address objects, much like the previous example. Figure shows the results for entering Eiffel Tower.

The results for geocoding the term Eiffel Tower.

The results for geocoding the term Eiffel Tower.

Always assume that you will get more than one result. It is good form to provide a picker for the user to select from the results and choose the most appropriate location. Another good way to confirm with the user that they entered the correct location is to map it. We now discuss a couple of different methods for mapping locations using Google Maps.



Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Android Topics