谷歌地理codeR服务unavaliable(坐标来解决)坐标、地理、codeR、unavaliable

由网友(   - 疯子 。)分享简介:我要得到一个地址坐标的,但它不工作和输出不可能得到地址:公共字符串的getAddress(北纬字符串,字符串LON){地理codeR地理codeR =新的地缘codeR(这一点,Locale.ENGLISH);字符串RET =;尝试 {名单<地址>地址=地理coder.getFromLocation(Dou...

我要得到一个地址坐标的,但它不工作和输出不可能得到地址:

 公共字符串的getAddress(北纬字符串,字符串LON)
        {
            地理codeR地理codeR =新的地缘codeR(这一点,Locale.ENGLISH);
            字符串RET =;
            尝试 {
                名单<地址>地址=地理coder.getFromLocation(Double.parseDouble(LAT),Double.parseDouble(LON),1);
                如果(地址!= NULL){
                    地址returnedAddress = addresses.get(0);
                    StringBuilder的strReturnedAddress =新的StringBuilder();
                    的for(int i = 0; I< returnedAddress.getMaxAddressLineIndex();我++){
                        strReturnedAddress.append(returnedAddress.getAddressLine(i))的追加( N)。
                    }
                    RET = strReturnedAddress.toString();
                    保留= ret.substring(0,ret.length() -  1);
                }
                其他{
                    RET =未知地址;
                }
            }
            赶上(IOException异常E){
                // TODO自动生成的catch块
                e.printStackTrace();
                RET =无法获取地址;
            }
            返回RET;
        }
 

LogCat中:

  09-27 23:47:49.837:W / System.err的(1862年):java.io.IOException异常:服务不可用
09-27 23:47:49.847:W / System.err的(1862年):在android.location.Geo coder.getFromLocation(GEO coder.java:136)
09-27 23:47:49.847:W / System.err的(1862年):在com.example.free.Add.GetAddress(Add.java:634)
09-27 23:47:49.857:W / System.err的(1862年):在com.example.free.Add $ LoadAddress.doInBackground(Add.java:608)
09-27 23:47:49.857:W / System.err的(1862年):在com.example.free.Add $ LoadAddress.doInBackground(Add.java:1)
09-27 23:47:49.857:W / System.err的(1862年):在android.os.AsyncTask $ 2.call(AsyncTask.java:287)
09-27 23:47:49.857:W / System.err的(1862):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:305)
09-27 23:47:49.857:W / System.err的(1862年):在java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-27 23:47:49.857:W / System.err的(1862年):在android.os.AsyncTask $ SerialExecutor $ 1.运行(AsyncTask.java:230)
09-27 23:47:49.857:W / System.err的(1862年):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-27 23:47:49.857:W / System.err的(1862):在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:569)
09-27 23:47:49.857:W / System.err的(1862年):在java.lang.Thread.run(Thread.java:856)
 

解决方案

常见的回答这个问题是,你必须重新启动您的设备。

当然,你不能告诉你的用户重新启动设备,以便为您的应用程序的功能,所以我的解决方案是使用一个HTTP回退,这里是AsyncTask的我在code使用。

谷歌地球企业版开源

您将不得不修改您的具体情况,因为我从位置查找地址,而不是周围的其他方式。

 私有类GetAddressPositionTask扩展
        AsyncTask的<字符串,整数,经纬度> {

    @覆盖
    在preExecute保护无效(){
        super.on preExecute();
    }

    @覆盖
    受保护的经纬度doInBackground(字符串... plookupString){

        串lookupString = plookupString [0];
        最后弦乐lookupStringUrien codeD = Uri.en code(lookupString);
        经纬度位置= NULL;

        //尽力放大
        尝试 {
            如果(GEO codeR!= NULL){
                名单<地址>地址=地理coder.getFromLocationName(
                        lookupString,1);
                如果(地址= NULL和放大器;!&安培;!addresses.isEmpty()){
                    地址first_address = addresses.get(0);
                    位置=新的经纬度(first_address.getLatitude()
                            first_address.getLongitude());
                }
            } 其他 {
                Log.e(TAG,地理codeR为null,则是加载模块?
                        + isLoaded);
            }

        }赶上(IOException异常E){
            Log.e(TAG,地理codeR失败,移动到HTTP);
        }
        //尝试HTTP查询到的地图API
        如果(位置== NULL){
            HTTPGET HTTPGET =新HTTPGET(
                    http://maps.google.com/maps/api/geo$c$c/json?address=
                            + lookupStringUrien codeD +&放大器,传感器=真正的);
            HttpClient的客户端=新DefaultHttpClient();
            HTT presponse响应;
            StringBuilder的StringBuilder的=新的StringBuilder();

            尝试 {
                响应= client.execute(HTTPGET);
                HttpEntity实体= response.getEntity();
                InputStream的流= entity.getContent();
                INT B:
                而((二= stream.read())!=  -  1){
                    stringBuilder.append((char)的B);
                }
            }赶上(ClientProtocolException E){
            }赶上(IOException异常E){
            }

            的JSONObject的JSONObject =新的JSONObject();
            尝试 {
                // Log.d(MAPSAPI,stringBuilder.toString());

                的JSONObject =新的JSONObject(stringBuilder.toString());
                如果(jsonObject.getString(状态)。等于(OK)){
                    的JSONObject = jsonObject.getJSONArray(结果)
                            .getJSONObject(0);
                    的JSONObject = jsonObject.getJSONObject(几何);
                    的JSONObject = jsonObject.getJSONObject(位置);
                    字符串纬度= jsonObject.getString(土地增值税);
                    字符串LNG = jsonObject.getString(LNG);

                    // Log.d(MAPSAPI,经纬度+纬度+,
                    // + LNG);

                    位置=新的经纬度(Double.valueOf(LAT)
                            Double.valueOf(LNG));
                }

            }赶上(JSONException E){
                Log.e(TAG,e.getMessage(),E);
            }

        }
        返回的位置;
    }

    @覆盖
    保护无效onPostExecute(经纬度结果){
        Log.i(GEO code,result.toString());
        super.onPostExecute(结果);
    }

};
 

I have to get an address by coordinates, but it doesnt work and outputs "Couldnt get address":

public String GetAddress(String lat, String lon)
        {
            Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);
            String ret = "";
            try {
                List<Address> addresses = geocoder.getFromLocation(Double.parseDouble(lat), Double.parseDouble(lon), 1);
                if(addresses != null) {
                    Address returnedAddress = addresses.get(0);
                    StringBuilder strReturnedAddress = new StringBuilder("");
                    for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
                        strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("n");
                    }
                    ret = strReturnedAddress.toString();
                    ret=ret.substring(0, ret.length() - 1);
                }
                else{
                    ret = "Unknown Address";
                }
            }
            catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                ret = "Couldn't get Address";
            }
            return ret;
        }

LogCat:

09-27 23:47:49.837: W/System.err(1862): java.io.IOException: Service not Available
09-27 23:47:49.847: W/System.err(1862):     at android.location.Geocoder.getFromLocation(Geocoder.java:136)
09-27 23:47:49.847: W/System.err(1862):     at com.example.free.Add.GetAddress(Add.java:634)
09-27 23:47:49.857: W/System.err(1862):     at com.example.free.Add$LoadAddress.doInBackground(Add.java:608)
09-27 23:47:49.857: W/System.err(1862):     at com.example.free.Add$LoadAddress.doInBackground(Add.java:1)
09-27 23:47:49.857: W/System.err(1862):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
09-27 23:47:49.857: W/System.err(1862):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-27 23:47:49.857: W/System.err(1862):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-27 23:47:49.857: W/System.err(1862):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
09-27 23:47:49.857: W/System.err(1862):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-27 23:47:49.857: W/System.err(1862):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-27 23:47:49.857: W/System.err(1862):     at java.lang.Thread.run(Thread.java:856)

解决方案

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);
    }

};

阅读全文

相关推荐

最新文章