根据起点与终点经纬度获取之间距离与预估时间
请求链接示例
https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=22.66511,114.1983&destinations=22.69229%2C114.1978&mode=driving&key=your_key
请求所返Json数据示例
{
"destination_addresses" : [ "中国广东省东莞市博深高速公路" ],
"origin_addresses" : [ "中国广东省深圳市龙岗区德全路6号 邮政编码: 518123" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "8.3 公里",
"value" : 8306
},
"duration" : {
"text" : "16分钟",
"value" : 970
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
请求参数
origins:起点经纬度
destinations:终点经纬度,以%2C为间隔(%2c=,)
以上参数的经纬度传递要反向,前为纬度,后为经度
以上参数的经纬度传递要反向,前为纬度,后为经度
以上参数的经纬度传递要反向,前为纬度,后为经度
units(默认为metric):
metric :(默认值)以公里和米为单位返回距离
units:用英里和英尺来表示距离
mode(出行方式):
BICYCLING:骑行 要求通过自行车道和优先道路骑自行车方向(目前只有在美国和一些加拿大城市)
DRIVING:驾驶 (default)(默认值)显示使用道路网络的标准行车方向
TRANSIT:公共交通 通过公共交通路线询问路线。
WALKING:步行 要求行人路及人行道(如有的话)提供行走方向
key: 你的谷歌key
Java代码
/**
* 距离测量
* @return
*/
public static Map<String,Integer> getJuliAndTime(double startjingdu, double startweidu,double endjingdu,double endweidu)throws Exception {
String result = "";
Map<String,Integer> map = new HashMap<>();
String url = "https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins="+startweidu+","+startjingdu+"&destinations="+endweidu+"%2C"+endjingdu+"&mode=driving&key="+Peizhi.google_map_key;
URL U = new URL(url);
URLConnection connection = U.openConnection();
InputStream ins = connection.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(ins, "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result += new String(line) + "\n";
}
JSONObject object = JSONObject.fromObject(result);
Object rows= object.get("rows");
JSONArray json = JSONArray.fromObject(rows);
//距离
JSONArray elements1 = JSONArray.fromObject(json.toArray()[0]);
JSONObject location = (JSONObject) elements1.get(0);
Object elements2 = location.get("elements");
JSONArray element3 = JSONArray.fromObject(elements2);
JSONObject distance2 = (JSONObject) element3.get(0);
JSONObject distance3 = (JSONObject) distance2.get("distance");
//两点之间距离(米)
Integer juli = (Integer) distance3.get("value");
JSONObject duration2 = (JSONObject) distance2.get("duration");
//预计行驶时间(秒)
Integer yujishijian = (Integer) duration2.get("value");
map.put("juli",juli);
map.put("yujishitime",yujishijian);
return map;
}
根据传入经纬度返回该点地址信息
请求链接示例
https://maps.googleapis.com/maps/api/geocode/json?latlng=22.66511,114.1983&key=your_key
请求所返Json数据示例
{
"plus_code" : {
"compound_code" : "M58X+28 中国广东省深圳市龙岗区",
"global_code" : "7PJPM58X+28"