网站开发中,有时候会用到地图定位。昨天查了一整天的通过经纬度定位,都没有找到满意的答案。后来,突然想起了,前几天写的博客里有地图标注的问题,一拍脑袋,地图标注不就是通过经纬度来定位的吗?经纬度定位在百度地图API里面叫做逆地址解析。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>经纬度定位定位</title>
<script src="js/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
$(function(){
//纬度在前,经度在后
var location="31.809935,102.537397";
$.ajax({
type: "get",
async: false,
url: "http://api.map.baidu.com/geocoder/v2/?output=json&pois=1&ak=百度AK&coor=bd09ll"+"&location="+location,
dataType: "jsonp",
jsonp: "callback",//传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(一般默认为:callback)
jsonpCallback:"address",//自定义的jsonp回调函数名称
success: function(json){
alert('您查询到的地址: ' +json.result.addressComponent.district);
},
error: function(){
alert('没有查询到!');
}
});
});
</script>
</head>
<body>
</body>
</html>
其中url解析出来是这样的:
{
"status": 0,
"result": {
"location": {
"lng": 102.53739699999994,
"lat": 31.809934973015204
},
"formatted_address": "四川省阿坝藏族羌族自治州马尔康县",
"business": "",
"addressComponent": {
"country": "中国",
"country_code": 0,
"country_code_iso": "CHN",
"country_code_iso2": "CN",
"province": "四川省",
"city": "阿坝藏族羌族自治州",
"city_level": 2,
"district": "马尔康县",
"town": "",
"adcode": "513229",
"street": "",
"street_number": "",
"direction": "",
"distance": ""
},
"pois": [],
"roads": [],
"poiRegions": [],
"sematic_description": "",
"cityCode": 185
}
}
返回的结果就是这样:
谢谢大家观赏!推荐一下,我的群:789826996