简体   繁体   中英

Geocoder getFromLocation(lat,lon) doesn't work

I have an activity which first uses an AsyncTask to get and parse data from a server. The entries contain a latitude and a longitude.

Then, in onPostExecute() I load my adapter and display my listView. And in my adapter's getView() I try to display the location with the latitude and the longitude :

private String getPosition(String longitude, String latitude)
{
    float lon = Float.parseFloat(longitude),
          lat = Float.parseFloat(latitude);

    Geocoder geocoder = new Geocoder(MyActivity.this, Locale.getDefault());
    List<Address> addresses = null;
    try
    {
        addresses = geocoder.getFromLocation(lat, lon, 1); 
    } catch (IOException e)
    {
        e.printStackTrace();
    }

    if(addresses == null || addresses.size() == 0) return "No location found.";
    else
    {
        String city = addresses.get(0).getAddressLine(1);
        String country = addresses.get(0).getAddressLine(2);
        return (city + ", " + country.toUpperCase());
    }
}

I always get "No location found." but I'm sure some entries are correct. For example one has a latitude of 39.017 and a longitude of 125.73. Google's API show there is at least one address for it :

http://maps.googleapis.com/maps/api/geocode/json?latlng=39.017,125.73&sensor=true

I've also declared this in my Manifest :

<uses-permission android:name="android.permission.INTERNET" >
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

But all this won't return any address. Any idea why?

geoCoder.getFromLocation (Reverse Geocoding) method will work when you build avd (virtual device) target as Google API 19 and also the application is built with the target set as "Google API 19" (you can modify the build target using Project->Properties). This worked for me. Should you need more details please reply to this post.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM