How widespread is Geocoder implementation on Android mobile devices?
-
28-06-2021 - |
문제
I need to fetch longitude and latitude by street name. I saw that Geocoder can do that job. The documentation tells us that "the Geocoder class requires a backend service that is not included in the core android framework". That means that I can't be sure that Geocoder is implemented in Android devices.
My question is, how widespread is the Geocoder implementation. If there are only very few devices without Geocoder, I can live with that, but if there are a lot of devices, I have to rethink this functionality of my app.
해결책
A GeoCoder client is included along with Google Maps, so any device with Google Maps should have a working GeoCoder implementation. By and large, Android devices have Google Maps installed. Other than that, there could be no Google Maps, and some other GeoCoder backend available. In such a case, you can use the following code to determine if the device has a working GeoCoder implementation:
final Geocoder geocoder = new Geocoder(this);
final String locName = "1600 Amphitheatre Parkway, Mountain View, CA";
try {
final List<Address> list = geocoder.getFromLocationName(locName, 1);
if ( ! (list == null || list.isEmpty()) ) {
final Address address = list.get(0);
System.out.println("Geocoder backend present, (lat,lon) = (" + address.getLatitude() + ", " + address.getLongitude() + ")");
}
else {
System.out.println("Geocoder backend not present");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
다른 팁
We should use isPresent() method of GeoCoder class to decide so.
http://developer.android.com/reference/android/location/Geocoder.html#isPresent()