百度地图根据多个经纬度,绘制路线

直接上代码:

<pre name="code" class="html"><!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
        body, html {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
        #allmap {position:absolute;left:0px;width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
    </style>
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=E06eb9d756d0eafc722effb355657b4c"></script>
    <title>车辆运行轨迹测试</title>
    <script src="http://c.cnzz.com/core.php"></script></head>
<body>
<div id="allmap"></div>
</body>
</html>
<script type="text/javascript">

    var list = [{Long:106.652024,Lat:26.617221},{Long:106.652024,Lat:26.614221},{Long:106.654024,Lat:26.612221},{Long:106.657024,Lat:26.612221}];
    var listLast = list.length-1;
    // 百度地图API功能
    var map = new BMap.Map("allmap");    // 创建Map实例
    map.centerAndZoom(new BMap.Point(list[0].Long,list[0].Lat), 15);  // 初始化地图,设置中心点坐标和地图级别
    map.addControl(new BMap.MapTypeControl());   //添加地图类型控件

    map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放

    setTimeout(drawIcon,500);
    var carMk;
    var myBeginIcon = new BMap.Icon("http://e.hiphotos.baidu.com/image/pic/item/7e3e6709c93d70cfaf72bca2f0dcd100bba12b90.jpg", new BMap.Size(25,37), {imageOffset: new BMap.Size(0, 0)});//人
    var myEndIcon = new BMap.Icon("http://b.hiphotos.baidu.com/image/pic/item/b58f8c5494eef01f534c096ae8fe9925bd317dc0.jpg", new BMap.Size(25,37), {imageOffset: new BMap.Size(0, 0)});//人

    function drawGreenLine(i){
        var polyline = new BMap.Polyline([
            new BMap.Point(list[i].Long,list[i].Lat),//起始点的经纬度
            new BMap.Point(list[i+1].Long,list[i+1].Lat)//终点的经纬度
        ], {strokeColor:"green",//设置颜色
            strokeWeight:4, //宽度
            strokeOpacity:1});//透明度
        map.addOverlay(polyline);
    }

    function drawIcon(){
        if(carMk){
            map.removeOverlay(carMk);
        }
        carMk2 = new BMap.Marker(
                new BMap.Point(list[0].Long,list[0].Lat),//起始点的经纬度
                {icon:myBeginIcon});
        map.addOverlay(carMk2);

        carMk = new BMap.Marker(
                new BMap.Point(list[listLast].Long,list[listLast].Lat),//终点的经纬度
                {icon:myEndIcon});
        map.addOverlay(carMk);

        for(var i=0;i<list.length-1;i++){
            drawGreenLine(i);
        }

    }
</script>

 运行后结果 


  • 9
    点赞
  • 52
    收藏
    觉得还不错? 一键收藏
  • 17
    评论
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值