在这个例子中,我想知道哪个城市离温哥华更近。 Victoria,Washington或Texas?一个我找到最短距离我怎么能看到这个值属于哪个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;
}
}
}
答案 0 :(得分:0)
根据您提供的代码,您将从Google网址获取JSON,因此您应该阅读API文档,您一定会在那里得到答案。
编辑:如果您想遍历JSON数组,可以使用yourjsonarray.getJSONObject(index).getString("Distance")
。
答案 1 :(得分: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();
}
}
}