其他分享
首页 > 其他分享> > 调用百度API,获取指定地点的经纬度

调用百度API,获取指定地点的经纬度

作者:互联网

http://api.map.baidu.com/geocoding/v3/?ak=你的AK&output=json&coordtype=wgs84ll&address=魏县中医医院&callback=showLocation

http://api.map.baidu.com/reverse_geocoding/v3/?ak=你的AK&output=json&coordtype=wgs84ll&location=36.34838186335655,114.94686698427187

 

主要JS如下:

var Hospital = function (name, lon, lat) {
            this.name = name;
            this.lon = lon;
            this.lat = lat;
            this.getName = function () {
                return this.name;
            }
            this.getLon = function () {
                return this.lon;
            }
            this.getlat = function () {
                return this.lat;
            }
        }

var hospitalArr = [
            '魏县中医医院',
            '康保县中医院',
            '怀安县中医院',
            '沽源县中医医院',
            '容城县中医医院',
]

function getLocation(name) {
            var url = "http://api.map.baidu.com/geocoding/v3/?ak=你的AK&output=json&coordtype=wgs84ll&address=" + name + "&callback=showLocation";
            $.ajax({
                type: "get",    //请求方式
                async: false,    //是否异步
                url: url,
                dataType: "jsonp",    //跨域json请求一定是jsonp
                jsonp: "callback",    //跨域请求的参数名,默认是callback
                jsonpCallback: "showLocation",    //自定义跨域参数值,回调函数名也是一样,默认为jQuery自动生成的字符串
                beforeSend: function () {
                    //请求前的处理
                },
                success: function (data) {
                    //请求成功处理,和本地回调完全一样
                    //console.log(name + ',');
                    // var h = new Hospital(name, tlon, tlat);
                    // objArr.push(h);
                    // console.log(h.getName() + ',' + h.getLon() + ',' + h.getlat());
                    console.log(name, ',', data.result.location.lng, ',', data.result.location.lat);
                    // console.log(data);
                },
                complete: function () {
                    //请求完成的处理
                },
                error: function (e) {
                    //请求出错处理
                    console.log(e);
                }
            });
        }


var num = 0;
        var timer = setInterval(function () {
            if (num == 308) {
                clearInterval(timer);
            }
            //console.log('num', num);
            var name = hospitalArr[num];
            //console.log('name', name);
            getLocation(name);
            num = num + 1;

        }, 5000);

  

标签:function,console,name,经纬度,num,API,var,百度,log
来源: https://www.cnblogs.com/sjzww/p/16252996.html