登录  /  注册
博主信息
博文 15
粉丝 0
评论 0
访问量 27101
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
谷歌地图多点规划路线
陈序员的博客
原创
1183人浏览过

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
   <title>谷歌地图 v3</title>
   <script src="https://ditu.google.cn/maps/api/js?sensor=true&v=3&language=zh-CN&key=YOU KEY" type="text/javascript"></script>

   <script type="text/javascript">
       var map;// 地图对象
       var directionsService = new google.maps.DirectionsService();
       var directionDisplay;
       var steps = new Array(), curr_step = null, timer = 0, index = 0, marker = null;
       var elapsed = 0;
       var pathlength = 0;
       var win = new google.maps.InfoWindow();
       function init() {
           directionsDisplay = new google.maps.DirectionsRenderer();
           var coor = new google.maps.LatLng(39.960872,116.271486);
           map = new google.maps.Map(document.getElementById("map"), { zoom: 12, center: coor, mapTypeControl: false, mapTypeId: google.maps.MapTypeId.ROADMAP });

           directionsDisplay.setMap(map);

           var request = {
               origin: "39.956728,116.275949",
               destination: "39.959392,116.299424",
               waypoints: [
                   {location:"39.961497,116.284018",stopover:true},
                   {location:"39.966464,116.283417",stopover:true},
                   {location:"39.966925,116.28861",stopover:true},
                   {location:"39.966464,116.283417",stopover:true},
                   {location:"39.961497,116.284018",stopover:false}

               ],
               optimizeWaypoints: true,
               travelMode: google.maps.DirectionsTravelMode.DRIVING
           };

           // 获取从“广州市火车站”到“广州市番禺区汉溪长隆”的线路
           directionsService.route(request, function(response, status) {
               if (status == google.maps.DirectionsStatus.OK) {
                   directionsDisplay.setDirections(response);

                   // 第一种显示里程和时间的框
                   // var legs = response.routes[0].legs;
                   // if (legs) {
                   //     for (var i = 0; i < legs.length; i++) {
                   //         var ss = legs[i].steps;
                   //         for (var j = 0; j < ss.length; j++) {
                   //             steps.push(ss[j]);
                   //         }
                   //     }
                   // }
                   // curr_step = steps.shift();
                   // timer = window.setInterval(function() {
                   //     if (index >= curr_step.path.length) {
                   //         if (steps.length == 0) {
                   //             clearInterval(timer);
                   //             return;
                   //         }
                   //         index = 0;
                   //         curr_step = steps.shift();
                   //         elapsed += curr_step.duration.value;
                   //         pathlength += curr_step.distance.value;
                   //         win.setContent("里程:" + pathlength + "米,耗时:" + elapsed + "秒");
                   //     }
                   //     var point = curr_step.path[index++]
                   //     if (!marker) {
                   //         marker = new google.maps.Marker({ position: point, map: map });
                   //         win.setPosition(point);
                   //         win.open(map);
                   //     } else {
                   //         marker.setPosition(point);
                   //         win.setPosition(point);
                   //     }
                   // }, 30);

                   // 第二种,轨迹回放
                   path = response.routes[0].overview_path;
                   if (path) {
                       timer = window.setInterval(function() {
                           if (!marker) {
                               marker = new google.maps.Marker({ position: path[index++], map: map });
                           } else {
                               if (index < path.length) {
                                   marker.setPosition(path[index++]);
                               } else {
                                   index = 0;
                                   window.clearInterval(timer);
                               }
                           }
                       }, 150);//30是运动时间
                   }
               }
           });
       }
   </script>
</head>
<body onload="init();">
<div id="map" style="width:800px; height:800px;"></div>
</body>
</html>

本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学