获取高德地图key

页面中引入所需文件,根据提供的api引入相应文件(本文主要是web端的路线规划问题)
<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.4.4&key=申请的key值&plugin=AMap.Driving"></script>
页面部分准备一个容器,用来放地图
<div id="container"></div>
绘制出基本地图
var map = new AMap.Map("container", {
resizeEnable: true,
zoom: 10
});
绘制路线导航
var driving = new AMap.Driving({
map: map,
showTraffic: false,
autoFitView: true
});
var path = {
waypoints: []
};
path.waypoints.push([途径点经度, 途径点维度]);
driving.search(
new AMap.LngLat(起点经度, 起点纬度),
new AMap.LngLat(终点经度, 终点纬度),
path);
标记途径点的信息
var markers = [{
icon: "http://webapi.amap.com/theme/v1.3/markers/n/mid.png",
position: [途径点经度, 途径点纬度],
title: '标题',
content: '内容'
}];
markers.forEach(function(marker) {
var marobj = new AMap.Marker({
map: map,
icon: marker.icon,
position: [marker.position[0], marker.position[1]],
title: marker.title,
zIndex: 101,
label: {
offset: new AMap.Pixel(20, 0),
content: marker.content
}
});
AMap.event.addListener(marobj, "click", _markerClick);
});
var position,title;
var _markerClick = function(obj) {
position = obj.target.getPosition();
title = obj.target.getTitle();
console.log(position,title);
navStation(postion,title);
};
地图导航
function navStation(position,title){
var geolocation ;
map.plugin('AMap.Geolocation', function() {
geolocation = new AMap.Geolocation({
enableHighAccuracy: true,
timeout: 10000,
maximumAge: 5000,
showButton: false
});
geolocation.getCurrentPosition();
AMap.event.addListener(geolocation, 'complete', function(data) {
window.location.href = "http://m.amap.com/navi/?start=" + data.position.getLng() + "," + data.position.getLat() + "&dest=" + position.lng + "," + position.lat + "&destName=" + title + "&naviBy=walk&key=申请的key值";
});
AMap.event.addListener(geolocation, 'error', function() {
var str = '定位失败:';
switch (data.info) {
case 'PERMISSION_DENIED':
str += '浏览器阻止了定位操作';
break;
case 'POSITION_UNAVAILBLE':
str += '无法获得当前位置';
break;
case 'TIMEOUT':
str += '定位超时';
break;
default:
str += '未知错误';
break;
}
console.log(str)
return false;
});
});
}