<script type="text/javascript"> // 百度地图API功能 var map = new BMap.Map("allmap"); map.centerAndZoom(new BMap.Point(116.404, 39.915), 11); //三种驾车策略:最少时间,最短距离,避开高速 var routePolicy = [BMAP_DRIVING_POLICY_LEAST_TIME,BMAP_DRIVING_POLICY_LEAST_DISTANCE,BMAP_DRIVING_POLICY_AVOID_HIGHWAYS]; $("#result").click(function(){ var start = document.getElementById("startPosition").value;//起始点 var end = document.getElementById("endPosition").value;//终止点 map.clearOverlays(); var i=$("#driving_way select").val(); search(start,end,routePolicy[i]); function search(start,end,route){ var driving = new BMap.DrivingRoute(map, {renderOptions:{map: map, autoViewport: true,enableDragging : true},policy: route}); driving.search(start,end); } }); </script>
解决方案就是将变量的声明放在function函数里面。(前提:body处创建两个input)
var start = document.getElementById("startPosition").value;//起始点 var end = document.getElementById("endPosition").value;//终止点