写了好一会 总算是凑出来了;先贴代码
<style type="text/css">
body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;}
</style>
<div id="allmap" style="width:80%;height:80%;">
</div>
<div id="panel" style="position:absolute;right:0px;top:10px;">
</div>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=1730592cac06035bcebfb8aa40d7173b"></script>
<script type="text/javascript">
// 百度地图API功能
var map = new BMap.Map("allmap"); // 创建Map实例
var point = new BMap.Point(117.183957,34.254484 ); // 创建点坐标 目的地坐标
map.centerAndZoom(point,15); // 初始化地图,设置中心点坐标和地图级别。
map.enableScrollWheelZoom(); //启用滚轮放大缩小
var marker = new BMap.Marker(point); // 创建标注
map.addOverlay(marker); // 将标注添加到地图中
var labelgps = new BMap.Label("XXX欢迎你",{offset:new BMap.Size(20,-10)});
marker.setLabel(labelgps); //添加标注
//map.addControl(new BMap.GeolocationControl());//自带的定位,只能定到点
//下面是定位与驾车导航的结合
var gectrl=new BMap.GeolocationControl( {anchor:BMAP_ANCHOR_TOP_LEFT, enableAutoLocation: true });
map.addControl(gectrl); //添加定位控件
var geolocation = new BMap.Geolocation();
geolocation.getCurrentPosition(function (r) {
if (this.getStatus() == BMAP_STATUS_SUCCESS ) {
var myP1 = new BMap.Point(r.point.lng,r.point.lat);
var driving = new BMap.DrivingRoute(map, {renderOptions:{map: map, panel: "panel",enableDragging:true}});//panel是文字说明 手机的话最好去掉 或者改改样子 不然太丑了
driving.search(myP1, point);//绘制路线图
}
})
</script>
这个定位在电脑上偏的离谱 有时候还会定位失败;不过在手机上还是很OK的