java用http请求高德,百度地图坐标以及驾驶时长

高德:
根据起始地计算驾驶时间


```java
//驾车
  @Override
    public double getDrivingTime(String sendAddr, String receiverAddr) {
        String urlStr = "https://restapi.amap.com/v4/direction/truck?key=244f1717f5857cddfxxxxxx&size=4&origin=";
        String sendAddress = getLonLat(sendAddr);
        String receiverAddress = getLonLat(receiverAddr);
        String[] subSendAddress = sendAddress.split(",");
        String[] subreceiverAddress = receiverAddress.split(",");
        double startLng = Double.parseDouble(subSendAddress[0]);
        double startLat = Double.parseDouble(subSendAddress[1]);
        double endLng = Double.parseDouble(subreceiverAddress[0]);
        double endLat = Double.parseDouble(subreceiverAddress[1]);
        String url = urlStr + startLng + "," + startLat + "&destination=" + endLng + "," + endLat;
        String result = httpService.executGet(url);
        if (!EmptyUtils.isEmpty(result)) {
            JSONObject jsStr = JSONObject.parseObject(result);
            JSONObject data = (JSONObject)jsStr.get("data");
            JSONObject route = (JSONObject)data.get("route");
            JSONArray paths = (JSONArray)route.get("paths");
            double totalDistance = 0L;
            for (int i = 0; i < paths.size(); i++) {
                JSONObject path = (JSONObject)paths.get(i);
                Integer distance = (Integer)path.get("distance");
                totalDistance += Double.parseDouble(distance+"");
            }
            // 按照35km/h计算
            double minute = Math.rint(totalDistance / 1000 / 45 * 60);
            return minute;
        }
        return 0;
    }
//货车
 @Override
    public double getAmapTruckTime(String sendAddr, String receiverAddr) {
        String urlStr = "https://restapi.amap.com/v4/direction/truck?key=244f1717f5857cddfxxxxxx&size=4&origin=";
        String sendAddress = getLonLat(sendAddr);
        String receiverAddress = getLonLat(receiverAddr);
        String[] subSendAddress = sendAddress.split(",");
        String[] subreceiverAddress = receiverAddress.split(",");
        double startLng = Double.parseDouble(subSendAddress[0]);
        double startLat = Double.parseDouble(subSendAddress[1]);
        double endLng = Double.parseDouble(subreceiverAddress[0]);
        double endLat = Double.parseDouble(subreceiverAddress[1]);
        String url = urlStr + startLng + "," + startLat + "&destination=" + endLng + "," + endLat;
        String result = httpService.executGet(url);
        if (!EmptyUtils.isEmpty(result)) {
            JSONObject jsStr = JSONObject.parseObject(result);
            JSONObject data = (JSONObject)jsStr.get("data");
            JSONObject route = (JSONObject)data.get("route");
            JSONArray paths = (JSONArray)route.get("paths");
            double totalDistance = 0L;
            for (int i = 0; i < paths.size(); i++) {
                JSONObject path = (JSONObject)paths.get(i);
                Integer distance = (Integer)path.get("distance");
                totalDistance += Double.parseDouble(distance+"");
            }
            // 按照35km/h计算
            double minute = Math.rint(totalDistance / 1000 / 45 * 60);
            return minute;
        }
        return 0;
    }

    /**
     * 根据地址获取经纬度
     *
     * @param address
     * @return
     */
    private String getLonLat(String address) {
        String url = "http://restapi.amap.com/v3/geocode/geo?key=244f1717f5857cddfxxxxxx&address=" + address
            + "&output=json";
        if (null == httpService) {
            httpService = new HttpService();
        }
        String text = httpService.executGet(url);
        JSONObject jsonObject = JSON.parseObject(text);
        String status = jsonObject.getString("status");
        if (!"1".equals(status)) {
            return null;
        }
        String strGeocodes = jsonObject.getString("geocodes");
        JSONArray geocodes = JSON.parseArray(strGeocodes);
        if (geocodes.isEmpty()) {
            return null;
        }
        String location = geocodes.getJSONObject(0).getString("location");
        return location;
    }
百度:
   

```java
//百度根据地址获取经纬度
 private String getbaiduLonLat(String address) {
        String url = "http://api.map.baidu.com/geocoder/v2/?ak=1T6fEGBG4fPVnhq5NxxxxxxS&address=" + address
            + "&output=json";
        if (null == httpService) {
            httpService = new HttpService();
        }
        String text = httpService.executGet(url);
        JSONObject jsonObject = JSON.parseObject(text);
        String status = jsonObject.getString("status");
        if (!"0".equals(status)) {
            return null;
        }
        JSONObject result = (JSONObject)jsonObject.get("result");
        JSONObject location = (JSONObject)result.get("location");
        location.get("lat");
        location.get("lng");
        String locationStr = location.get("lng") + "," + location.get("lat");
        return locationStr;
    }

你可能感兴趣的:(亲力亲为,java获取行驶时间,百度地图,多点连线,用户轨迹)