java接入高德地圖常用WEB API

盧水發發表於2020-02-22

1、先看一下高德地圖WEB API都有哪些內容

java接入高德地圖常用WEB API

2、根據經經緯度查詢高德地圖省市區資料

/**
     *根據經緯度獲取省市區
     * @param lat
     * @return
     */
    public static String getCoordinate(Double lng, Double lat) throws IOException {
        String url = "http://restapi.amap.com/v3/geocode/regeo";
        Map<String,Object> parameters = Maps.newHashMap();
        parameters.put("key",mapKey);
        parameters.put("location",lng+","+lat);
        parameters.put("poitype","");
        parameters.put("radius","");
        parameters.put("extensions","base");
        parameters.put("batch","true");
        String resultData = HttpUtil.get(url, parameters, 3000, 3000, "UTF-8");
        if (StringUtils.isNotBlank(resultData)) {
            JSONObject jsonObject = (JSONObject) JSON.parseObject(resultData.toString()).getJSONArray("regeocodes").get(0);
            JSONObject addressComponent = jsonObject.getJSONObject("addressComponent");
            return addressComponent.getString("province").replace("省","")+""+addressComponent.getString("city");
        }else{
            return "";
        }
    }
複製程式碼

3、根據ip地址獲取省市區

/**
     * 根據ip獲取地址
     * @param ip
     * @return
     */
    public static String getAddressByIp(String ip){
        log.info("getAddressByIp ip={}",ip);
        try {
            String url = "https://restapi.amap.com/v3/ip";
            Map<String,Object> parameters = Maps.newHashMap();
            //ip=114.247.50.2&output=xml&key=<使用者的key>
            parameters.put("ip",ip);
            parameters.put("key",mapKey);
            String jsonResult = HttpUtil.get(url, parameters, 3000, 3000, "UTF-8");
            if (null!=jsonResult) {
                log.info("getAddressByIp result={}",jsonResult);
                JSONObject resultObj = JSONObject.parseObject(jsonResult);
                String returnStr = resultObj.getString("province")+","+resultObj.getString("city");
                if(returnStr.contains("[],[]")){
                    return "地球村";
                }
                return returnStr;
            }
        } catch (Exception e) {
            log.error("getAddressByIp error={}",e);
        }
        return "地球村";
    }
複製程式碼

4、根據商家關鍵字、城市名 搜尋商家標註資訊

 /**
     * 查詢附近的資料
     * @param keywords
     * @param city
     * @param page
     * @param pagesize
     * @return
     */
    public static JSONArray searchMapNearby(String keywords,String city,int page,int pagesize){
        log.info("searchMapNearby keywords={}",keywords);
        try {
            String url = "https://restapi.amap.com/v3/place/text";
            Map<String,Object> parameters = Maps.newHashMap();
            //restapi.amap.com/v3/place/text?key=您的key&keywords=電影院&types=&city=深圳&children=1&offset=20&page=1&extensions=all
            parameters.put("key",mapKey);
            parameters.put("keywords",keywords);
            parameters.put("types","");
            parameters.put("city",city);
            parameters.put("children",1);
            parameters.put("offset",pagesize);
            parameters.put("page",page);
            parameters.put("extensions","all");
            String jsonResult = HttpUtil.get(url, parameters, 3000, 3000, "UTF-8");
            if (StringUtils.isNotBlank(jsonResult)) {
                log.info("searchMapNearby result={}",jsonResult);
                JSONObject resultJson = JSON.parseObject(jsonResult);
                JSONArray pois = resultJson.getJSONArray("pois");
                return pois;
            }
        } catch (Exception e) {
            log.error("searchMapNearby error={}",e);
            //異常返回空陣列
            return new JSONArray();
        }
        return null;
    }
複製程式碼

相關文章