简体   繁体   中英

Geocoder returns false on isPresent()

So I wrote some code for maps in my app, and it works on my phone just fine, sadly it does not work on my emulator which is not comfortable enough for me here is the code:

String  searchString = mSearchText.getText().toString();

Geocoder geocoder = new Geocoder(this);
List<Address> list = new ArrayList<>();
try{
    int i=0;
    while(list.size()==0 && i<10) {
        boolean a = geocoder.isPresent();
        list = geocoder.getFromLocationName(searchString,1);
        i++;
    }
}catch(IOException e){
    Log.d(TAG, "geoLocate: IOException " + e.getMessage());
}

So when I debug it I see that variable "a" is always false on my emulator.

I use android emulator for visual studio since I have an AMD processor (Ryzen 7 1800x) on which I installed google play store and google play services(map works just fine, just geocoder doesn't), now is there some way to fix it?

As I read on https://developer.android.com/reference/android/location/Geocoder.html website "The Geocoder query methods will return an empty list if there no backend service in the platform." Can I get the service somehow? Download it on my emulator or something like this?

First, isPresent() is a static method so the call should be

Geocoder.isPresent();

IsPresent method "Returns true if the Geocoder methods getFromLocation and getFromLocationName are implemented" and false otherwise. Some emulators don't have the geocoder service installed. Is the method

geocoder.getFromLocationName 

returning what you need or an empty array?

Your snippet works just fine on my Nexus 6P Android 7.0 API 24 emulator.

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