【问题标题】:Can only obtain address using GeoCoder with WiFi, not mobile internet (LTE)只能使用带 WiFi 的 GeoCoder 获取地址,而不是移动互联网 (LTE)
【发布时间】:2015-11-01 10:02:25
【问题描述】:

当我运行此处找到的“位置地址”示例时: https://github.com/googlesamples/android-play-location

应用仅在连接到 WiFi 时获取地址。当我关闭 WiFi 并使用 LTE 时,我看到“抱歉,该服务不可用”。

我正在尝试将此代码传输到我自己的项目中,但我希望它能够使用移动互联网和 WiFi 工作。

有人知道为什么会这样吗?

logcat 跟随超时:

09-28 21:05:40.219  16344-19027/com.google.android.gms.location.sample.locationaddress E/fetch-address-intent-service﹕ Sorry, the service is not available
java.io.IOException: Timed out waiting for response from server
        at android.location.Geocoder.getFromLocation(Geocoder.java:136)
        at com.google.android.gms.location.sample.locationaddress.FetchAddressIntentService.onHandleIntent(FetchAddressIntentService.java:92)
        at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:145)
        at android.os.HandlerThread.run(HandlerThread.java:61)

【问题讨论】:

  • 您可以在尝试与 LTE 连接时发布日志吗?

标签: android location google-play-services reverse-geocoding


【解决方案1】:

2014 年的一些帖子表明 Geocoder 可能无法与 LTE 一起使用。参见例如 Geocoder "Timed out waiting for response from server"

建议改为使用 Web API。见Google Geocoder service is unavaliable (Coordinates to address)

请报告您取得的任何成功。我也在使用地理编码器,但到目前为止,只是在 Wifi 上(它可以工作)

【讨论】:

  • 在尝试使用您提供的示例解决此问题之前,我将继续讨论其他事情。当我弄清楚时,我会回来并发布。谢谢。
  • 我最终通过http url连接使用了google maps api。请参阅下面的答案。
【解决方案2】:

我不知道如何让地理编码器使用移动互联网工作,所以我求助于使用谷歌地图 api,它可以通过 wifi 和移动互联网工作。这是我的实现:

try {
            URL url = new URL("http://maps.googleapis.com/maps/api/geocode/json?latlng=" +
                    latitude + "," + longitude + "&sensor=true");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(10000);
            conn.setConnectTimeout(15000);
            conn.connect();

            jsonResult = inputStreamToString(conn.getInputStream()).toString();
            JSONObject jsonResponse = new JSONObject(jsonResult);

            Log.d("json result", "Geocoder Status: " + jsonResponse.getString("status"));
            if("OK".equalsIgnoreCase(jsonResponse.getString("status"))) {
                JSONArray addressComponents = ((JSONArray)jsonResponse.get("results")).getJSONObject(0).getJSONArray("address_components");
                for (int i = 0; i < addressComponents.length(); i++) {
                    String addressComponent = ((JSONArray) ((JSONObject) addressComponents.get(i)).get("types")).getString(0);
                    if (addressComponent.equals("locality")) {
                        locality = ((JSONObject) addressComponents.get(i)).getString("long_name");
                    }
                    if (addressComponent.equals("administrative_area_level_1")) {
                        admin1 = ((JSONObject) addressComponents.get(i)).getString("long_name");
                    }
                    if (addressComponent.equals("country")) {
                        countryCode = ((JSONObject) addressComponents.get(i)).getString("short_name");
                    }
                }
                Log.d("json result", locality + "." + admin1 + "." + countryCode);
            }

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

private StringBuilder inputStreamToString(InputStream is) {
    String rLine = "";
    StringBuilder answer = new StringBuilder();
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));
    try {
        while ((rLine = rd.readLine()) != null) {
            answer.append(rLine);
        }
    } catch (IOException e) {
        // e.printStackTrace();
        Toast.makeText(mContext.getApplicationContext(),
                "Error..." + e.toString(), Toast.LENGTH_LONG).show();
    }
    return answer;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-03
    • 1970-01-01
    • 1970-01-01
    • 2014-06-01
    • 2011-06-08
    • 2011-11-12
    • 2014-04-07
    • 1970-01-01
    相关资源
    最近更新 更多