1

我知道这个问题已经被问过好几次了,但我仍然收到这个错误。

在模拟器和实际设备上都尝试过,将模拟器的目标更改为 Google API,并将项目的目标构建更改为 Google API

在这些方面需要帮助:(谢谢!

4

1 回答 1

0

我希望你现在解决了。正如你所说,它有很多线程。在研究了所有线程之后,我得到的答案是 Geocoder 并不总是返回一个值。您可以尝试在 for 循环中发送 3 次请求。我至少可以回来一次。如果不是,那么它们可能是连接问题,也可能是服务器未回复您的请求等其他问题。

我也有一个while循环,但我曾经最多尝试10次。有时,即使它连接到互联网,它也不会返回任何东西。然后,我每次都使用这种更可靠的方式来获取地址:

我曾经获取纬度和经度,然后请求谷歌服务器,回复一个 JSON 对象,其中包含有关位置坐标的各种信息。这种获取地址字符串的方式不需要地理编码器。这是功能:

public JSONObject getLocationInfo() {

        HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?latlng="+lat+","+lng+"&sensor=true");
        HttpClient client = new DefaultHttpClient();
        HttpResponse response;
        StringBuilder stringBuilder = new StringBuilder();

        try {
            response = client.execute(httpGet);
            HttpEntity entity = response.getEntity();
            InputStream stream = entity.getContent();
            int b;
            while ((b = stream.read()) != -1) {
                stringBuilder.append((char) b);
            }
        } catch (ClientProtocolException e) {
            } catch (IOException e) {
        }

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject = new JSONObject(stringBuilder.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonObject;
    }

我这样称呼它:

JSONObject ret = getLocationInfo(); 
JSONObject location;
String location_string;
try {
    location = ret.getJSONArray("results").getJSONObject(0);
    location_string = location.getString("formatted_address");
    Log.d("test", "formattted address:" + location_string);
} catch (JSONException e1) {
    e1.printStackTrace();

}

希望这可以帮助。我也厌倦了依赖 Geocoder。这对我有用。虽然它可能比地理编码器慢一点。为了测试其功能,您只需将您拥有的纬度和经度坐标放入 URL 中。尝试在 Web 浏览器中查看返回的 JSON 对象。您将看到如何提取地址字符串。尝试并阅读这些线程:

Geocoder 并不总是返回值,并且geocoder.getFromLocationName 仅返回 null

于 2013-03-17T19:45:50.093 回答