需要注册时获取用户所在地址,找到了一个不错的demo,记录一下:
<script type="text/javascript" src="/Public/js/jquery.min.js"></script>
<!-- 定位 -->
<script type="text/javascript">
$(function () {
var latlon = null;
//ajax获取用户所在经纬度
$.ajax({
url: "http://api.map.baidu.com/location/ip?ak=申请的AK&coor=bd09ll",
type: "POST",
dataType: "jsonp",
success: function (data) {
latlon = data.content.point.y + "," + data.content.point.x;
console.log(latlon);
//ajax根据经纬度获取省市区
$.ajax({
type: "POST",
dataType: "jsonp",
url: 'http://api.map.baidu.com/geocoder/v2/?ak=申请的AK&callback=renderReverse&location=' + latlon + '&output=json&pois=0',
success: function (json) {
if (json.status == 0) {
// console.log(json.result.addressComponent.province+' '+json.result.addressComponent.city+' '+json.result.addressComponent.district);
$('input[name="citys"]').val(json.result.addressComponent.province+' '+json.result.addressComponent.city+' '+json.result.addressComponent.district);
}
}
});
}
});
});
</script>