q2617426
无感五
采纳率73.3%
2017-11-22 04:03

百度地图驾车路线API如何自动获取当前路线

50
 <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%; margin:0;font-family:"微软雅黑";}
        #l-map{height:300px;width:100%;}
        #r-result,#r-result table{width:100%;}
    </style>

    <title>驾车导航的结果面板</title>
</head>
<body>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=1.2"></script>
    <div id="l-map"></div>
    <div id="r-result"></div>
</body>
</html>
<script type="text/javascript">
    // 百度地图API功能
    var map = new BMap.Map("l-map");
    map.centerAndZoom(new BMap.Point(116.404, 39.915), 12);

    var driving = new BMap.DrivingRoute(map, {renderOptions: {map: map, panel: "r-result", autoViewport: true}});
    driving.search("中关村一街", "魏公村");
</script>


<script type="text/javascript">

/*************js实现*************/

$(document).ready(function(){

if(navigator.geolocation){

navigator.geolocation.getCurrentPosition(getPositionSuccess, getPositionError);

}else{

alert("您的浏览器不支持自动定位!");

}

});


/***用户定位成功**/
function getPositionSuccess(position){
var lat = position.coords.latitude;
var lng = position.coords.longitude;
var address;

//通过baiduMap API获取街道名称
var map = new BMap.Map("allmap");
var point = new BMap.Point(lng,lat);
var gc = new BMap.Geocoder();
gc.getLocation(point, function(rs){
  var addComp = rs.addressComponents;
  address = addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber;
  alert("address="+address);
});

alert("longitude="+lng+"  latitude="+lat);
}


/**用户定位失败**/
function getPositionError(error){
switch(error.code){
case error.TIMEOUT:
alert("连接超时,请重试");
break;
case error.PERMISSION_DENIED:
alert("您拒绝了使用位置共享服务,查询已取消");
break;
case error.POSITION_UNAVAILABLE:
alert("亲爱的火星网友,非常抱歉,我们暂时无法为您所在的星球提供位置服务");
break;
}
} 


</script>

请问这两段代码如何融合起来改为自动获取当前位置呢

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • showbo GoCityPass新加坡曼谷通票 4年前

    getPositionSuccess添加描出的代码,第一段的去掉

        /***用户定位成功**/
        function getPositionSuccess(position) {
            var lat = position.coords.latitude;
            var lng = position.coords.longitude;
            var address;
    
            //通过baiduMap API获取街道名称
            var map = new BMap.Map("allmap");
            var point = new BMap.Point(lng, lat);
            var gc = new BMap.Geocoder();
            gc.getLocation(point, function (rs) {
                var addComp = rs.addressComponents;
                address = addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber;
    
                alert("address=" + address);
                ///////////////////////////////////////////////////
                map.centerAndZoom(point, 12);
    
                var driving = new BMap.DrivingRoute(map, { renderOptions: { map: map, panel: "r-result", autoViewport: true } });
                driving.search(address, "魏公村");
    
                ///////////////////////////////////////////////////
            });
    
            alert("longitude=" + lng + "  latitude=" + lat);
        }
    
    点赞 1 评论 复制链接分享

相关推荐