html部分
<script charset="utf-8" src="https://map.qq.com/api/js?v=2.exp&key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77"></script>
<!-- 定义地图显示容器 -->
<div id="container"></div>
<div id="wxd"></div>
<div id="wxd1"></div>
<div id="wxd2" style="color:red"></div>
<div id="wxd3" style="color:red"></div>
js部分
var geocoder, map, marker = null;
var wxdindex = 0;
$(function () {
var center = new qq.maps.LatLng(39.916527,116.397128);//设置中心点坐标
//初始化地图
map = new qq.maps.Map(document.getElementById("container"), {
center: center, // 地图的中心地理坐标。
zoom: 10 // 地图的中心地理坐标。
});
});
fenxiAdd(list) 该list为后台返回的 id与地址数据【自己的数据】
- 例如:
function fenxiAdd(list) {
var errorI = 0;
//地址和经纬度之间进行转换服务
geocoder = new qq.maps.Geocoder();
//设置服务请求成功的回调函数
geocoder.setComplete(function (result) {
var lat = result.detail.location.lat;// 纬度
var lng = result.detail.location.lng; // 经度
var ads = result.detail.address; // 地址
$("#wxd").empty().append("<p>(" + id+ "," + ads + "," + lat + "," + lng + ")</p>");
// 在这个位置添加一个覆盖物
var marker = new qq.maps.Marker({
position: new qq.maps.LatLng(lat, lng),
map: map
});
// 添加文本标注
var label = new qq.maps.Label({
position: new qq.maps.LatLng(lat, lng),
offset: new qq.maps.Size(10, -40),// 这个位置文本标注位置偏移使用
map: map,
content: ads
});
//点击Marker会弹出反查结果
qq.maps.event.addListener(marker, 'click', function () {
layer.msg("坐标地址为: " + result.detail.location);
});
});
//若服务请求失败,则运行以下函数
geocoder.setError(function () {
$("#wxd2").append("<p>地址解析失败的【主体ID:" + list[Number(wxdindex - 1)].id + ",地址:" + list[Number(wxdindex - 1)].address + "】</p>");
$("#wxd3").empty().append("<p>地址解析失败的主体个数:" + (++errorI) + "</p>");
});
var id = "";
function codeAddress() {
var i = wxdindex;
id= list[i].id;
geocoder.getLocation(list[i].address);
wxdindex++;
}
//定时任务总次数
var maxCount = list.length;
//随机单位时间范围
//执行时间单位(1毫秒,10/10毫秒;100/百秒;1000/秒,)
var p_unit = 100;
//随机数函数
function randomNum(minNum, maxNum) {
switch (arguments.length) {
case 1:
return parseInt(Math.random() * minNum + 1, 10);
break;
case 2:
return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
break;
default:
return 0;
break;
}
}
//随机数实例函数
function num(unit) {
if (!unit)
unit = p_unit;
var n = randomNum(1, 5);
var nowDate = new Date().getTime();
console.log(timestampToTime(nowDate) + "间隔" + n * unit / 1000 + "秒执行");
return n * unit;
}
//执行任务.
var ii = setInterval(befor, num());
var index = 1;
function befor() {
clearInterval(ii);
//主函数
codeAddress();
$('#wxd1').empty().append("温馨提示:任务总数>>>" + maxCount + ", 当前执行次数>>>" + index);
console.log("温馨提示:任务总数>>>" + maxCount + ", 当前执行次数>>>" + index)
//递归
if (index < list.length) {
ii = setInterval(befor, num())
} else {
$('#wxd1').empty().append("温馨提示:任务完毕,总执行次数>>>" + index);
console.log("温馨提示:任务完毕,总执行地址解析次数>>>" + index)
}
//递归使用,当前索引+1
index = index + 1;
}
}
//时间格式化
function timestampToTime(timestamp) {
var date = new Date();//时间戳为10位需*1000,时间戳为13位的话不需乘1000
var Y = date.getFullYear() + '-';
var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
var strDate = Y + M + D + h + m + s;
return strDate;
}