I'm trying to get the clicked address on a Google Map with the onMapLongClickListener(LatLng point), using the lat and the lng of the point and converting them in a address with a Geocoder.It works fine if I am connected to Internet, but if I'm not the app crashes because the getFromLocation method gives a null result. So I suppose that the Geocoder class works only with the connection is enabled.Is it so?And is there a way to get the adress staying offline?
That's the code of the method:
public void onMapLongClick(LatLng point) {
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(point.latitude, point.longitude, 1);
} catch (IOException e) {
e.printStackTrace();
}
String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getAddressLine(1);
String country = addresses.get(0).getAddressLine(2);
String str = address + ", " + city + ", " + country;
gMap.addMarker(new MarkerOptions()
.position(point)
.title("Clicked Point")
.draggable(false)
.snippet(address + ", " + city + ", " + country));
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(50);
}
And these are the permissions:
<uses-permission android:name="com.example.mappine.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.VIBRATE" />
The Android API contains a Geocoder class that can use either a location name or a location's latitude and longitude values to get further details about an address (it can perform both forward and reverse geocoding). The returned address details include address name, country name, country code, postal code and more.
The Geocoding API uses a pay-as-you-go pricing model.
The short story is you need to do: Geocoder geocoder = new Geocoder(this, Locale. getDefault()); List<Address> addresses = geocoder. getFromLocation(lat, lng, 1);
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.
Yes. Do you know how much data is needed for the full geocoder? You aren't storing that on a cellphone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With