Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
546 views
in Technique[技术] by (71.8m points)

android - geocoder.getFromLocationName returns only null

I am going out of my mind for the last two days with an IllegalArgumentException error I receive in Android code when trying to get a coordinates out of an address, or even reverse, get address out of longitude and latitude. This is the code, but I cannot see an error. It is a standard code snippet that is easily found on a Google search.

public GeoPoint determineLatLngFromAddress(Context appContext, String strAddress) {
    Geocoder geocoder = new Geocoder(appContext, Locale.getDefault());
    GeoPoint g = null; 
    try {
        System.out.println("str addres: " + strAddress);
        List<Address> addresses = geocoder.getFromLocationName(strAddress, 5);
        if (addresses.size() > 0) {
            g = new GeoPoint(
               (int) (addresses.get(0).getLatitude() * 1E6),
               (int) (addresses.get(0).getLongitude() * 1E6)
            );
        }
    } catch (Exception e) {
        throw new IllegalArgumentException("locationName == null");
    }
    return g;
 }

These are the permissions from manifest.xml file:

<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" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />

I do have the Google API key declared too: <uses-library android:name="com.google.android.maps" />

From the code snippet above, geocoder is not null, neither is the address or appContext, and I stumble here: geocoder.getFromLocationName(strAddress, 5);

I did a lot of Google searching and found nothing that worked, and the most important info I found is this:

The Geocoder class requires a backend service that is not included in the core android framework.

Sooo, I am confuzed now. What do I have to call, import, add, use in code.... to make this work? I am using Google API 2.2, API level 8. If somebody has found a solution for this, or a pointer for documentation, something that I didn't discover, please let us know.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I had a similar problem and found that polling the Geocoder until i got a result worked. Here is how i did it, so far works great.

try {
    List<Address> geoResults = geocoder.getFromLocationName("<address goes here>", 1);
    while (geoResults.size()==0) {
        geoResults = geocoder.getFromLocationName("<address goes here>", 1);
    }
    if (geoResults.size()>0) {
        Address addr = geoResults.get(0);
        myLocation.setLatitude(addr.getLatitude());
        myLocation.setLongitude(addr.getLongitude());
    }
} catch (Exception e) {
    System.out.print(e.getMessage());
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...