Skip to content Skip to sidebar Skip to footer

Cannot Get Place From Google Map Using Location

I try to get place(ex: locality, country, postal code) form google map using latitude and longitude. The following is my code that got null data: Edit: public void onLocationChange

Solution 1:

I think the problem is on the parameters of the method. I guess that location is a GeoPoint In that case, instead of

addresses = geocoder.getFromLocation(location.getLatitude(),
                    location.getLongitude(), 1);

try this:

addresses = geocoder.getFromLocation(location.getLatitude()/1E6,
                    location.getLongitude()/1E6, 1);

because the coordinates in a geopoint are represented in microdegrees

Edit I copied your code and tried it. Assuming your "location" object is a GeoPoint, the code that works is as follows:

GeoPoint location = new GeoPoint(lat, lon);
    if (location != null) {
        Geocoder geocoder = new Geocoder(this, Locale.getDefault());
        try {
            List<Address> addresses = geocoder.getFromLocation(location.getLatitudeE6()/1E6,
                    location.getLongitudeE6()/1E6, 1);
            if (addresses.size() > 0) {
                Address resultAddress = addresses.get(0);

                String locality = resultAddress.getLocality();
                String sublocality = resultAddress.getSubLocality();
                String postalcode = resultAddress.getPostalCode();
                String country = resultAddress.getCountryName();
                String adminarea = resultAddress.getSubAdminArea();

            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

however, if you are testing it on the emulator, be sure you have internet connection. Otherwise, the method "getFromLocation" cannot find out the address and won't display anything. If you don't have any error in the logcat and the problem is only that nothing is displayed, that is the problem: no network.


Post a Comment for "Cannot Get Place From Google Map Using Location"