我需要在网址上运行查询:
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=-23.601646,-46.650278&destinations=-22.907560,-43.481736&key=CODE_KEY");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
JSONObject jObject = new JSONObject(result);
String nameAdress = jObject.getString("destination_addresses");
/*Show in TextView*/
tvDistancia.setText(nameAdress);
} catch (Exception e) {
// Oops
}
/*finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}*/
然后检索数据Json。
URL中的哪些更改是报告的源和目标坐标。
我尝试了这种方式并没有成功。
{
"destination_addresses" : [ "Unnamed Road - Senador Camará, Rio de Janeiro - RJ, Brasil" ],
"origin_addresses" : [
"R. João de Castilho, 31 - Vila Clementino, São Paulo - SP, 04041-070, Brasil"
],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "412 km",
"value" : 411915
},
"duration" : {
"text" : "5 horas 46 minutos",
"value" : 20780
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
结果json:
{{1}}
也许有更好的方法来做到这一点。
文档:Link