web端使用高德地图逆地理编码

1、首先去地理/逆地理编码-基础 API 文档-开发指南-Web服务 API | 高德地图API注册一下

2、点击产品介绍-------地理/逆地理编码 

 3、创建应用拿到key

 创建web服务、看底下有逆地理编码服务

4、上一步就能拿到key了最后一步复制底下代码即可

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>高德逆地理编码示例</title>
    <script src="https://webapi.amap.com/maps?v=1.4.15&key=你申请的key"></script> 
    <style>
        /* 设置地图容器的大小 */
        #map {
            width: 100%;
            height: 800px;
        }
    </style>
</head>
<body>
    <h1>通过经纬度查找位置</h1>
    <label for="longitude">经度:</label>
    <input type="text" id="longitude" placeholder="请输入经度">
    <label for="latitude">纬度:</label>
    <input type="text" id="latitude" placeholder="请输入纬度">
    <button id="getAddress">查找位置</button>
    <div id="output"></div>
    <div id="map"></div>

    <script>
        // 初始化地图
        const map = new AMap.Map('map', {
            zoom: 10, // 设置地图缩放级别
            center: [108.999, 34.2449] // 设置地图中心点,
        });

        document.getElementById('getAddress').addEventListener('click', function() {
            const longitude = document.getElementById('longitude').value;
            const latitude = document.getElementById('latitude').value;

            if (!longitude || !latitude) {
                document.getElementById('output').innerText = '请输入有效的经纬度。';
                return;
            }

            // 构建 API 请求 URL
            const key = ''; // 替换为你的高德API密钥
            const location = `${longitude},${latitude}`;
            const apiUrl = `https://restapi.amap.com/v3/geocode/regeo?key=${key}&location=${location}&extensions=all&output=JSON&radius=10`;

            // 发送 API 请求
            fetch(apiUrl)
                .then(response => response.json())
                .then(data => {
                    console.log(data); // 打印返回的数据用于调试
                    if (data.status === '1' && data.regeocode) {
                        const address = data.regeocode.formatted_address; // 获取地址
                        document.getElementById('output').innerText = `具体位置: ${address}`;
                        
                        // 在地图上标记该位置
                        const lngLat = new AMap.LngLat(longitude, latitude);
                        const marker = new AMap.Marker({
                            position: lngLat,
                            title: address // 标记的标题
                        });
                        marker.setMap(map);
                        map.setCenter(lngLat); // 将地图中心移动到标记位置
                        map.setZoom(15); // 缩放级别
                    } else {
                        document.getElementById('output').innerText = '获取具体位置失败。错误信息: ' + data.info;
                    }
                })
                .catch(error => {
                    console.error('错误:', error);
                    document.getElementById('output').innerText = '请求出错,请重试。';
                });
        });
    </script>
</body>
</html>

 记得两处把你得key换了ok了

5、成品效果 

Vue 高德地图地理编码可以使用 AMap JavaScript API 实现。以下是一个简单的示例: 首先需要在 `index.html` 中引入高德地图 JavaScript API: ```html <script src="https://webapi.amap.com/maps?v=1.4.15&key=your-amap-key"></script> ``` 然后在 Vue 组件中,可以使用以下代码获取当前位置的地理编码信息: ```vue <template> <div> <div ref="map" style="width: 100%; height: 400px;"></div> <div>{{ address }}</div> </div> </template> <script> export default { data() { return { address: "" }; }, mounted() { // 创建地图 const map = new AMap.Map(this.$refs.map, { zoom: 16 }); // 获取当前位置 map.plugin("AMap.Geolocation", () => { const geolocation = new AMap.Geolocation({ enableHighAccuracy: true, // 是否使用高精度定位,默认为 false timeout: 10000, // 超过 10 秒后停止定位,默认值为 无穷大 maximumAge: 0, // 定位结果缓存时间,默认值为 0 convert: true, // 自动偏移坐标,偏移后的坐标为高德坐标,默认为 true showButton: true, // 显示定位按钮,默认为 true buttonPosition: "LB", // 定位按钮的位置,默认为 'LB',左下角 buttonOffset: new AMap.Pixel(10, 10), // 定位按钮距离容器左下角的偏移量,默认为 Pixel(10, 20) showMarker: true, // 定位成功后在定位到的位置显示点标记,默认为 true showCircle: true, // 定位成功后用圆圈表示定位精度范围,默认为 true panToLocation: true, // 定位成功后将定位到的位置作为地图中心点,默认为 true zoomToAccuracy: true // 定位成功后自动调整地图缩放级别以适合定位结果的范围,默认为 true }); geolocation.getCurrentPosition((status, result) => { if (status === "complete") { this.getAddress(result.address); } else { console.log("获取当前位置信息失败"); } }); }); }, methods: { // 地理编码 getAddress(address) { this.address = address; } } }; </script> ``` 在 `mounted` 钩子函数中,我们使用 `AMap.Geolocation` 插件获取当前位置信息。然后在 `getCurrentPosition` 回调函数中,我们通过 `result.address` 获取当前位置的地理编码信息,并将其赋值给 `this.address`,以在页面中显示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值