2012-12-24 42 views

回答

1

據我所知,沒有。距離矩陣是Google提供的唯一服務,它將爲您提供所需的信息(車,步行,兩點之間的自行車距離)。

用法很簡單。我已經使用了好幾次(JSON),並且效果很好。以下是獲取距離的代碼:

String sDistance = ""; 

      try 
      { 
       URL googleDMatrix = new URL("http://maps.googleapis.com/maps/api/distancematrix/json?origins=" 
         + URLEncoder.encode(params[6].toString(), "UTF-8") + "&destinations=" 
         + URLEncoder.encode(params[7].toString(), "UTF-8") + "&language=en-GB&sensor=false&units=imperial"); 
       URLConnection tc = googleDMatrix.openConnection(); 
       BufferedReader in = new BufferedReader(new InputStreamReader(tc.getInputStream())); 

       String line; 
       StringBuffer sb = new StringBuffer(); 
       // take Google's legible JSON and turn it into one big string. 
       while ((line = in.readLine()) != null) 
       { 
        sb.append(line); 
       } 

       // turn that string into a JSON object 
       JSONObject main = new JSONObject(sb.toString()); 
       // now get the JSON array that's inside that object 

       JSONArray rows_array = new JSONArray(main.getString("rows")); 
       JSONObject elements_object = rows_array.getJSONObject(0); 
       JSONArray elements_array = elements_object.getJSONArray("elements"); 
       JSONObject distance_object = elements_array.getJSONObject(0); 
       JSONObject distance = distance_object.getJSONObject("distance"); 

       double dist = (distance.getDouble("value")/1E3) * 0.62; 

       sDistance = Double.toString(dist); 
      } 
      catch (Exception e) 
      { 
       sDistance = "0"; 
       e.printStackTrace(); 
      } 

可能有一些額外的東西在這裏,因爲這是直接從我的項目。 params [6] params [7]是兩個點之間的距離將被計算。

相關問題