利用百度地图API获取用户浏览器所在省市区

好久没用过h5新api地理定位——navigator.geolocation,

就在前几天,,,

发现,不能用了???(谷歌被墙了,陷入悲伤)

整个人都懵逼,项目需要定位啊,,,懵逼,,,

然后就写了三天,tm的写不出来,今天终于,,,

直接上代码咯。

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>标题</title>
<meta name="keywords" content="">
<meta name="description" content="">
<script src="jquery-1.8.3.min.js"></script>
</head>
<body>
<script>
$(function(){
var latlon=null;
//ajax获取用户所在经纬度
$.ajax({
url:"http://api.map.baidu.com/location/ip?ak=bFNYmleHunu49WxpIlCN8QxTpzs93c8V&coor=bd09ll",
type:"POST",
dataType:"jsonp",
success:function(data){
latlon=data.content.point.y+","+data.content.point.x;
//ajax根据经纬度获取省市区
$.ajax({
type: "POST",
dataType: "jsonp",
url: 'http://api.map.baidu.com/geocoder/v2/?ak=C93b5178d7a8ebdb830b9b557abce78b&callback=renderReverse&location='+latlon+'&output=json&pois=0',
success: function (json) {
if(json.status==0){
console.log(json.result.addressComponent.district);
}
}
});
}
});
});
</script>
</body>
</html>

但是我能说这定位的真的很不准吗,简直都是错的,但是目前只能利用浏览器这样定位了,谁要能准确定位,求教!!!

好了,下班。

posted @ 2017-04-20 18:05  张郎会飞  阅读(3626)  评论(0编辑  收藏  举报