<!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="http://cache.amap.com/lbs/static/main1119.css" />
<style type="text/css">
#panel {
position: fixed;
background-color: white;
max-height: 90%;
overflow-y: auto;
top: 10px;
right: 10px;
width: 280px;
}
</style>
<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.4.2&key=你的密钥&plugin=AMap.Walking"></script>
<script type="text/javascript" src="http://cache.amap.com/lbs/static/addToolbar.js"></script>
</head>
<body>
<div id="container"></div>
<div id="panel"></div>
<script type="text/javascript">
var markers = [];
function f_initMarkers() {
markers.push({ position: [116.474111, 39.911986] });
markers.push({ position: [116.466439, 39.844188] });
markers.push({ position: [116.460543, 39.822305] });
markers.push({ position: [116.412534, 39.821182] });
markers.push({ position: [116.423304, 39.779751] });
markers.forEach(function (marker) {
addMarker(marker.position[0], marker.position[1], "http://webapi.amap.com/theme/v1.3/markers/n/mark_b.png", true);
})
}
var startPoint = [116.399028, 39.845042, "http://webapi.amap.com/theme/v1.3/markers/n/mark_r.png"];
//地图初始化定义
var marker, map = new AMap.Map("container", {
resizeEnable: true,
center: [116.397428, 39.90923],//地图中心点
zoom: 13, //地图显示的缩放级别
});
//默认标记
addMarker(startPoint[0], startPoint[1], startPoint[2], true);
f_initMarkers();
//
map.on('click', function (e) {
console.log('您在[ ' + e.lnglat.getLng() + ',' + e.lnglat.getLat() + ' ]的位置点击了地图!');
addMarker(e.lnglat.getLng(), e.lnglat.getLat(), "http://webapi.amap.com/theme/v1.3/markers/n/mark_b.png", false);
});
// 实例化点标记
function addMarker(lat, lng, icon, isInit) {
marker = new AMap.Marker({
icon: icon,
position: [lat, lng]
});
marker.setMap(map);
if (!isInit) {
f_walking(lat, lng);
}
}
//步行导航
var walking = new AMap.Walking({
map: map,
panel: "panel",
});
function f_walking(lat, lng) {
//清除覆盖物
if (walking)
walking.clearMap;
walking.search([startPoint[0], startPoint[1]], [lat, lng], function (status, result) {
// f_initMarkers();
});
}
// setTimeout(function(){
// walking.search([116.399028, 39.845042], [116.396034,39.923271]);
// },2000)
</script>
</body>
</html>