Does Android Geocoder work only with internet connection?
Asked Answered
O

1

7

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" />
Occipital answered 28/4, 2013 at 2:23 Comment(2)
I'm sure that someone somewhere offers an offline geocoding database, but that someone isn't Google, and their product probably isn't free.Kalil
Google's geocode API is free with conditions (See here).Flamenco
M
17

Yes. Do you know how much data is needed for the full geocoder? You aren't storing that on a cellphone.

Malorie answered 28/4, 2013 at 2:33 Comment(5)
A geocoder needs to map all addresses in the world (or at least the region being used by the app) to longitude-latitude coordinates. This is a lot of data.Caundra
And yet geocode providers like TomTom, Navigon, etc have done just that by bundling offline geocode data with their Android apps and installed it on your Android devices. I don't see what the fuss is with offline geocode data. Using reverse geocoding with an Internet connection is only viable for those who are on big data plans. Those who are on limited data plans would be better off with the offline data option.Flamenco
The deal is three things. First, it takes up a lot of room for any real degree of accuracy- we're talking gigs. You have a limited subset in those apps. Secondly, accuracy- downloaded versions aren't up to date. Third- he asked about a specific geocoder. That one doesn't have an offline mode. If you want to provide one, you'd have to use another service and not Google's.Malorie
What about Google Maps offline saved regions? But I guess they don't allow any API to retrieve such info in other appsIbanez
THe offline saved regions may not have existed 9 years ago when I answered this. But even then- that's saved data for maps, not a service provided locally. They probably could cache a region these days with the increase in storage size, whether they do I don't know.Malorie

© 2022 - 2024 — McMap. All rights reserved.