sample_code/amap_route_planning.html
2025-03-07 00:24:33 +08:00

182 lines
6.7 KiB
HTML

<!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> // 手动初始化定位版本
<style type="text/css">
html,
body,
#container {
width: 100%;
height: 100%;
}
#panel {
position: fixed;
background-color: white;
max-height: 90%;
overflow-y: auto;
top: 10px;
right: 10px;
width: 280px;
}
#panel .amap-call {
background-color: #009cf9;
border-top-left-radius: 4px;
border-top-right-radius: 4px;
}
#panel .amap-lib-driving {
border-bottom-left-radius: 4px;
border-bottom-right-radius: 4px;
overflow: hidden;
}
#input-container {
position: fixed;
top: 10px;
left: 10px;
background-color: white;
padding: 10px;
border-radius: 4px;
box-shadow: 0 2px 6px rgba(0,0,0,0.3);
z-index: 1000;
}
#input-container input {
margin-bottom: 10px;
padding: 5px;
width: 200px;
display: block; /* 每个输入框单独一行 */
}
#input-container button {
padding: 5px 10px;
background-color: #009cf9;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
width: 100%; /* 按钮宽度与输入框一致 */
}
</style>
<link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css" />
<script src="https://a.amap.com/jsapi_demos/static/demo-center/js/demoutils.js"></script>
<script type="text/javascript" src="https://cache.amap.com/lbs/static/addToolbar.js"></script>
</head>
<body>
<div id="container"></div>
<div id="input-container">
<input type="text" id="start-input" placeholder="输入起点">
<input type="text" id="end-input" placeholder="输入终点">
<button onclick="planRoute()">规划路线</button>
</div>
<div id="panel"></div>
<script type="text/javascript">
window._AMapSecurityConfig = {
securityJsCode: "安全秘钥",
};
</script>
<script type="text/javascript" src="https://webapi.amap.com/maps?v=2.0&key=高德KEY&plugin=AMap.Driving,AMap.AutoComplete,AMap.PlaceSearch"></script>
<script type="text/javascript">
//基本地图加载
var map = new AMap.Map("container", {
resizeEnable: true,
center: [114.202549, 22.66327], // 初始地图中心点(北京)
zoom: 15 //地图显示的缩放级别
});
//构造路线导航类
var driving = new AMap.Driving({
map: map,
panel: "panel"
});
// 存储起点和终点的城市信息
var startCity, endCity;
// 初始化起点和终点的自动补全
AMap.plugin(['AMap.AutoComplete', 'AMap.PlaceSearch'], function() {
var placeSearch = new AMap.PlaceSearch({
map: map
});
// 起点自动补全
var startAuto = new AMap.AutoComplete({
input: "start-input"
});
// 监听起点选择事件
startAuto.on("select", function(e) {
if (e.poi && e.poi.city) {
startCity = e.poi.city; // 获取起点的城市
console.log("获取到的起点城市信息:", startCity); // 输出城市信息
// 根据起点设置地图中心点
placeSearch.search(e.poi.name, function(status, result) {
if (status === 'complete' && result.poiList.pois.length > 0) {
var location = result.poiList.pois[0].location;
console.log("获取到的起点经纬度:", location); // 输出经纬度信息
map.setCenter([location.lng, location.lat]); // 设置地图中心点
} else {
console.error("获取起点经纬度失败,状态:", status, "结果:", result); // 输出错误信息
}
});
} else {
startCity = ''; // 如果未获取到城市信息,设置为空
console.warn("未获取到起点城市信息,使用默认城市"); // 输出警告信息
}
});
// 终点自动补全
var endAuto = new AMap.AutoComplete({
input: "end-input"
});
// 监听终点选择事件
endAuto.on("select", function(e) {
if (e.poi && e.poi.city) {
endCity = e.poi.city; // 获取终点的城市
console.log("获取到的终点城市信息:", endCity); // 输出城市信息
// 根据终点设置地图中心点
placeSearch.search(e.poi.name, function(status, result) {
if (status === 'complete' && result.poiList.pois.length > 0) {
var location = result.poiList.pois[0].location;
console.log("获取到的终点经纬度:", location); // 输出经纬度信息
map.setCenter([location.lng, location.lat]); // 设置地图中心点
} else {
console.error("获取终点经纬度失败,状态:", status, "结果:", result); // 输出错误信息
}
});
} else {
endCity = ''; // 如果未获取到城市信息,设置为空
console.warn("未获取到终点城市信息,使用默认城市"); // 输出警告信息
}
});
});
// 规划路线函数
function planRoute() {
var start = document.getElementById('start-input').value;
var end = document.getElementById('end-input').value;
if (!start || !end) {
alert('请输入起点和终点');
return;
}
// 根据起终点名称规划驾车导航路线
driving.search([
{keyword: start, city: startCity || '全国'}, // 使用起点的城市,如果未获取到则默认全国
{keyword: end, city: endCity || '全国'} // 使用终点的城市,如果未获取到则默认全国
], function(status, result) {
// result 即是对应的驾车导航信息,相关数据结构文档请参考 https://lbs.amap.com/api/javascript-api/reference/route-search#m_DrivingResult
if (status === 'complete') {
log.success('绘制驾车路线完成');
console.log(result); // 输出路线规划结果
} else {
log.error('获取驾车数据失败:' + result);
}
});
}
</script>
</body>
</html>