DeepSeek代码之旅1:卫星地图标记方法之——html语言的实现
zhezhongyun 2025-04-26 22:36 19 浏览
最近遇到一个任务,具体功能如下:
1、调用高德地图API,图层为卫星图层,根据需要标记兴趣点;
2、标记完成后可以保存兴趣点,便于下次加载历史兴趣点。
目前DeepSeek是非常的火爆啊,听说写代码还不错,那就赶紧把需求跟它说说吧,首先我们得告诉它使用什么语言去实现什么功能,我提问如下:
使用HTML语言,实现在线高德卫星地图api多点标记功能,具体实现如下:HTML界面中添加功能区、显示区。
1、功能区:靠左,文本框 1 显示“经度”、经度输入框 1(经度)(鼠标点击自动获取)、文本框 2 显示“纬度”、经度输入框 2(纬度)(鼠标点击自动获取)、标记点文本框显示“标记点名称”、标记点名称输入框,添加标记按钮、搜索标记按钮、清楚所有标记按钮、导出标记按钮、加载本地标记按钮;
2、显示区:靠右,显示地图
它直接给出了解决方案,并得到html代码示例:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>高德地图多点标记系统</title>
<style>
body { margin: 0; padding: 0; }
#container {
display: flex;
height: 100vh;
}
#control-panel {
width: 300px;
padding: 20px;
background: #f0f0f0;
overflow-y: auto;
}
#map-container {
flex: 1;
height: 100%;
}
.input-group { margin-bottom: 15px; }
input[type="text"] { width: 100%; padding: 5px; }
button { padding: 8px 15px; margin: 5px; cursor: pointer; }
</style>
</head>
<body>
<div id="container">
<!-- 功能区 -->
<div id="control-panel">
<div class="input-group">
<label>经度:</label>
<input type="text" id="longitude" placeholder="点击地图获取">
</div>
<div class="input-group">
<label>纬度:</label>
<input type="text" id="latitude" placeholder="点击地图获取">
</div>
<div class="input-group">
<label>标记名称:</label>
<input type="text" id="marker-name">
</div>
<button onclick="addMarker()">添加标记</button>
<button onclick="searchMarker()">搜索标记</button>
<button onclick="clearMarkers()">清除所有标记</button>
<button onclick="exportMarkers()">导出标记</button>
<button onclick="document.getElementById('file-input').click()">加载标记</button>
<input type="file" id="file-input" style="display: none;">
</div>
<!-- 地图显示区 -->
<div id="map-container"></div>
</div>
<!-- 高德地图API -->
<script src="https://webapi.amap.com/maps?v=2.0&key=您的高德地图Key"></script>
<script>
let map = null;
let markers = [];
// 初始化地图
function initMap() {
map = new AMap.Map('map-container', {
zoom: 12,
center: [116.397428, 39.90923],
layers: [new AMap.TileLayer.Satellite()]
});
// 地图点击事件
map.on('click', function(e) {
document.getElementById('longitude').value = e.lnglat.getLng();
document.getElementById('latitude').value = e.lnglat.getLat();
});
}
// 添加标记
function addMarker() {
const lng = parseFloat(document.getElementById('longitude').value);
const lat = parseFloat(document.getElementById('latitude').value);
const name = document.getElementById('marker-name').value;
if (isNaN(lng) || isNaN(lat)) return;
const marker = new AMap.Marker({
position: [lng, lat],
title: name,
map: map
});
markers.push({
name: name,
lng: lng,
lat: lat,
marker: marker
});
}
// 搜索标记
function searchMarker() {
const name = prompt("请输入要搜索的标记名称:");
const target = markers.find(m => m.name === name);
if (target) {
map.setCenter([target.lng, target.lat]);
map.setZoom(17);
}
}
// 清除所有标记
function clearMarkers() {
markers.forEach(m => m.marker.setMap(null));
markers = [];
}
// 导出标记
function exportMarkers() {
const data = JSON.stringify(markers.map(m => ({
name: m.name,
lng: m.lng,
lat: m.lat
})));
const blob = new Blob([data], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'markers.json';
a.click();
}
// 文件读取
document.getElementById('file-input').addEventListener('change', function(e) {
const file = e.target.files[0];
const reader = new FileReader();
reader.onload = function(event) {
const data = JSON.parse(event.target.result);
data.forEach(item => {
document.getElementById('longitude').value = item.lng;
document.getElementById('latitude').value = item.lat;
document.getElementById('marker-name').value = item.name;
addMarker();
});
};
reader.readAsText(file);
});
// 初始化
window.onload = initMap;
</script>
</body>
</html>
我们直接将代码复制到vscode中(没有可以安装)方便代码修改,直接浏览器打开肯定是异常的!浏览器打开如下:
要想正常使用地图功能,首先我们要去高德开放平台(https://lbs.amap.com/)申请权限,如下图中控制台中申请得到key和安全密钥(创建新应用—>添加key—>选择web端):
我们将得到的key替换掉html代码中的位置:
刷新后发现可以正常显示了,但是标记点无法显示,根据不断的调试和优化得到最终的html代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>地图标记点管理系统</title>
<script src="https://webapi.amap.com/loader.js"></script>
<style>
/* 页面布局 */
body {
display: flex;
margin: 0;
height: 100vh;
font-family: Arial, sans-serif;
}
/* 功能区样式 */
#function-area {
width: 300px;
padding: 20px;
background-color: #f4f4f4;
box-shadow: 2px 0 5px rgba(0,0,0,0.1);
overflow-y: auto;
}
/* 显示区样式 */
#display-area {
flex: 1;
position: relative;
}
/* 输入框和按钮样式 */
.input-group {
margin-bottom: 15px;
}
input[type="text"], button {
width: 100%;
padding: 8px;
margin: 5px 0;
border: 1px solid #ccc;
border-radius: 4px;
}
button {
background-color: #007bff;
color: white;
border: none;
cursor: pointer;
transition: background 0.3s;
}
button:hover {
background-color: #0056b3;
}
#coordinates {
position: absolute;
bottom: 20px;
left: 20px;
background: white;
padding: 10px;
border-radius: 4px;
box-shadow: 0 2px 6px rgba(0,0,0,0.1);
z-index: 999;
}
/* 新增文件上传样式 */
.file-input {
display: none;
}
.file-upload-label {
display: block;
text-align: center;
padding: 8px;
background: #28a745;
color: white;
border-radius: 4px;
cursor: pointer;
}
.file-upload-label:hover {
background: #218838;
}
</style>
</head>
<body>
<!-- 功能区 -->
<div id="function-area">
<div class="input-group">
<label>经度</label>
<input type="text" id="longitude" placeholder="点击地图获取或输入">
</div>
<div class="input-group">
<label>纬度</label>
<input type="text" id="latitude" placeholder="点击地图获取或输入">
</div>
<div class="input-group">
<label>标记名称</label>
<input type="text" id="marker-name" placeholder="输入标记点名称">
</div>
<button id="add-marker">添加标记</button>
<button id="search-marker">搜索标记</button>
<button id="clear-markers">清除所有标记</button>
<button id="export-data">导出数据</button>
<!-- 新增文件上传组件 -->
<input type="file" id="file-input" class="file-input" accept=".json">
<label for="file-input" class="file-upload-label">导入JSON文件</label>
</div>
<!-- 地图显示区 -->
<div id="display-area">
<div id="mapContainer" style="width:100%;height:100%;"></div>
<div id="coordinates">点击地图获取坐标:未记录</div>
</div>
<script>
// 高德地图初始化
let map;
let markers = [];
const AMapKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // 替换为真实Key
// 页面元素
const longitudeInput = document.getElementById('longitude');
const latitudeInput = document.getElementById('latitude');
const markerNameInput = document.getElementById('marker-name');
// 初始化地图
window.onload = function() {
AMapLoader.load({
key: AMapKey,
version: "2.0",
plugins: ['AMap.Scale', 'AMap.ToolBar']
}).then((AMap) => {
map = new AMap.Map('mapContainer', {
layers: [new AMap.TileLayer.Satellite()],
zoom: 13,
center: [108.378925, 22.842527]
});
// 添加控件
map.addControl(new AMap.Scale());
map.addControl(new AMap.ToolBar());
// 地图点击事件
map.on('click', function(e) {
const lng = e.lnglat.getLng().toFixed(6);
const lat = e.lnglat.getLat().toFixed(6);
longitudeInput.value = lng;
latitudeInput.value = lat;
document.getElementById('coordinates').innerHTML =
`点击坐标:经度 ${lng}, 纬度 ${lat}`;
});
});
};
// 添加标记函数
function addMarker(lng, lat, title) {
if (!map) return;
// 获取当前页面的URL
var currentURL = window.location.href;
// 提取目录路径
var directoryPath = currentURL.substring(0, currentURL.lastIndexOf('/') + 1);
// 将目录路径显示在页面上
document.getElementById('coordinates').textContent = "当前目录: " + directoryPath;
const marker = new AMap.Marker({
position: [parseFloat(lng), parseFloat(lat)],
map: map,
title: title,
icon: directoryPath + "towerdot.png"
});
// 信息窗口
marker.on('click', () => {
new AMap.InfoWindow({
content: `<strong>${title}</strong><br>经纬度:${lng},${lat}`
}).open(map, marker.getPosition());
});
markers.push(marker);
}
// NEW: 添加文件导入功能
document.getElementById('file-input').addEventListener('change', function(e) {
const file = e.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = function(event) {
try {
const importedData = JSON.parse(event.target.result);
// 验证数据格式
if (!Array.isArray(importedData)) {
throw new Error("无效的JSON格式");
}
importedData.forEach(marker => {
if (!marker.lng || !marker.lat || !marker.title) {
throw new Error("缺少必要字段: lng/lat/title");
}
addMarker(marker.lng, marker.lat, marker.title);
});
} catch (error) {
alert(`导入失败: ${error.message}`);
}
};
reader.readAsText(file);
});
// 事件监听
document.getElementById('add-marker').addEventListener('click', () => {
const lng = longitudeInput.value;
const lat = latitudeInput.value;
const title = markerNameInput.value;
if (!lng || !lat) return alert("请先获取或输入坐标!");
if (!title) return alert("请输入标记名称!");
addMarker(lng, lat, title);
markerNameInput.value = ''; // 清空名称输入
});
document.getElementById('search-marker').addEventListener('click', () => {
const keyword = prompt("请输入搜索关键词:");
if (!keyword) return;
const found = markers.find(marker =>
marker.getTitle().includes(keyword)
);
if (found) {
map.setCenter(found.getPosition());
map.setZoom(16);
found.emit('click'); // 触发点击显示信息窗口
} else {
alert("未找到匹配标记!");
}
});
document.getElementById('clear-markers').addEventListener('click', () => {
markers.forEach(marker => marker.setMap(null));
markers = [];
alert("已清除所有标记!");
});
document.getElementById('export-data').addEventListener('click', () => {
const data = markers.map(marker => ({
lng: marker.getPosition().lng,
lat: marker.getPosition().lat,
title: marker.getTitle()
}));
const blob = new Blob([JSON.stringify(data)], {type: 'application/json'});
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'markers.json';
a.click();
});
</script>
</body>
</html>
复制上面代码保存为html文件,并将上面代码中AMapKey替换为自己的key后可以得到如下界面:
最终实现:自定义标记点图标(图标文件需与html文件同一目录下),添加标记功能、搜索标记功能、清楚标记功能、导出标记功能、加载标记功能。
相关推荐
- Win10全新版本速览 全新图标设计焕然一新
-
来源:太平洋电脑网[PConline资讯]原定于今年下半年发布的Win1021H2(SunValley),再有几个月就要与我们见面了。之前我们已经陆续介绍过新版在开始菜单、窗口动效、通知中心等方...
- 第四周B组最佳选手Icon:陨落天团的小将,还是重生星球的偶像?
-
B组本来被认为是死亡之组,但在上周的异组对抗赛中,实力较强的几只队伍却先后被A组击沉,反而是之前一分难求的OMG2:1战胜了IG,其中Icon功不可没,两次秀翻全场的妖姬和与Juejue中野联动的实力...
- 系统小技巧:不花一分钱 让声音再大些
-
有时,笔记本扬声器的音量可能无法满足我们对声音播放的需求。点击系统托盘的小喇叭图标,音量调节滑动条调整到最大也无效,而添置大功率扬声器又不是我们所愿。这时,可通过调整系统本身的设置或利用第三方软件来解...
- DNF手游:70级毕业搭配曝光!需集齐四类装备,别指望无形升级了
-
随着dnf手游7.16版本的临近,各种70级装备的消息满天飞,真真假假难以辨别,但只要以“发布会爆料”为核心,主播十四的爆料为辅,就能确定70版本的毕业标准搭配,查理策划是打算“集百家之长”于一身,完...
- 学会这5个电脑设置 可解决99%的故障
-
现阶段复工大家都是宅家办公吧,所以电脑可谓是大救星,因为无论是“停课不停学”的网课,还是在家办公的穷社畜,都离不开电脑。所以今天小编就来教大家几招电脑的自救方法,让你可以在电脑出问题时从容面对,足不出...
- LOL:上单腕豪的取胜之道——掌控自身优势,融合装备特效
-
01前言腕豪这个英雄,在11.9版本之前都没有太多的出场机会,即使出现也大多是在辅助位置上,充当开团、先手控制的角色。但是在11.9版本以后,这个英雄突然之间仿佛飞升了一般,直接冲上T1级别上单,之前...
- 英雄联盟手游:一篇文章读懂所有龙buff属性效果,还不赶紧收藏
-
英雄联盟手游采用也是经典峡谷地图,看起来和端游并没有太大差异,但实际上为了适应手游节奏,官方也是进行了适当调整,所以包括红蓝buff,大龙小龙buff效果会有一定的差异,现在就来看看小龙和大龙具体属性...
- 双击打不开怎么办?双击文件夹显示属性的解决办法
-
今天小编在双击文件夹的时候,就是打不开,还弹出“属性”对话框,不知道大家有没有出现过,为避免出现这种情况时大家束手无策,今天小编就来为大家分享一下解决方法。1.检查键盘,看看“Alt”键是否卡住。由于...
- 柜子布局好超省空间,1㎡当10㎡用,收纳涨5倍!户型图直接抄
-
柜子不嫌多,就怕你家放不下!▼每个空间几乎都需要柜子客厅要有电视柜,卧室要能放下全家四季的衣柜,厨房还需要大容量的橱柜空间就这么小,想要榨出放柜子的地方,还要活得不拥挤简直太难了!其实全是你找错地方打...
- 制作幻灯片的另类方法(然后制作幻灯片)
-
如果提到制作幻灯片,很多人都会想到PowerPoint。PowerPoint提供了很多模板,但这些模板实用性并不太高;PowerPoint的交互还算比较方便,但不可否认,交互显得有些呆板,而且要想修改...
- 《心灵杀手重制版》画面晃动怎么办?画面晃动解决办法
-
针对《心灵杀手重制版》画面晃动的问题,以下是一些解决办法:修改游戏启动路径:打开游戏所在的文件夹,找到游戏图标的属性并打开。在属性中修改启动路径,加入“-noblur”参数。例如,如果游戏安装在“X:...
- win10网络图标是小地球怎么办(excel表格怎样选定区域打印)
-
win10系统右下角的网络图标突然变成地球图标,而且无法上网,提示无法连接到Internet。出现这个问题多数是因为win10正式版更新了或者是网络中断后遗症。那么win10网络图标变成地球怎么办呢?...
- 重看电脑任务栏的使用方法:windows 10小贴士
-
【环球科技综合报道】据日本Livedoor新闻网2月18日报道,windows电脑的屏幕下方配置的任务栏由于总是在电脑桌面上显示,可以说是一个映入眼帘的机会格外多的区域。像是把经常使用的软件图标设置在...
- 《地狱之门》攻略:四大元素属性介绍
-
《地狱之门》是近期比较看好的一款卡牌手游,绚丽的3D游戏画面以及创新的即时战斗系统给人耳目一新的感觉,非常值得一试。玩家不仅可以体验到卡牌收集的乐趣,更主要的是能体验到酣畅淋漓的战斗体验。下面小编就来...
- 一脸懵?DNF希洛克词条赋予属性触发条件解析
-
DNF希洛克已经开放两天,但是还是看到不少小伙伴对于希洛克词条赋予系统充满疑惑。今天就来简单易懂的给大家科普一下希洛克词条赋予系统里的弯弯绕绕。◎希洛克装备融合在希洛克获取了专属史诗之后,即可通过歌兰...
- 一周热门
- 最近发表
- 标签列表
-
- HTML 教程 (33)
- HTML 简介 (35)
- HTML 实例/测验 (32)
- HTML 测验 (32)
- JavaScript 和 HTML DOM 参考手册 (32)
- HTML 拓展阅读 (30)
- HTML常用标签 (29)
- HTML文本框样式 (31)
- HTML滚动条样式 (34)
- HTML5 浏览器支持 (33)
- HTML5 新元素 (33)
- HTML5 WebSocket (30)
- HTML5 代码规范 (32)
- HTML5 标签 (717)
- HTML5 标签 (已废弃) (75)
- HTML5电子书 (32)
- HTML5开发工具 (34)
- HTML5小游戏源码 (34)
- HTML5模板下载 (30)
- HTTP 状态消息 (33)
- HTTP 方法:GET 对比 POST (33)
- 键盘快捷键 (35)
- 标签 (226)
- HTML button formtarget 属性 (30)
- CSS 水平对齐 (Horizontal Align) (30)