2013-07-28 3 views
1

GeoCoder를 사용하여 도시 이름을 가져올 수 있지만 GPS가 GetLocality에서 인식 할 수없는 값 ("null"을 반환 함)을 반환하는 경우가 있습니다. 가장 좋은 해결책은 가장 가까운 도시 이름을 얻는 것입니다. 아무도 내가 그걸하는 방법을 말해 줄 수 있습니다. 아니면 해결할 다른 대안이 있다면 괜찮습니다.위도와 경도를 통해 가장 가까운 도시 이름을 얻는 방법은 무엇입니까?

public void getCityName(double dLatitude, double dLongitude) throws IOException{ 
    Geocoder gcd = new Geocoder(this, Locale.getDefault()); 
    List<Address> addresses = gcd.getFromLocation(dLatitude, dLongitude, 1); 

    TextView cityname = (TextView)findViewById(R.id.city_name); 
    if (addresses.size() > 0) {    
     cityname.setText("" +addresses.get(0).getLocality() + "," + addresses.get(0).getCountryName());   
    } 
} 
+0

예 google을 사용하여 Google에 요청하고 json을 구문 분석하고 위도와 경도에 따라 주소를 가져올 수 있습니다. –

답변

0

이 방법을 사용할 수 있습니다.

ProgressDialog dialog=null; 
    @Override 
    public void onClick(View v) { 
      dialog = ProgressDialog.show(context, "", "Please! Wait...", true); 
      GetCurrentAddress result = new GetCurrentAddress(); 
      result.execute(""); 
     } 



    // getting address task 
    private class GetCurrentAddress extends AsyncTask<String, Void, String> { 

     @Override 
     protected String doInBackground(String... urls) { 
      String responseText = null; 
      String googleurl = "http://maps.google.com/maps/geo?"; 
      String latitude="38.89"; 
      String longitude ="-77.03"; 
     StringBuilder sbuilder=new StringBuilder(); 
     sbuilder.append(googleurl); 

     sbuilder.append("q="+latitude+","+longitude); 
     sbuilder.append("&amp;output=responseText&amp;sensor=true"); 
     String url=sbuilder.toString(); 

     try { 
      DefaultHttpClient httpclient=new DefaultHttpClient(); 
      HttpPost httppost=new HttpPost(url); 
      HttpResponse httpresponse=httpclient.execute(httppost); 
      HttpEntity httpentity=httpresponse.getEntity(); 
      InputStream is=httpentity.getContent(); 
      BufferedReader reader=new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
      StringBuilder sb=new StringBuilder(); 
      String line=null; 
     while((line=reader.readLine())!=null) 
     { 
      sb.append(line+"\n"); 

     } 
     is.close(); 
     responseText=sb.toString(); 
     } 
     catch(ClientProtocolException e) 
     { 
      e.printStackTrace(); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 
     return responseText; 
     } 

     @Override 
     protected void onPostExecute(String resultString) { 
      dialog.dismiss(); 

      String response= resultString; 

      if (responseText != null) { 
       Log.v(TAG, "json response is:" + response); 
       try { 
        jObj = new JSONObject(response); 
        jsonarray = jObj.getJSONArray("Placemark"); 
        for (int i = 0; i < jsonarray.length(); i++) { 
         JSONObject innerJsonobject = jsonarray.getJSONObject(i); 
         String jsonaddress = innerJsonobject 
           .getString("address"); 


        } 

       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
+0

이 google URL은 이제 Google Maps API V3 URL로 대체되었습니다. http://maps.googleapis.com/maps/api/geocode/json?latlng=5.975632,80.427973&sensor=true – bynu022