4

(해결하기 위해 좌표)되어 사용할 수 없습니다이지만, "couldnt는이 주소를 얻을"작업과 출력 나던 :구글 지오 코더 서비스는 내가 좌표로 주소를 얻을 수있다

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

로그 캣 :

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) 
+0

http://stackoverflow.com/questions/7109240/service-not-available-geocoder-android – nhgrif

+0

실례합니다. 그것의 이상한 문제 .. – NirPes

+0

[this] (http://stackoverflow.com/questions/4761052/why-is-android-geocoder-throwing-a-service-not-available-exception) 링크를 따라 갔습니까? – nhgrif

답변

13

을 이 문제에 대한 일반적인 대답은 장치를 다시 시작해야한다는 것입니다.

앱을 작동시키기 위해 기기를 다시 시작하도록 사용자에게 알릴 수 없으므로 내 해결책은 HTTP 폴백을 사용하는 것이 었습니다. 여기서는 내 코드에서 사용하는 AsyncTask입니다.

위치에서 주소를 조회하고 다른 방법으로 주소를 조회하지 않으므로 상황에 맞게 수정해야합니다.

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

}; 
+0

lookupStringUriencoded의 내용은 무엇입니까? –

+1

이것은 uri가 인코딩 된 검색 문자열입니다 (공백을 % 20 등으로 대체). 나는 쿼리 매개 변수에 대해 HTTP 폴백 (fallback)에이 필터를 사용합니다. – cYrixmorten

+0

아마도 API 호출을 시도해 볼만한 가치가 없을 것입니다. HTTP 만 사용하면됩니다. 앱이 http 호출을하기 전에 사용자가 연결 시간 제한을두고 놀고 싶지는 않습니다. – ComeIn