简体   繁体   中英

Genymotion - Get Address from Longitude und Latitude

I use this code, to get my current address:

public String getAdressFromLongLat(double lati, double longi) {
    List<Address> addresses = new ArrayList<Address>();
    Geocoder geocoder = new Geocoder(mContext,Locale.getDefault());

    try {
        addresses = geocoder.getFromLocation(lati, longi, 1);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }
    if(addresses==null || addresses.size()<=0) {
        return null;
    }
    String address = addresses.get(0).getAddressLine(0);
    String city = addresses.get(0).getAddressLine(1);
    return address + "/;/" + city;
}

But it returns everytime null. I have no real device, so I test in a genymotion emulator. Is this the problem? Or is something wrong with the code?

-> I used the debugger to find the problem: addresses is not null, but the size is 0, why? :(

It's possible that Genymotion doesn't support address lookup. From the official documentation :

Note: Address lookup requires a backend service that is not included in the core Android framework. If this backend service is not available, Geocoder.getFromLocation() returns an empty list. The helper method isPresent(), available in API level 9 and later, checks to see if the backend service is available.

Try calling Geocoder.isPresent() to be sure.

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