• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

通过经纬度转换城市 微信小程序上传腾讯经纬度 通过经纬度转换成城市 ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

Java实现调用腾讯地图接口获取行政地区信息

 

开始之前做好如下准备:

腾讯地图API:http://lbs.qq.com/webservice_v1/guide-gcoder.html
腾讯地图秘钥(百度如何获取):KEY
调用接口:http://apis.map.qq.com/ws/geocoder/v1/?location=39.984154,116.307490&key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77&get_poi=1
需要的jar包:
json-lib-2.3-jdk15.jar(我用的是jdk1.8,这个jdk15我暂时不知道是啥,不过我可以正常使用)
commons-beanutils-1.8.0.jar
commons-collections-3.2.1.jar
commons-lang-2.4.jar
commons-logging-1.1.1.jar
ezmorph-1.0.6.jar
我整理好了所需的jar包,这是下载地址:https://download.csdn.net/download/blue_hh/10453610
当然,需要是maven项目的话,就简单粗暴了。以下是maven依赖:

    <dependencies>
<!--腾讯地图api star-->
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.8.0</version>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>net.sf.ezmorph</groupId>
            <artifactId>ezmorph</artifactId>
            <version>1.0.6</version>
        </dependency>
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <classifier>jdk15</classifier>
            <version>2.2.3</version>
        </dependency>
<!--腾讯地图api end-->
    </dependencies>

代码调用类:

package com.weixiu.zxt.utils;
import net.sf.json.JSONObject;

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

/**
 * @Author: 张学涛
 * @Date: 2020-05-26 11:27
 * @Version 1.0
 */
public class LocationUtils {
    // key
    private static final String KEY = "OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77";

    /**
     * <p>
     * 通过经纬度获取位置
     * </p>
     *
     * @author 张学涛
     * @since 2020-05-26
     */
    public static Map<String, Object> getLocation(String lng, String lat) {

        Map<String, Object> resultMap = new HashMap<String, Object>();

        // 参数解释:lng:经度,lat:维度。KEY:腾讯地图key,get_poi:返回状态。1返回,0不返回
        String urlString = "http://apis.map.qq.com/ws/geocoder/v1/?location=" + lat + "," + lng + "&key=" + KEY + "&get_poi=1";
        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();
        }
        System.out.println(result);
        // 转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;
    }

    public static void main(String[] args) {

        // 测试
        String lng = "118.116112";//经度
        String lat = "39.36";//维度
        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"));
    }

}

运行结果:

