高德地图地理编码与逆地理编码

66 阅读1分钟

1.地理编码

在这里插入图片描述

通过地理信息查询坐标

参考地址 :链接地址

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <title>地理编码(地址->经纬度)</title>
    <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css"/>
    <style>
        html,body,#container{
            height:100%;
            width:100%;
        }
        .btn{
            width:10rem;
            margin-left:6.8rem;   
        }
    </style>
</head>
<body>
<div id="container"></div>
<div class="input-card" style='width:28rem;'>
    <label style='color:grey'>地理编码,根据地址获取经纬度坐标</label>
    <div class="input-item">
            <div class="input-item-prepend"><span class="input-item-text" >地址</span></div>
            <input id='address' type="text" value='北京市朝阳区阜荣街10号' >
    </div>
    <div class="input-item">
            <div class="input-item-prepend"><span class="input-item-text">经纬度</span></div>
            <input id='lnglat' disabled type="text">
    </div>
    <input id="geo" type="button" class="btn" value="地址 -> 经纬度" />
</div>
<script src="https://a.amap.com/jsapi_demos/static/demo-center/js/demoutils.js"></script>
<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=您申请的key值&plugin=AMap.Geocoder"></script>
<script type="text/javascript">
    var map = new AMap.Map("container", {
        resizeEnable: true
    });
    
    var geocoder = new AMap.Geocoder({
        city: "010", //城市设为北京,默认:“全国”
    });
    
    var marker = new AMap.Marker();
    
    function geoCode() {
        var address  = document.getElementById('address').value;
        geocoder.getLocation(address, function(status, result) {
            if (status === 'complete'&&result.geocodes.length) {
                var lnglat = result.geocodes[0].location
                document.getElementById('lnglat').value = lnglat;
                marker.setPosition(lnglat);
                map.add(marker);
                map.setFitView(marker);
            }else{
                log.error('根据地址查询位置失败');
            }
        });
    }
    document.getElementById("geo").onclick = geoCode;
    document.getElementById('address').onkeydown = function(e) {
        if (e.keyCode === 13) {
            geoCode();
            return false;
        }
        return true;
    };
</script>
</body>
</html>

Vue版本

<script>
 import { ref, reactive } from 'vue';
 import AMapLoader from '@amap/amap-jsapi-loader';
 import { debounce } from 'lodash-es';
 let BasicMap = null;
 let SelfMap = null;
 let geocoder = ref();
 //初始化地图
 function initMap() {
        AMapLoader.load({
             key: '*******************************', // 申请好的Web端开发者Key,首次调用 load 时必填
            version: '2.0', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
            plugins: ['AMap.Geocoder'], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
        })
            .then((AMap) => {
                SelfMap = AMap;
                //设置地图容器id
                BasicMap = new AMap.Map('container', {
                    viewMode: '3D', //是否为3D地图模式
                    zoom: 12, //初始化地图级别
                    // zooms: [4, 16.5],//地图层级范围
                    center: [105.602725, 37.076636], //初始化地图中心点位置
                    resizeEnable: true,
                });
                // 注册坐标转地址
                geocoder.value = new AMap.Geocoder({
                    city: '', //城市设为北京,默认:“全国”
                    radius: 1000, //范围,默认:500
                });
                if (position.lng) {
                    panTo(position);
                    addMarker(position);
                }
            })
            .catch((e) => {
                console.log(e);
            });
    }
    //输入框查询
    function onSearch(value) {
        //关键字查询
        if (value) {
            searchRes(value);
        }
    }
    const searchRes = debounce((value) => {
        placeSearch.value.search(value, (status, result) => {
            if (status === 'complete') {
                // 返回结果 list result.poiList.pois
            }
        });
    }, 500);
</script>

2.逆地理编码

通过坐标查询地理信息 在这里插入图片描述

参考地址:链接地址