HTML
<div id=”search”>
<input type=”text” id=”start” placeholder=”正在定位您的位置…”/>
<input type=”hidden” id=”start_point” value=””/>
<input type=”hidden” id=”start_location” value=””/>
</div>
JS脚本
<script type=”text/javascript” src=”http://api.map.baidu.com/api?v=2.0&ak=FbzOyQ4YujPrZsxiQKoB07aB”></script>
<script type=”text/javascript” src=”http://libs.baidu.com/jquery/1.8.2/jquery.min.js”></script>
$(function(){
// 定位对象
var geoc = new BMap.Geocoder();
var geolocation = new BMap.Geolocation();
geolocation.getCurrentPosition(function(r){
if(this.getStatus() == BMAP_STATUS_SUCCESS){
$(“#start_point”).val(r.point.lng+’,’+r.point.lat);
setLocation(r.point);
}else {
$(“#start”).attr(“placeholder”,”请输入您的当前位置”)
alert(‘无法定位到您的当前位置,导航失败,请手动输入您的当前位置!’+this.getStatus());
}
},{enableHighAccuracy: true});
function setLocation(point){
geoc.getLocation(point, function(rs){
var addComp = rs.addressComponents;
console.log(rs);
var result = addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber;
$(“#start”).val(result);
$(“#start_location”).val(result);
});
}
})
近期评论