<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

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