JAVA调用百度地图API {"status":240,"message":"APP 服务被禁用"}

博客时间2020/05/09
报错是因为2019.6.18号以后申请的账号,将不再支持v2版本查询了
然而sn加密哪里还是v2…所以对应编写下api就好
v3:http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding

/**
     * 根据地址获取经纬度
     */
    @PostMapping("/getAddressLngLat")
    public R<JSONObject> getAddressLngLat(@RequestBody @Valid GetAddressLngLatReq req) {
        String address = req.getAddress();
        JSONObject object = getCoordinate(address);
        JSONObject resp=new JSONObject();
        if (object != null) {
            resp = JSON.parseObject(object.getString("result"));

        }
        return R.ok(resp);
    }


    /**
     * 查询的地址
     */
    private static JSONObject getCoordinate(String addr) {
        String url = getUrl(addr);
        JSONObject jsonObject = loadJSON(url);
        return jsonObject;
    }

    public static JSONObject loadJSON(String url) {
        StringBuilder json = new StringBuilder();
        try {
            URL oracle = new URL(url);
            URLConnection yc = oracle.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream(), "UTF-8"));
            String inputLine = null;
            while ((inputLine = in.readLine()) != null) {
                json.append(inputLine);
            }
            in.close();
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        }
        if (!StringUtils.isEmpty(json)) {
            return JSON.parseObject(json.toString());
        } else {
            return null;
        }
    }
    //组装地址
    private static String getUrl(String address) {
        Map paramsMap = new LinkedHashMap<String, String>();
        paramsMap.put("address", address);
        paramsMap.put("output", "json");
        paramsMap.put("ak", "你的ak");
        String paramsStr = null;
        try {
            paramsStr = toQueryString(paramsMap);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String wholeStr = new String("/geocoding/v3/?" + paramsStr + "你的sk");
        String s = new String("/geocoding/v3/?" + paramsStr);
        String tempStr = null;
        try {
            tempStr = URLEncoder.encode(wholeStr, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "http://api.map.baidu.com" + s + "&sn=" + MD5(tempStr);
    }

    // 对Map内所有value作utf8编码,拼接返回结果
    public static String toQueryString(Map<?, ?> data)
            throws UnsupportedEncodingException {
        StringBuffer queryString = new StringBuffer();
        for (Map.Entry<?, ?> pair : data.entrySet()) {
            queryString.append(pair.getKey() + "=");
            queryString.append(URLEncoder.encode((String) pair.getValue(),
                    "UTF-8") + "&");
        }
        if (queryString.length() > 0) {
            queryString.deleteCharAt(queryString.length() - 1);
        }
        return queryString.toString();
    }

    // 来自stackoverflow的MD5计算方法,调用了MessageDigest库函数,并把byte数组结果转换成16进制
    public static String MD5(String md5) {
        try {
            java.security.MessageDigest md = java.security.MessageDigest
                    .getInstance("MD5");
            byte[] array = md.digest(md5.getBytes());
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < array.length; ++i) {
                sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100)
                        .substring(1, 3));
            }
            return sb.toString();
        } catch (java.security.NoSuchAlgorithmException e) {
        }
        return null;
    }

你可能感兴趣的:(第三方)