{
    "status": 0,
    "message": "query ok",
    "request_id": "213eb7ec-9f16-11ea-bec6-5254007c7f9e",
    "result": {
        "location": {
            "lat": 39.36,
            "lng": 118.116112
        },
        "address": "河北省唐山市丰南区",
        "formatted_addresses": {
            "recommend": "丰南区柳树酄镇柳北村",
            "rough": "丰南区柳树酄镇柳北村"
        },
        "address_component": {
            "nation": "中国",
            "province": "河北省",
            "city": "唐山市",
            "district": "丰南区",
            "street": "",
            "street_number": ""
        },
        "ad_info": {
            "nation_code": "156",
            "adcode": "130207",
            "city_code": "156130200",
            "name": "中国,河北省,唐山市,丰南区",
            "location": {
                "lat": 39.330021,
                "lng": 118.375
            },
            "nation": "中国",
            "province": "河北省",
            "city": "唐山市",
            "district": "丰南区"
        },
        "address_reference": {
            "town": {
                "id": "130207110",
                "title": "柳树酄镇",
                "location": {
                    "lat": 39.357296,
                    "lng": 118.013344
                },
                "_distance": 0,
                "_dir_desc": "内"
            },
            "landmark_l2": {
                "id": "15150289657492403698",
                "title": "柳北村",
                "location": {
                    "lat": 39.351933,
                    "lng": 118.112129
                },
                "_distance": 961.3,
                "_dir_desc": "北"
            }
        },
        "poi_count": 10,
        "pois": [
            {
                "id": "15150289657492403698",
                "title": "柳北村",
                "address": "河北省唐山市丰南区",
                "category": "地名地址:行政地名",
                "location": {
                    "lat": 39.351933,
                    "lng": 118.112129
                },
                "ad_info": {
                    "adcode": "130207",
                    "province": "河北省",
                    "city": "唐山市",
                    "district": "丰南区"
                },
                "_distance": 961.3,
                "_dir_desc": "北"
            },
            {
                "id": "1137573208476801566",
                "title": "柳树瞿农业技术推广区域综合站",
                "address": "河北省唐山市丰南区386乡道西50米",
                "category": "机构团体:政府机关",
                "location": {
                    "lat": 39.357449,
                    "lng": 118.110092
                },
                "ad_info": {
                    "adcode": "130207",
                    "province": "河北省",
                    "city": "唐山市",
                    "district": "丰南区"
                },
                "_distance": 590.9,
                "_dir_desc": "东北"
            },
            {
                "id": "9998963502928947632",
                "title": "蒲台河村村委会",
                "address": "河北省唐山市丰南区胥涧线尖子沽乡蒲台河村村民委员会",
                "category": "机构团体:政府机关",
                "location": {
                    "lat": 39.365437,
                    "lng": 118.111633
                },
                "ad_info": {
                    "adcode": "130207",
                    "province": "河北省",
                    "city": "唐山市",
                    "district": "丰南区"
                },
                "_distance": 717.5,
                "_dir_desc": "东南"
            },
            {
                "id": "2168461130746174169",
                "title": "中共柳树瞿镇委员会",
                "address": "河北省唐山市丰南区386乡道西50米",
                "category": "机构团体:政府机关",
                "location": {
                    "lat": 39.35268,
                    "lng": 118.110428
                },
                "ad_info": {
                    "adcode": "130207",
                    "province": "河北省",
                    "city": "唐山市",
                    "district": "丰南区"
                },
                "_distance": 950.5,
                "_dir_desc": "东北"
            },
            {
                "id": "47706947276927087",
                "title": "唐山市丰南区柳树瞿阝镇柳北村村民委员会",
                "address": "河北省唐山市丰南区386乡道东150米",
                "category": "机构团体:政府机关",
                "location": {
                    "lat": 39.351933,
                    "lng": 118.112129
                },
                "ad_info": {
                    "adcode": "130207",
                    "province": "河北省",
                    "city": "唐山市",
                    "district": "丰南区"
                },
                "_distance": 961.3,
                "_dir_desc": "北"
            },
            {
                "id": "8630756855486014819",
                "title": "柳树瞿镇人民政府",
                "address": "河北省唐山市丰南区中国移动柳树瞿区域管理中心附近",
                "category": "机构团体:政府机关",
                "location": {
                    "lat": 39.352528,
                    "lng": 118.11039
                },
                "ad_info": {
                    "adcode": "130207",
                    "province": "河北省",
                    "city": "唐山市",
                    "district": "丰南区"
                },
                "_distance": 966.8,
                "_dir_desc": "东北"
            },
            {
                "id": "2910919500614693976",
                "title": "唐山市丰南区柳树酄镇委",
                "address": "河北省唐山市丰南区胥涧线",
                "category": "机构团体:政府机关",
                "location": {
                    "lat": 39.352711,
                    "lng": 118.109978
                },
                "ad_info": {
                    "adcode": "130207",
                    "province": "河北省",
                    "city": "唐山市",
                    "district": "丰南区"
                },
                "_distance": 968.1,
                "_dir_desc": "东北"
            },
            {
                "id": "13131799084140645835",
                "title": "唐山市丰南区柳树酄镇人大",
                "address": "河北省唐山市丰南区386乡道西50米",
                "category": "机构团体:政府机关",
                "location": {
                    "lat": 39.352444,
                    "lng": 118.110474
                },
                "ad_info": {
                    "adcode": "130207",
                    "province": "河北省",
                    "city": "唐山市",
                    "district": "丰南区"
                },
                "_distance": 971.2,
                "_dir_desc": "东北"
            },
            {
                "id": "7631071592149128051",
                "title": "唐山市柳树圈镇政府",
                "address": "河北省唐山市丰南区柳树圈镇柳北村委会",
                "category": "机构团体:政府机关",
                "location": {
                    "lat": 39.352489,
                    "lng": 118.110367
                },
                "ad_info": {
                    "adcode": "130207",
                    "province": "河北省",
                    "city": "唐山市",
                    "district": "丰南区"
                },
                "_distance": 971.4,
                "_dir_desc": "东北"
            },
            {
                "id": "12171363555386895350",
                "title": "明本超市",
                "address": "河北省唐山市丰南区胥涧线",
                "category": "购物:超市",
                "location": {
                    "lat": 39.363098,
                    "lng": 118.105431
                },
                "ad_info": {
                    "adcode": "130207",
                    "province": "河北省",
                    "city": "唐山市",
                    "district": "丰南区"
                },
                "_distance": 981.8,
                "_dir_desc": "东"
            }
        ]
    }
}

{province=河北省, nation=中国, city=唐山市, provinceCode=130207, cityCode=156130200, nationCode=156}
国   籍:中国
国家代码:156
省   份:河北省
省份代码:130207
城   市:唐山市
城市代码:156130200
View Code

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
微信小程序使用Vant组件库发布时间:2022-07-18
下一篇:
活动抽奖小程序发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap