高德地图JavaScript API-路径规划
——基于amap-js_V1.3
2015年7月31日
注意:高德将一些服务作为服务插件集成到当前map中(搞的越来越复杂了……)。
详见:http://lbs.amap.com/api/javascript-api/reference/search_plugin/#m_AMap.Driving
(这URL也不一定什么时候就会变,早上的内容和下午都不一样,说不准……)
function driving(){
AMap.service(["AMap.Driving"],function(){
vardrive=new AMap.Driving();
var ptStart=new AMap.LngLat(116.379018,39.865026);
var ptEnd=new AMap.LngLat(116.321139, 39.896028);
drive.search(ptStart,ptEnd,driveRouteResult);
});
}
<!DOCTYPE HTML>
<html>
<head>
<metaname="viewport" content="initial-scale=1.0,user-scalable=no" >
<metahttp-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Hello,world</title>
<styletype="text/css">
html { height:100% }
body { height:100%; margin:0px; padding:0px }
#container { height:100% }
</style>
<scripttype="text/javascript"src="amap.js"></script>
<scripttype="text/javascript"src="jquery-1.11.1.min.js"></script>
<scripttype="text/javascript">
var mapObj=null;
$(document).ready(function(){
mapObj = new AMap.Map("container"); // 创建地图实例
var point = new AMap.LngLat(116.404, 39.915); // 创建点坐标,这是个火星坐标,真实坐标对应的火星坐标为116.410251,39.916412
mapObj.setZoomAndCenter(11,point);
//test
driving();
});
//test driving
function driving(){
AMap.service(["AMap.Driving"],function(){
var drive=newAMap.Driving();
var ptStart=newAMap.LngLat(116.379018,39.865026);
var ptEnd=newAMap.LngLat(116.321139, 39.896028);
drive.search(ptStart,ptEnd,driveRouteResult);
});
}
functiondriveRouteResult(status,result){
if(status === 'complete' && result.info === 'OK'){
console.debug("route ok");
showRoutes(result.routes);
}else{
console.debug("error="+result);
}
}
function showRoutes(routes){
$(routes).each(function(index,route){
var arrayRoute=new Array();//all points
console.debug("route"+index+"="+route.steps);
$(route.steps).each(function(index,step){
console.debug("step"+index+"="+step.instruction+",distance="+step.distance+",path="+step.path);
drawPolyline(mapObj,step.path);
arrayRoute=arrayRoute.concat(step.path);
});
var car=addMarker(mapObj);
console.debug("all pts="+arrayRoute);
car.moveAlong(arrayRoute,500,null,true);//animation
mapObj.setFitView();
});
}
function addMarker(mapObj){
var car=new AMap.Marker({
id:"test01",
position:new AMap.LngLat(116.397428,39.90923),
icon:"images/car.png",
autoRotation:true,
map:mapObj
});
return car;
}
functiondrawPolyline(mapObj,arrayLine){
var polyline=new AMap.Polyline({
map:mapObj,
path:arrayLine,
strokeColor:"#3366FF", //线颜色
strokeOpacity:1, //线透明度
strokeWeight:5, //线宽
strokeStyle:"solid", //线样式
strokeDasharray:[10,5] //补充线样式
});
return polyline;
}
</script>
</head>
<body>
<div id="container"></div>
</body>
</html>
方式与驾车相似,但是多了公交换乘和步行的路径。
<!DOCTYPE HTML>
<html>
<head>
<metaname="viewport" content="initial-scale=1.0,user-scalable=no" >
<metahttp-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Hello,world</title>
<styletype="text/css">
html { height:100% }
body { height:100%; margin:0px; padding:0px }
#container { height:100% }
</style>
<scripttype="text/javascript"src="amap.js"></script>
<scripttype="text/javascript"src="jquery-1.11.1.min.js"></script>
<scripttype="text/javascript">
var mapObj=null;
$(document).ready(function(){
mapObj = new AMap.Map("container"); // 创建地图实例
var point = new AMap.LngLat(116.404, 39.915); // 创建点坐标,这是个火星坐标,真实坐标对应的火星坐标为116.410251,39.916412
mapObj.setZoomAndCenter(11,point);
//test
driving();
});
//test driving
function driving(){
AMap.service(["AMap.Transfer"],function(){
var drive=new AMap.Transfer({
city: '010',
policy: AMap.TransferPolicy.LEAST_TIME
});
var ptStart=new AMap.LngLat(116.379028,39.865042);
var ptEnd=new AMap.LngLat(116.427281,39.903719);
drive.search(ptStart,ptEnd,routeResult);
});
}
functionrouteResult(status,result){
if(status === 'complete' && result.info === 'OK'){
console.debug("route ok,start="+result.start+",end="+result.end+",taxi_cost="+result.taxi_cost);
console.debug(result);
showRoutes(result.plans);
}else{
console.debug("error="+result);
}
}
function showRoutes(plans){
$(plans).each(function(index,route){
console.debug("route"+index+"="+route.path);
//show route
drawPolyline(mapObj,route.path);
var car=addMarker(mapObj);
console.debug("all pts="+route.path);
car.moveAlong(route.path,500,null,true);//animation
mapObj.setFitView();
return false;
});
}
function addMarker(mapObj){
var car=new AMap.Marker({
id:"test01",
position:new AMap.LngLat(116.397428,39.90923),
icon:"images/car.png",
autoRotation:true,
map:mapObj
});
return car;
}
functiondrawPolyline(mapObj,arrayLine){
var polyline=new AMap.Polyline({
map:mapObj,
path:arrayLine,
strokeColor:"#3366FF", //线颜色
strokeOpacity:1, //线透明度
strokeWeight:5, //线宽
strokeStyle:"solid", //线样式
strokeDasharray:[10,5] //补充线样式
});
return polyline;
}
</script>
</head>
<body>
<div id="container"></div>
</body>
</html>
方式与驾车相似,但是这里的返回结果中成功时是小写的ok(其它的都是大写……高德就是有国际范,大小写不同哦,SB)。
<!DOCTYPE HTML>
<html>
<head>
<metaname="viewport" content="initial-scale=1.0,user-scalable=no" >
<metahttp-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Hello,world</title>
<styletype="text/css">
html { height:100% }
body { height:100%; margin:0px; padding:0px }
#container { height:100% }
</style>
<scripttype="text/javascript"src="amap.js"></script>
<scripttype="text/javascript"src="jquery-1.11.1.min.js"></script>
<scripttype="text/javascript">
var mapObj=null;
$(document).ready(function(){
mapObj = new AMap.Map("container"); // 创建地图实例
var point = new AMap.LngLat(116.404, 39.915); // 创建点坐标,这是个火星坐标,真实坐标对应的火星坐标为116.410251,39.916412
mapObj.setZoomAndCenter(11,point);
//test
driving();
});
//test driving
function driving(){
AMap.service(["AMap.Walking"],function(){
var drive=new AMap.Walking();
var ptStart=new AMap.LngLat(116.480355,39.989783);
var ptEnd=new AMap.LngLat(116.469766,39.998731);
drive.search(ptStart,ptEnd,routeResult);
});
}
functionrouteResult(status,result){
if(status === 'complete' && result.info.toUpperCase()=== 'OK'){
console.debug("route ok");
console.debug(result);
showRoutes(result.routes);
}else{
console.debug("error="+result);
}
}
function showRoutes(routes){
$(routes).each(function(index,route){
var arrayRoute=new Array();//all points
console.debug("route"+index+"="+route.steps);
$(route.steps).each(function(index,step){
console.debug("step"+index+"="+step.instruction+",distance="+step.distance+",path="+step.path);
drawPolyline(mapObj,step.path);
arrayRoute=arrayRoute.concat(step.path);
});
var car=addMarker(mapObj);
console.debug("all pts="+arrayRoute);
car.moveAlong(arrayRoute,500,null,true);//animation
mapObj.setFitView();
});
}
function addMarker(mapObj){
var car=new AMap.Marker({
id:"test01",
position:new AMap.LngLat(116.397428,39.90923),
icon:"images/car.png",
autoRotation:true,
map:mapObj
});
return car;
}
functiondrawPolyline(mapObj,arrayLine){
var polyline=new AMap.Polyline({
map:mapObj,
path:arrayLine,
strokeColor:"#3366FF", //线颜色
strokeOpacity:1, //线透明度
strokeWeight:5, //线宽
strokeStyle:"solid", //线样式
strokeDasharray:[10,5] //补充线样式
});
return polyline;
}
</script>
</head>
<body>
<div id="container"></div>
</body>
</html>