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