Explain Geocoding and reverse geocoding.
(4M)
- Geocoding converts addresses into geographic coordinates (latitude and longitude).
- Reverse geocoding does the opposite, it converts coordinates into human-readable
addresses.
-
Geocoding:
Input:
A human-readable address (e.g., "1600 Amphitheatre Parkway, Mountain View, CA").
Process:
Uses a geocoding service or API to find the corresponding geographic coordinates (e.g.,
latitude and longitude) for the given address.
Output:
Geographic coordinates (e.g., latitude: 37.423021, longitude: -122.083739).
Reverse Geocoding:
Input:
Geographic coordinates (e.g., latitude: 37.423021, longitude: -122.083739).
Process:
Uses a geocoding service or API to find the nearest human-readable address or place name
for the given coordinates.
Output:
A human-readable address (e.g., "1600 Amphitheatre Parkway, Mountain View, CA").
- Reverse-Geocoding is a process used to convert coordinates (latitude and longitude) to human
readable addresses.
- This is not exactly the opposite of Geocoding.
- In Geocoding, the place is associated with a name and fixed coordinates.
- These coordinates are double in nature.
- Negligible change in these coordinates may still refer to the same place, but we shall never get
the place name as it is associated with only those fixed coordinates.
- Therefore, we shall definitely get the complete address in reverse geocoding, but the place name
is not guaranteed.
- Searching location in Google Map API is done through Geocoder class. Geocoder class is used to
handle geocoding and reverse geocoding.
- Geocoding is a process in which street address is converted into a coordinate (latitude,longitude).
Reverse geocoding is a process in which a coordinate (latitude,longitude) is converted into an
address.
Methods of Geocoder class
- List<Address> getFromLocation(double latitude, double longitude, int
maxResults): This method returns an array of Address which specifies the
surrounding latitude and longitude.
- List<Address> getFromLocationName(String location, int results, double
leftLatitude, double leftLongitude, double rightLatitude, double
rightLongitude): This method returns an array of Address which describes the given
location such as place, an address, etc.
- List<Address> getFromLocationName(String location, int results): This method
returns an array of Address which describes te given location such as place, an
address, etc.
- static boolean isPresent(): This method returns true if the methods
getFromLocation() and getFromLocationName() are implemented.
Code which convert location name into coordinate.
List<Address> addressList = geocoder.getFromLocationName(location, 1);
Address address = addressList.get(0);
LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
Geocoding
Geocoder geocoder = new Geocoder(context);
try {
List<Address> addresses = geocoder.getFromLocationName(address, 1); // Get up
to 1 result
if (addresses != null && !addresses.isEmpty()) {
Address foundAddress = addresses.get(0);
double latitude = foundAddress.getLatitude();
double longitude = foundAddress.getLongitude();
Toast.makeText(getApplicationContext(),"Latitude: " + latitude + ", Longitude: "
+ longitude,Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(),Toast.LENGTH_LONG).show();
}
} catch (IOException e) { }
Reverse Geocoding
Geocoder geocoder = new Geocoder(context);
long latitude=1430000;
long longitude=145000;
try {
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1); //
Get up to 1 result
if (addresses != null && !addresses.isEmpty()) {
Address foundAddress = addresses.get(0);
Toast.makeText(getApplicationContect(),"Address: " +
foundAddress.getAddressLine(0),Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContect(),"No address found.",
Toast.LENGTH_LONG).show();
}
} catch (IOException e) { }