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

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

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

3、创建应用拿到key

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

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

javascript 复制代码
<!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、成品效果

相关推荐
Jonathan Star36 分钟前
沉浸式雨天海岸:用A-Frame打造WebXR互动场景
前端·javascript
缺点内向40 分钟前
C#: 高效移动与删除Excel工作表
开发语言·c#·.net·excel
工业甲酰苯胺1 小时前
实现 json path 来评估函数式解析器的损耗
java·前端·json
老前端的功夫1 小时前
Web应用的永生之术:PWA落地与实践深度指南
java·开发语言·前端·javascript·css·node.js
LilySesy2 小时前
ABAP+WHERE字段长度不一致报错解决
java·前端·javascript·bug·sap·abap·alv
ᐇ9592 小时前
Java HashMap深度解析:数据结构、原理与实战指南
java·开发语言·数据结构
QT 小鲜肉3 小时前
【个人成长笔记】在 Linux 系统下撰写老化测试脚本以实现自动压测效果(亲测有效)
linux·开发语言·笔记·单片机·压力测试
Wang's Blog3 小时前
前端FAQ: Vue 3 与 Vue 2 相⽐有哪些重要的改进?
前端·javascript·vue.js
程序员龙一3 小时前
C++之static_cast关键字
开发语言·c++·static_cast
yue0083 小时前
C# 分部类读取学生信息
开发语言·c#