用百度开放地图api在代码中获得两地距离
生活随笔
收集整理的這篇文章主要介紹了
用百度开放地图api在代码中获得两地距离
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
示例:
<!DOCTYPE html> <html> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta name="viewport" content="initial-scale=1.0, user-scalable=no" /><style type="text/css">body, html {width: 100%;height: 100%; margin:0;font-family:"微軟雅黑";}#allmap{height:500px;width:100%;}#r-result,#r-result table{width:100%;}</style><script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=9AvzGkm2h2EDbNmULlOApaiOCteZWcqF"></script><script type="text/javascript" src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script><title>根據起終點名稱駕車導航</title> </head> <body> <div id="allmap"></div> <div id="driving_way"><select><option value="0">最少時間</option><option value="1">最短距離</option><option value="2">避開高速</option></select><input type="button" id="result" value="查詢"/> </div> <div id="r-result"></div> </body> </html> <script type="text/javascript">// 百度地圖API功能var map = new BMap.Map("allmap");var start = "天安門";var end = "金燕龍辦公樓";map.centerAndZoom(new BMap.Point(116.404, 39.915), 11);map.enableScrollWheelZoom(true); //開啟鼠標滾輪縮放//三種駕車策略:最少時間,最短距離,避開高速var routePolicy = [BMAP_DRIVING_POLICY_LEAST_TIME,BMAP_DRIVING_POLICY_LEAST_DISTANCE,BMAP_DRIVING_POLICY_AVOID_HIGHWAYS];$("#result").click(function(){map.clearOverlays();var i=$("#driving_way select").val();search(start,end,routePolicy[i]);function search(start,end,route){var driving = new BMap.DrivingRoute(map, {renderOptions:{map: map, autoViewport: true},policy: route});driving.search(start,end);}}); </script>打開該html可以看到百度地圖
工具類:
/*** 百度地圖操作工具類*/ public class BaiduMapUtils {public static void main(String[] args) {String origin = getCoordinate("北京市育新花園小區");String destination = getCoordinate("北京市百度大廈");Double distance = getDistance(origin, destination);System.out.println("訂單距離:"+distance + "米");Integer time = getTime(origin, destination);System.out.println("線路耗時"+time+"秒");}private static String AK = "UEBQm9c3KZ5LrsO2C2qsOAs1eSdLvlzM";/*** 調用百度地圖地理編碼服務接口,根據地址獲取坐標(經度、緯度)* @param address* @return*/public static String getCoordinate(String address){String httpUrl = "http://api.map.baidu.com/geocoding/v3/?address=" + address + "&output=json&ak=" + AK;String json = loadJSON(httpUrl);Map map = JSON.parseObject(json, Map.class);String status = map.get("status").toString();if(status.equals("0")){//返回結果成功,能夠正常解析地址信息Map result = (Map) map.get("result");Map location = (Map) result.get("location");String lng = location.get("lng").toString();String lat = location.get("lat").toString();DecimalFormat df = new DecimalFormat("#.######");String lngStr = df.format(Double.parseDouble(lng));String latStr = df.format(Double.parseDouble(lat));String r = latStr + "," + lngStr;return r;}return null;}/*** 調用百度地圖駕車路線規劃服務接口,根據寄件人地址和收件人地址坐標計算訂單距離* @param origin* @param destination* @return*/public static Double getDistance(String origin,String destination){String httpUrl = "http://api.map.baidu.com/directionlite/v1/driving?origin="+origin+"&destination="+destination+"&ak=" + AK;String json = loadJSON(httpUrl);Map map = JSON.parseObject(json, Map.class);if ("0".equals(map.getOrDefault("status", "500").toString())) {Map childMap = (Map) map.get("result");JSONArray jsonArray = (JSONArray) childMap.get("routes");JSONObject jsonObject = (JSONObject) jsonArray.get(0);double distance = Double.parseDouble(jsonObject.get("distance") == null ? "0" : jsonObject.get("distance").toString());return distance;}return null;}/*** 調用百度地圖駕車路線規劃服務接口,根據寄件人地址和收件人地址坐標計算訂單距離* @param origin* @param destination* @return*/public static Integer getTime(String origin,String destination){String httpUrl = "http://api.map.baidu.com/directionlite/v1/driving?origin="+origin+"&destination="+destination+"&ak=" + AK;String json = loadJSON(httpUrl);Map map = JSON.parseObject(json, Map.class);if ("0".equals(map.getOrDefault("status", "500").toString())) {Map childMap = (Map) map.get("result");JSONArray jsonArray = (JSONArray) childMap.get("routes");JSONObject jsonObject = (JSONObject) jsonArray.get(0);int time = Integer.parseInt(jsonObject.get("duration") == null ? "0" : jsonObject.get("duration").toString());return time;}return null;}/*** 調用服務接口,返回百度地圖服務端的結果* @param httpUrl* @return*/public static String loadJSON(String httpUrl){StringBuilder json = new StringBuilder();try {URL url = new URL(httpUrl);URLConnection urlConnection = url.openConnection();BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));String inputLine = null;while ((inputLine = in.readLine()) != null) {json.append(inputLine);}in.close();} catch (MalformedURLException e) {} catch (IOException e) {}System.out.println(json.toString());return json.toString();} }結果:
總結
以上是生活随笔為你收集整理的用百度开放地图api在代码中获得两地距离的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【代码学习】lua+redis分布式锁代
- 下一篇: springboot中使用规则引擎Dro