首页 > 代码库 > 关于百度地图API批量转换成坐标的方法

关于百度地图API批量转换成坐标的方法

 

下面的代码思路是:从页面获取馆Id数组,将其数组通过ajax提交到服务器,服务器返回馆的ID,馆名,和馆地址;

再循环地址,获取每个地址的坐标,并将馆ID和其坐标对应起来,提交到服务器更新,转换失败的显示到页面上。

只记录了部分js代码~

<script type="text/javascript">

 

$(function () {
$("#Iposition").click(function () {
ArrayIdTwo();
});
});

var map = new BMap.Map("waiMap"); //创建地图对象
var localSearch = new BMap.LocalSearch(map); 
var keywordArray = new Array(); //存放转换成功后的参数数组
var keyword = new Array(); //存放馆地址数组
var vId = "";             //
var successParm = ""; //用来获取成功的参数
var vAdress = "";
var index = 0;

 

function ArrayIdTwo() {

//获取选中的馆Id
$("#RpBody .tabshow input[type=‘checkbox‘]").each(function(index) {
if ($(this).attr("checked") == "checked") {
vId += $(this).parent().parent(‘td‘).next("td").html().trim() + ",";
}
});
//传参ID,返回馆地址
$.ajaxSetup({ async: false });
$.getJSON("handler/changeBaidu.ashx?callback=?", { "action": "getLibraryId", id: vId }, function(data) {
if (data =http://www.mamicode.com/="0") return;
for (var i = 0; i < data.length; i++) {
if (i == data.length - 1) {
vAdress += data[i].Id + ":" + data[i].Name + ":" + data[i].Adress;

} else {
vAdress += data[i].Id + ":" + data[i].Name + ":" + data[i].Adress + ",";
}
}
});
var vAdressObj = vAdress.split(","); //切割成每个馆的参数
var adress = "";

for (var j = 0; j < vAdressObj.length; j++) { //循环输出地址数组
adress = vAdressObj[j].split(":");
keyword.push(adress[2]);
keywordArray.push(adress[0]);   //馆ID数组
}
searchBd(); 
}

function searchBd() {

var searchAdd = keyword[index];
setTime();
localSearch.search(searchAdd);
localSearch.setSearchCompleteCallback(function (searchResult) {
var poi = searchResult.getPoi(0);
if (poi == "" || poi == null) { //转换失败,抓取馆名
document.getElementById("error").innerHTML += "转换失败的馆ID:" + keywordArray[index-1] + "、" + searchAdd + ": error" + "</br>";
} else {
successParm += keywordArray[index-1] + "," + poi.point.lng + "," + poi.point.lat + "|";
document.getElementById("result").innerHTML += keywordArray[index - 1] + "、" + searchAdd + ":" + poi.point.lng + "," + poi.point.lat + "</br>";
}
});
}

function setTime() {
if (index < keyword.length) {
setTimeout(window.searchBd, 500);
index++;
} else {
//转换后将变量都清空,防止数据重复
vId = "";
vAdress = "";
index = 0;
keyword = [];
keywordArray = [];
$.getJSON("handler/changeBaidu.ashx?callback=?", { "action": "update", successParm: successParm }, function (data) {
if (data =http://www.mamicode.com/="0") return;
if (data =http://www.mamicode.com/="1") {
successParm = ""; //转换后将变量都清空,防止数据重复
alert("坐标更新完成");
}
});
}
}

</script>

关于百度地图API批量转换成坐标的方法