java接入高德地图常用WEB API

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

java接入高德地图常用WEB API_第1张图片

2、根据经经纬度查询高德地图省市区数据

/**
     *根据经纬度获取省市区
     * @param lat
     * @return
     */
    public static String getCoordinate(Double lng, Double lat) throws IOException {
        String url = "http://restapi.amap.com/v3/geocode/regeo";
        Map 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 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 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;
    }

你可能感兴趣的:(后端程序设计)