javascript的问题,我想实现调用百度地图API循环取出每两个城市间距离的程序,谢谢大家!
2个回答
展开全部
首先引入地图API:
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=cPoCAOgOKfIfWeAv9ku3kWfg"></script>
CSS
body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;}
HTML代码:
<div id="allmap"></div>
Javascript代码如下:
var map = new BMap.Map("allmap");
map.centerAndZoom(new BMap.Point(116.404, 39.915), 12);
var output = "";
var searchComplete = function (results)
{
if (transit.getStatus() != BMAP_STATUS_SUCCESS)
{
return ;
}
var plan = results.getPlan(0);
output += "从"+cities[m]+"到"+cities[n]+"需要"
output += plan.getDuration(true) + "\n"; //获取时间
output += "总路程为:" ;
output += plan.getDistance(true) + "\n\n"; //获取距离
};
var cities = ['深圳市','广州市','南京市','兰州市'];
var m=0;
var n=1;
var len = cities.length;
var transit = new BMap.DrivingRoute(map, {
renderOptions: {map: map},
onSearchComplete: searchComplete,
onPolylinesSet: function()
{
if(n<len-1){
n = n+1;
}else{
m = m+1;
n = m+1;
if(n==len){
alert(output);
return;
}
}
setTimeout(function(){
transit.search(cities[m],cities[n]);
},"1000");
}
});
transit.search(cities[m],cities[n] );
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询