2014-08-29 71 views
1

我能使用langitude和緯度,但以檢索從谷歌公交API數據不directions屬性。這是我到目前爲止谷歌公交JSON解析方向

public class MainActivity extends Activity { 
     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 
     if (android.os.Build.VERSION.SDK_INT > 9) { 
      StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
      StrictMode.setThreadPolicy(policy); 
    } 
      try 
      { 
       String Lat=Double.toString(22.508507985602836); 
       String Long=Double.toString(73.474991977022533); 

       String Address = ReadAddressFromWebService(Lat,Long); 
       Toast.makeText(this,Address, Toast.LENGTH_LONG).show(); 
      } 

      catch(Exception e) 
      { 
       Log.e("error","Json parsing error :"+ e.toString()); 
       Toast.makeText(this,e.toString(), Toast.LENGTH_LONG).show(); 
      } 
    } 

    public String ReadAddressFromWebService(String latitude, String longitude) { 
      String Address=""; 
      StringBuffer sb=new StringBuffer(); 
      sb.append("http://maps.googleapis.com/maps/api/geocode/json?latlng="+ 
       latitude +","+longitude +"&sensor=false"); 
       String url=sb.toString(); 
      HttpClient httpClient=new DefaultHttpClient(); 

      Log.i("","HTTP client created"); 
      String responseData=""; 
     try { 
      HttpResponse response=httpClient.execute(new HttpGet(url)); 
      response.addHeader("Accept-Language", "it-IT"); 
      HttpEntity entity=response.getEntity(); 
      Log.i("", "HTTP Response arrived"); 

      BufferedReader bf=new BufferedReader(new 
      InputStreamReader((entity.getContent()),"UTF-8")); 
      String line=""; 
      Log.i("","Start buffre reading"); 

     while((line=bf.readLine())!=null){ 
      responseData=responseData+line; 
     } 

      JSONObject jsonObj = new JSONObject(responseData); 

      JSONArray resultArry = jsonObj.getJSONArray("results"); 

      Address = resultArry.getJSONObject(0).getString("formatted_address").toString(); 

     } catch (Exception e) { 
     // TODO Auto-generated catch block 
      e.printStackTrace(); 
    } 

    return Address; 
    } 
} 

我試圖改變做在這樣的StringBuffersb.append("http://maps.googleapis.com/maps/api/directions/json?origin="+ latitude +"&destination="+longitude +"&sensor=false");

但我用看到的結果並沒有顯示任何東西..敬酒是空的,我得到了一個行錯誤的JSONObject:地址= resultArry.getJSONObject(0).getString("formatted_address").toString();

因此,這爲t他JSON我將解析:http://maps.googleapis.com/maps/api/directions/json?origin=vicenza&destination=costabissara&sensor=false

,我會顯示路線..當然我改變:

`JSONArray resultArry = jsonObj.getJSONArray( 「結果」);

 Address = resultArry.getJSONObject(0).getString("formatted_address").toString();` 

`JSONArray resultArry = jsonObj.getJSONArray( 「路由」);

 Address = resultArry.getJSONObject(0).getString("legs").toString();` 

但沒有成功..

回答

1

我想你的例子,它的工作。我所要做的唯一的變化是強似座標ReadAddressFromWebService我通過出發地和目的地的字符串:

String from = "vicenza"; 
String to = "costabissara"; 

String Address = ReadAddressFromWebService(from,to); 

origindestination作品與地名不座標。

+0

謝謝你,你是對的..但是,在這種方式下,我可以設置2個不同的城市..我怎麼能回收目的地使用同一城市的2個地址?我不知道你是否理解.. – 2014-08-29 13:03:34

+1

@ End.Game [這是一個檢查](http://maps.googleapis.com/maps/api/directions/json?origin=ViaEugenioMontale36077,Vicenza,Italy&destination=StradadelleCaperse,15536​​100Casale ,意大利維琴察&傳感器= FALSE)。你只需用逗號分隔地址,城市和國家。您可以從[Google地圖](http://maps.google.com)獲取完整地址。 – Simas 2014-08-29 13:12:02

+0

哦,好的,非常感謝你:) – 2014-08-29 13:16:02