高德抓路服务精确获取道路定位信息 道路等级 限速值

最近由于公司需要获取车辆当前所在地点的道路等级和限速值,但是由于以前是根据gps回传定位位置名称进行判断,容易造成数据误报,所以将车辆最新的三个点位信息存储起来进行高德查询 引用了hutool工具包
实例:
package com.ltmonitor.util;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**

  • @program: gps-root

  • @description: 高德服务

  • @author: wj

  • @create: 2020-04-28 13:49
    **/
    public class LbsLocation {

    private static String key=“key”;

    public static JSONArray GDautograsp(String carid,String locations,String time,String direction,String speed) {
    JSONArray jsonArray=new JSONArray();
    // carid=“abcd123456”;
    // locations=“116.496167,39.917066|116.496149,39.917205|116.496149,39.917326”;
    // time=“1434077500,1434077501,1434077510”;
    // direction=“1,1,2”;
    // speed=“1,1,2”;
    // carid=“1db1”+carid;
    String httpUrl = “https://restapi.amap.com/v3/autograsp?carid=”+carid+
    “&locations=”+locations +
    “&time=”+time +
    “&direction=”+direction +
    “&speed=”+speed +
    “&output=json&key=”+key;
    StringBuilder json = new StringBuilder();
    URL url = null;
    HttpURLConnection httpConn = null;
    try {
    String address = httpUrl;
    url = new URL(address);
    httpConn = (HttpURLConnection) url.openConnection();
    //URL连接可用于input或output。如果想用URL连接输入,设置DoInput标签为true。
    //输入和输出是针对计算机的,如果以程序员的角度考虑,经常弄混。
    //input输入,output输出,那么不是从output里read,input中write吗,其实相反
    //input输入进计算机,计算机才能读,所以是从input read,而output是计算机输出,通过output write。
    httpConn.setDoOutput(false);
    //所以如果setDoInput(false),想从URLConnection读取时不能读取
    //Cannot read from URLConnection if doInput=false (call setDoInput(true))
    httpConn.setDoInput(true);
    //连接建立超时时间还有读取数据超时时间,
    httpConn.setConnectTimeout(10000);
    httpConn.setReadTimeout(10000);
    httpConn.setRequestMethod(“GET”);
    httpConn.connect();
    //获取状态码
    int code = httpConn.getResponseCode();
    System.out.println(code);
    //读http请求响应
    BufferedReader reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), “utf-8”));
    String inputLine = null;
    while((inputLine = reader.readLine()) != null) {
    json.append(inputLine);
    }
    String maxspeed=JSONUtil.parseObj(json.toString()).get(“roads”).toString();
    jsonArray=JSONUtil.parseArray(maxspeed);
    //关闭IO和连接
    reader.close();
    httpConn.disconnect();
    }
    catch(Exception e){
    System.out.println(“error in wapaction,and e is " + e.getMessage());
    }
    finally{
    if(httpConn!=null){
    httpConn.disconnect();
    }
    }
    return jsonArray;
    }
    public static void main(String[] args) {
    System.out.println(GDautograsp(”","","","",""));
    }

}

你可能感兴趣的:(java,高德抓路服务)