百度找了好久没找到,最后还是自力更生解决问题,获取城市的各种信息和编号
直接上代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script type="text/javascript" src="http://libs.baidu.com/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=你的ak值,自己去百度申请"></script>
<title>浏览器定位</title>
</head>
<body>
<div>
<span id="load_geolocation">点击获取位置</span>
</div>
</body>
<script>
$(function(){
$("#load_geolocation").click(function(ev){
$(ev.currentTarget).text("正在获取位置......");
//创建百度地图控件
var geolocation = new BMap.Geolocation();
geolocation.getCurrentPosition(function(r){
var adds =r.point;
var latitude = adds.lat,
longitude = adds.lng;
if(this.getStatus() == BMAP_STATUS_SUCCESS){
$.ajax({
url:'http://api.map.baidu.com/geocoder/v2/?ak=你的ak值&location=' + latitude + ',' + longitude + '&output=json&pois=1',
dataType: 'jsonp',
callback: 'BMap._rd._cbk43398',
success: function(res) {
console.log(res);
var result = res.result,
addressComponent = result.addressComponent,
adcode = addressComponent.adcode
$(ev.currentTarget).text('城市号码为'+ adcode);
}
});
}
else {
$(ev.currentTarget).text('定位失败');
}
},{enableHighAccuracy: true})//指示浏览器获取高精度的位置,默认false
});
});
</script>
</html>