<!DOCTYPE html>
<html>
<head>
<title>try</title>
<script type="text/javascript">
window.onload=init;
//自定义回调函数,重点
function jsonpcallback(rs) {
// var rs = JSON.stringify(rs);
var city = rs['content']['address_detail']['city'];
city = city.toString().substr(0,city.length - 1);
alert(city);
console.log(rs);//百度地图返回值
//console.log(city);//城市名称,去掉最后的“市”
//console.log(rs['content']['point']['x'],',',rs['content']['point']['y']);//坐标
}
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('&');
//console.log(fullpath);
CreateScript(fullpath);
//生成script
function CreateScript(src){
var el = document.createElement('script');
//console.log(el);
el.src = src;
el.async = true;
el.defer = true;
document.body.appendChild(el);
}
}
</script>
</head>
<body>
lalalalla
</body>
</html>
js百度地图WebAPI接口普通IP定位
最新推荐文章于 2024-06-28 09:38:40 发布