没有在网上找到答案。。。大概是我方式不对。
<%--
Created by IntelliJ IDEA.
User:
Date: 2018/9/26
Time: 8:51
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<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">
#allmap {width: 100%;height:40%;overflow: hidden;margin:0;font-family:"微软雅黑";}
body, html {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
html {width: 100%;height: 80%;overflow: hidden;margin:0;font-family:"微软雅黑";}
</style>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=你自己申请的秘钥"></script>
<title>单个标注点沿直线的轨迹运动</title>
</head>
<body>
<div id="allmap"></div>
<!-- <div id = "content"></div>-->
</body>
<script type="text/javascript">
// 百度地图API功能
var map = new BMap.Map("allmap"); //创建Map实例
//初始化 调用centerAndZoom方法对地图初始化
// Point(116.404, 39.915) 以指定的经度和纬度创建一个地理点坐标
map.centerAndZoom(new BMap.Point(116.404, 39.915), 15); //坐标点 第二个级别参数必须有
var myP1 = new BMap.Point(116.380967,39.913285); //起点
var myP2 = new BMap.Point(116.424374,39.914668); //终点
var driving = new BMap.DrivingRoute(map, {onSearchComplete:function(results){
if(driving.getStatus()== BMAP_STATUS_SUCCESS){
var pts = driving.getResults().getPlan(0).getRoute(0).getPath(); //通过驾车实例,获得一系列点的数组
var paths = pts.length; //获得有几个点
console.log("点的个数为:");
console.log("paths=%d",paths);
for(var i=0;i<paths;i++){
console.log(pts[i].lng+" "+pts[i].lat);
//document.write (pts[i].lng+" "+pts[i].lat);
}}
}},{renderOptions:{map:map,autoViewport:true}}); //驾车实例
driving.search(myP1, myP2); //显示一条公交线路
</script>
</html>
结果 就是下图所示,没有调整起始点,根据地图的那个GPS中的单源路线改的(北京的西单到东单)