java腾讯地图根据经纬度获取具体位置

腾讯地图

  • java腾讯地图根据经纬度获取具体位置

java腾讯地图根据经纬度获取具体位置

package com.fengdi.lianmeng.util.tencent;

import com.fengdi.lianmeng.util.MD5;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * 腾讯地图工具类
 */

public class LocationUtils {
     

    private static Logger log = LoggerFactory.getLogger(LocationUtils.class);

    private static final String KEY = "腾讯地图应用key";
    private static final String SECRET_KEY = "腾讯地图秘钥";

    /**
     * 通过经纬度获取位置
     * @param lng
     * @param lat
     * @return
     */
    public static Map<String, Object> getLocation(String lng, String lat) {
     
        Map<String, Object> resultMap = new HashMap<>();

        // 参数解释:lng:经度,lat:维度。KEY:腾讯地图key,get_poi:返回状态。1返回,0不返回
        String urlString = "https://apis.map.qq.com/ws/geocoder/v1?key=" + KEY + "&location=" + lat + "," + lng + "&sig=" + getTxMapSig(lng, lat);
        String result = "";
        try {
     
            URL url = new URL(urlString);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            // 腾讯地图使用GET
            conn.setRequestMethod("GET");
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String line;
            // 获取地址解析结果
            while ((line = in.readLine()) != null) {
     
                result += line + "\n";
            }
            in.close();
        } catch (Exception e) {
     
            e.getMessage();
        }

        // 转JSON格式
        JSONObject jsonObject = JSONObject.fromObject(result).getJSONObject("result");
        // 获取地址(行政区划信息) 包含有国籍,省份,城市
        JSONObject adInfo = jsonObject.getJSONObject("ad_info");
        resultMap.put("nation", adInfo.get("nation"));
        resultMap.put("nationCode", adInfo.get("nation_code"));
        resultMap.put("province", adInfo.get("province"));
        resultMap.put("provinceCode", adInfo.get("adcode"));
        resultMap.put("city", adInfo.get("city"));
        resultMap.put("cityCode", adInfo.get("city_code"));
        return resultMap;
    }

    private static String getTxMapSig (String lng, String lat) {
     
        return MD5.encryptByMD5("/ws/geocoder/v1?key=BRXBZ-HSUCF-K6QJI-NTWJY-2HDXZ-FJFYH&location=" + lat + "," + lng + SECRET_KEY);
    }

    public static void main(String[] args) {
     

        // 测试
        String lng = "111.546112";//经度
        String lat = "24.378622";//维度
        Map<String, Object> map = getLocation(lng, lat);
        System.out.println(map);
        System.out.println("国   籍:" + map.get("nation"));
        System.out.println("国家代码:" + map.get("nationCode"));
        System.out.println("省   份:" + map.get("province"));
        System.out.println("省份代码:" + map.get("provinceCode"));
        System.out.println("城   市:" + map.get("city"));
        System.out.println("城市代码:" + map.get("cityCode"));
    }
}

你可能感兴趣的:(腾讯地图,腾讯地图)