Google Geocoder service is unavaliable (Coordinates to address)

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

The common answer to this problem is that you have to restart your device.

Surely you cannot tell your users to restart device in order for your app to function, so my solution was to use a HTTP fallback, here is the AsyncTask I use in my code.

You will have to modify it for your situation, as I lookup address from position and not the other way around.

    private class GetAddressPositionTask extends
        AsyncTask<String, Integer, LatLng> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected LatLng doInBackground(String... plookupString) {

        String lookupString = plookupString[0];
        final String lookupStringUriencoded = Uri.encode(lookupString);
        LatLng position = null;

        // best effort zoom
        try {
            if (geocoder != null) {
                List<Address> addresses = geocoder.getFromLocationName(
                        lookupString, 1);
                if (addresses != null && !addresses.isEmpty()) {
                    Address first_address = addresses.get(0);
                    position = new LatLng(first_address.getLatitude(),
                            first_address.getLongitude());
                }
            } else {
                Log.e(TAG, "geocoder was null, is the module loaded? "
                        + isLoaded);
            }

        } catch (IOException e) {
            Log.e(TAG, "geocoder failed, moving on to HTTP");
        }
        // try HTTP lookup to the maps API
        if (position == null) {
            HttpGet httpGet = new HttpGet(
                    "http://maps.google.com/maps/api/geocode/json?address="
                            + lookupStringUriencoded + "&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 {
                // Log.d("MAPSAPI", stringBuilder.toString());

                jsonObject = new JSONObject(stringBuilder.toString());
                if (jsonObject.getString("status").equals("OK")) {
                    jsonObject = jsonObject.getJSONArray("results")
                            .getJSONObject(0);
                    jsonObject = jsonObject.getJSONObject("geometry");
                    jsonObject = jsonObject.getJSONObject("location");
                    String lat = jsonObject.getString("lat");
                    String lng = jsonObject.getString("lng");

                    // Log.d("MAPSAPI", "latlng " + lat + ", "
                    // + lng);

                    position = new LatLng(Double.valueOf(lat),
                            Double.valueOf(lng));
                }

            } catch (JSONException e) {
                Log.e(TAG, e.getMessage(), e);
            }

        }
        return position;
    }

    @Override
    protected void onPostExecute(LatLng result) {
        Log.i("GEOCODE", result.toString());
        super.onPostExecute(result);
    }

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