1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 谷歌地图Api部分使用

谷歌地图Api部分使用

时间:2024-03-29 15:13:20

相关推荐

谷歌地图Api部分使用

根据起点与终点经纬度获取之间距离与预估时间

请求链接示例

/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 = "/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;}

根据传入经纬度返回该点地址信息

请求链接示例

/maps/api/geocode/json?latlng=22.66511,114.1983&key=your_key

请求所返Json数据示例

{"plus_code" : {"compound_code" : "M58X+28 中国广东省深圳市龙岗区","global_code" : "7PJPM58X+28"},"results" : [{"address_components" : [{"long_name" : "6","short_name" : "6","types" : [ "street_number" ]},{"long_name" : "德全路","short_name" : "德全路","types" : [ "route" ]},{"long_name" : "龙岗区","short_name" : "龙岗区","types" : [ "political", "sublocality", "sublocality_level_1" ]},{"long_name" : "深圳市","short_name" : "深圳市","types" : [ "locality", "political" ]},{"long_name" : "广东省","short_name" : "广东省","types" : [ "administrative_area_level_1", "political" ]},{"long_name" : "中国","short_name" : "CN","types" : [ "country", "political" ]},{"long_name" : "518123","short_name" : "518123","types" : [ "postal_code" ]}],"formatted_address" : "中国广东省深圳市龙岗区德全路6号 邮政编码: 518123","geometry" : {"location" : {"lat" : 22.6643799,"lng" : 114.01},"location_type" : "ROOFTOP","viewport" : {"northeast" : {"lat" : 22.66572888029151,"lng" : 114.2024499802915},"southwest" : {"lat" : 22.6630309197085,"lng" : 114.199757085}}},"place_id" : "ChIJ5-6aBTd0BDQRm5cCAD6JqLI","plus_code" : {"compound_code" : "M672+QC 中国广东省深圳市龙岗区","global_code" : "7PJPM672+QC"},"types" : [ "street_address" ]},{"address_components" : [{"long_name" : "Unnamed Road","short_name" : "Unnamed Road","types" : [ "route" ]},{"long_name" : "龙岗区","short_name" : "龙岗区","types" : [ "political", "sublocality", "sublocality_level_1" ]},{"long_name" : "深圳市","short_name" : "深圳市","types" : [ "locality", "political" ]},{"long_name" : "广东省","short_name" : "广东省","types" : [ "administrative_area_level_1", "political" ]},{"long_name" : "中国","short_name" : "CN","types" : [ "country", "political" ]}],"formatted_address" : "Unnamed Road, 龙岗区深圳市广东省中国","geometry" : {"bounds" : {"northeast" : {"lat" : 22.6662071,"lng" : 114.698},"southwest" : {"lat" : 22.6655759,"lng" : 114.851}},"location" : {"lat" : 22.6658722,"lng" : 114.922},"location_type" : "GEOMETRIC_CENTER","viewport" : {"northeast" : {"lat" : 22.6672404802915,"lng" : 114.2027264302915},"southwest" : {"lat" : 22.6645425197085,"lng" : 114.2000284697085}}},"place_id" : "ChIJcRllOjd0BDQRojN7MXwz394","types" : [ "route" ]},{"address_components" : [{"long_name" : "龙岗区","short_name" : "龙岗区","types" : [ "political", "sublocality", "sublocality_level_1" ]},{"long_name" : "深圳市","short_name" : "深圳市","types" : [ "locality", "political" ]},{"long_name" : "广东省","short_name" : "广东省","types" : [ "administrative_area_level_1", "political" ]},{"long_name" : "中国","short_name" : "CN","types" : [ "country", "political" ]}],"formatted_address" : "中国广东省深圳市龙岗区","geometry" : {"bounds" : {"northeast" : {"lat" : 22.8136909,"lng" : 114.6284666},"southwest" : {"lat" : 22.4385811,"lng" : 114.0500933}},"location" : {"lat" : 22.720974,"lng" : 114.246899},"location_type" : "APPROXIMATE","viewport" : {"northeast" : {"lat" : 22.8136909,"lng" : 114.6284666},"southwest" : {"lat" : 22.4385811,"lng" : 114.0500933}}},"place_id" : "ChIJdariJoRtBDQR8NOUIzMLibU","types" : [ "political", "sublocality", "sublocality_level_1" ]},{"address_components" : [{"long_name" : "深圳市","short_name" : "深圳市","types" : [ "locality", "political" ]},{"long_name" : "广东省","short_name" : "广东省","types" : [ "administrative_area_level_1", "political" ]},{"long_name" : "中国","short_name" : "CN","types" : [ "country", "political" ]}],"formatted_address" : "中国广东省深圳市","geometry" : {"bounds" : {"northeast" : {"lat" : 22.8617483,"lng" : 114.6284666},"southwest" : {"lat" : 22.3963441,"lng" : 113.7514535}},"location" : {"lat" : 22.543096,"lng" : 114.057865},"location_type" : "APPROXIMATE","viewport" : {"northeast" : {"lat" : 22.8617483,"lng" : 114.6284666},"southwest" : {"lat" : 22.3963441,"lng" : 113.7514535}}},"place_id" : "ChIJkVLh0Aj0AzQRyYCStw1V7v0","types" : [ "locality", "political" ]},{"address_components" : [{"long_name" : "广东省","short_name" : "广东省","types" : [ "administrative_area_level_1", "political" ]},{"long_name" : "中国","short_name" : "CN","types" : [ "country", "political" ]}],"formatted_address" : "中国广东省","geometry" : {"bounds" : {"northeast" : {"lat" : 25.5167711,"lng" : 117.3186209},"southwest" : {"lat" : 20.2117179,"lng" : 109.6549363}},"location" : {"lat" : 23.3790333,"lng" : 113.7632828},"location_type" : "APPROXIMATE","viewport" : {"northeast" : {"lat" : 25.5167711,"lng" : 117.3186209},"southwest" : {"lat" : 20.2117179,"lng" : 109.6549363}}},"place_id" : "ChIJP1yvMvGFUjERKZ8lCW8c1C4","types" : [ "administrative_area_level_1", "political" ]},{"address_components" : [{"long_name" : "中国","short_name" : "CN","types" : [ "country", "political" ]}],"formatted_address" : "中国","geometry" : {"bounds" : {"northeast" : {"lat" : 53.5609739,"lng" : 134.7754563},"southwest" : {"lat" : 17.9996,"lng" : 73.4994136}},"location" : {"lat" : 35.86166,"lng" : 104.195397},"location_type" : "APPROXIMATE","viewport" : {"northeast" : {"lat" : 53.5609739,"lng" : 134.7754563},"southwest" : {"lat" : 17.9996,"lng" : 73.4994136}}},"place_id" : "ChIJwULG5WSOUDERbzafNHyqHZU","types" : [ "country", "political" ]},{"address_components" : [{"long_name" : "M58X+28","short_name" : "M58X+28","types" : [ "plus_code" ]},{"long_name" : "龙岗区","short_name" : "龙岗区","types" : [ "political", "sublocality", "sublocality_level_1" ]},{"long_name" : "深圳市","short_name" : "深圳市","types" : [ "locality", "political" ]},{"long_name" : "广东省","short_name" : "广东省","types" : [ "administrative_area_level_1", "political" ]},{"long_name" : "中国","short_name" : "CN","types" : [ "country", "political" ]}],"formatted_address" : "M58X+28 中国广东省深圳市龙岗区","geometry" : {"bounds" : {"northeast" : {"lat" : 22.665125,"lng" : 114.198375},"southwest" : {"lat" : 22.665,"lng" : 114.19825}},"location" : {"lat" : 22.66511,"lng" : 114.1983},"location_type" : "ROOFTOP","viewport" : {"northeast" : {"lat" : 22.6664114802915,"lng" : 114.1996614802915},"southwest" : {"lat" : 22.6637135197085,"lng" : 114.1969635197085}}},"place_id" : "GhIJGD4ipkSqNkAR_rJ78rCMXEA","plus_code" : {"compound_code" : "M58X+28 中国广东省深圳市龙岗区","global_code" : "7PJPM58X+28"},"types" : [ "plus_code" ]}],"status" : "OK"}

请求参数

latlng:索要查询地点的经纬度。

此参数的经纬度传递要反向,前为纬度,后为经度

此参数的经纬度传递要反向,前为纬度,后为经度

此参数的经纬度传递要反向,前为纬度,后为经度

key:你的谷歌key

Java代码

/*** 根据经纬度获取地址* @param longitude* @param latitude* @return*/public static String getGoogleAddressBylatlng(double jigndu, double weidu ) {String result = "";String addr = "";Object results="";try {String url = "/maps/api/geocode/json?latlng="+weidu+","+jigndu+"&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);results = object.get("results");JSONArray json = JSONArray.fromObject(results);Object obj[] = json.toArray();JSONObject obj1 = null;if (obj.length > 0) {obj1 = JSONObject.fromObject(obj[0]);addr = (String) obj1.get("formatted_address");} else {addr = "未知地址";}} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return addr;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。