根据IP自动定位---百度API
<script type="text/javascript">//获取用户当前位置window.onload=init;function init(){var url = 'http://api.map.baidu.com/location/ip';var data = {...
·
<script type="text/javascript">
//获取用户当前位置
window.onload=init;
function init(){
var url = 'http://api.map.baidu.com/location/ip';
var data = {
ak: "你的AK",
coor: "bd09ll",
callback: 'jsonpcallback'//对应值为自定义回调函数名
};
//组合url
var buffer = [];
for (var key in data) {
buffer.push(key + '=' + encodeURIComponent(data[key]));
}
var fullpath = url + '?' + buffer.join('&');
CreateScript(fullpath);
//生成script
function CreateScript(src){
var el = document.createElement('script');
el.src = src;
el.async = true;
el.defer = true;
document.body.appendChild(el);
}
}
function jsonpcallback(rs) {
var city = rs['content']['address_detail']['city'];
console.log(city);//城市名称
}
</script>
转自https://blog.csdn.net/majingjing1314/article/details/80771419
更多推荐



所有评论(0)