geocoder.getFromLocationName returns only null

房东的猫 提交于 2019-11-27 09:08:16

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());
}
public LatLng determineLatLngFromAddress(Context appContext, String strAddress) {
        LatLng latLng = null;
        Geocoder geocoder = new Geocoder(appContext, Locale.getDefault());
        List<Address> geoResults = null;

        try {
            geoResults = geocoder.getFromLocationName(strAddress, 1);
            while (geoResults.size()==0) {
                geoResults = geocoder.getFromLocationName(strAddress, 1);
            }
            if (geoResults.size()>0) {
                Address addr = geoResults.get(0);
                latLng = new LatLng(addr.getLatitude(),addr.getLongitude());
            }
        } catch (Exception e) {
            System.out.print(e.getMessage());
        }

    return latLng; //LatLng value of address
    }

Had the same problem. Pooling didn't work so I used this to get the location: Geocoding Responses

Use this class to get JSONObject of location:

public class GetLocationDownloadTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... strings) {


        String result = "";
        URL url;
        HttpURLConnection urlConnection;
        try {
            url = new URL(strings[0]);
            urlConnection = (HttpURLConnection) url.openConnection();
            InputStream is = urlConnection.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(is);

            int data = inputStreamReader.read();
            while(data != -1){
                char curr = (char) data;
                result += curr;
                data = inputStreamReader.read();
            }
            return result;

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        if(result != null) {
            try {
                JSONObject locationObject = new JSONObject(result);
                JSONObject locationGeo = locationObject.getJSONArray("results").getJSONObject(0).getJSONObject("geometry").getJSONObject("location");


            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

    }

You can create and run the class instance like this:

String link = "https://maps.googleapis.com/maps/api/geocode/json?address=" + addressString + "&key={YOUR_API_KEY}";

GetLocationDownloadTask getLocation = new GetLocationDownloadTask();

getLocation.execute(link);

Run your code on Android 2.0, It will work. I had the same problem with may code. Its not working in 2.2, don't know why

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!