0

在这个例子中,我想知道哪个城市离温哥华更近。维多利亚、华盛顿还是德克萨斯?一个我找到最短距离的我如何查看该值属于哪个destination_address?

{
   "destination_addresses" : [
      "Victoria, BC, Canada",
      "Texas, États-Unis",
      "Washington, District de Columbia, États-Unis"
   ],
   "origin_addresses" : [ "Vancouver, BC, Canada" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "135 km",
                  "value" : 134638
               },
               "duration" : {
                  "text" : "6 heures 37 minutes",
                  "value" : 23815
               },
               "status" : "OK"
            },
            {
               "distance" : {
                  "text" : "3 457 km",
                  "value" : 3456694
               },
               "duration" : {
                  "text" : "7 jours 15 heures",
                  "value" : 658855
               },
               "status" : "OK"
            },
            {
               "distance" : {
                  "text" : "5 000 km",
                  "value" : 4999636
               },
               "duration" : {
                  "text" : "11 jours 2 heures",
                  "value" : 956487
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}

我试图通过解析距离并找到最小距离来找到离温哥华最近的城市。通过发现我们看到最短的距离属于维多利亚。我怎么能说这个最小距离属于维多利亚。 这是我寻找最近城市(最短距离)的代码

public class MainActivity extends Activity {
HttpClient client; 
double minDistance=0;
String destination_addresses;
JSONArray rows;
JSONArray elements;
final static String URL= "http://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver+BC&destinations=San+Francisco|Victoria+BC&mode=bicycling&language=fr-FR&sensor=false";
protected void OnCreate (Bundle savedInstanceState ){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    client = new DefaultHttpClient();
    }
public JSONObject ClosestObject (String Origin) throws ClientProtocolException, IOException , JSONException {

    StringBuilder url= new StringBuilder (URL);
    HttpGet get = new HttpGet(url.toString());
    HttpResponse r = client.execute(get);
    int status = r.getStatusLine().getStatusCode();
    if (status == 200){
        HttpEntity e = r.getEntity();
        String data = EntityUtils.toString(e);
        JSONArray locations = new JSONArray(data);
        for (int i=0; i< locations.length(); i++){
            rows=  (JSONArray) locations.getJSONObject(2).get("rows");
            elements= (JSONArray) locations.getJSONObject(0).get("elements");
            String Value= elements.getJSONObject(i).toString();
            double distanceValue = Double.parseDouble(Value);
            if ( distanceValue< minDistance){
                minDistance= distanceValue;
            }
        }

    }
4

2 回答 2

0

试试看 :)

JSONObject bigData = new JSONObject(data);
rows = bigData.getJSONArray("rows");
elements = rows.getJSONObject(0).getJSONArray("elements");
JSONObject ob = elements.getJSONObject(0);
String value = ob.getJSONObject("duration").getString("value");//or "distance"
minDistance = Double.parseDouble(value);
int index = 0;
for (int i = 1; i < elements.length(); i++) {
    ob = elements.getJSONObject(i);
    value = ob.getJSONObject("duration").getString("value");//or "distance"
    double distanceValue = Double.parseDouble(value);
       if (distanceValue < minDistance) {
        minDistance = distanceValue;
        index = i;
    }
}
JSONArray closers = bigData.getJSONArray("destination_addresses");
String closer = closers.getString(index);
Log.d("closer", closer);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
于 2013-04-01T05:24:45.380 回答
0

通过您提供的代码,您正在从 Google url 获取 JSON,因此您应该阅读 API 的文档,您肯定会在那里得到答案。 编辑: 如果你想遍历 JSON 数组,你可以使用yourjsonarray.getJSONObject(index).getString("Distance").

于 2013-04-01T04:10:57.477 回答