-1
我使用一個JavaScript程序運行Geocoder獲取經度和緯度的地址數組達到100,它運行良好。但是,如果我想處理超過100個地址的列表,我必須多次運行此程序,每次填充100個地址或更少的地址,直到完成我的列表。試圖使用Geocoder幾個100地址陣列每個
我嘗試修改它,並將數組拆分成幾個數組,每個數組有100個地址,併爲每個數組運行Geocoder。但是,我只執行最後一個數組,可能是因爲程序以遞歸模式運行Geocoder。
有沒有辦法爲幾個數組運行geocode()?
在本例中,我嘗試處理的150個地址的陣列,所以我將在兩個陣列,100之一和50.
在這裏,我初始化變量的另一個它拆分:
<script type="text/javascript">
var map = null;
var geocoder = null;
var addresses = null; /* array that will be used by geocode() function */
var current_address = 0;
var geocode_results = new Array();
var markers = new Array();
var infowindows = new Array();
var timeouts = 0;
var geocodeWait = 1000; //wait a second betweeen requests
function initialize()
{
var mapOptions = {
'zoom': 1,
'center': new google.maps.LatLng(0.0,0.0),
'mapTypeId': google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
geocoder = new google.maps.Geocoder();
}
這是在02陣列和呼叫地理編碼()函數分割的地址陣列的功能,但它僅處理最後陣列
function submitForm()
{
current_address = 0;
/* this is the array that will contain list of 150 addressess */
var temp_addresses = document.getElementById("addresses").value.split("\n");
for(var x=0;x<2;x++) /*Here I try to split list in 2 arrays */
{
current_address = 0;
var ini = x*100;
var fin = ini+99;
addresses = new Array(); /*array is reinitialized in each loop */
if (fin > temp_addresses.length) fin = temp_addresses.length;
for(var i=ini;i<fin;i++)
{
if(temp_addresses[i].length>1)addresses.push(temp_addresses[i]);
/* here 'addresses' array is filled to be used by 'geocode()' function */
}
geocode(); /* function is recursive but is only executed when 'x' get last value */
}
}
這裏是地理編碼()函數:
function geocode() {
if (current_address<addresses.length && geocoder) {
document.getElementById("progress").innerHTML = "Geocoding " + (current_address+1) + " of " + addresses.length;
geocoder.geocode({ 'address': addresses[current_address]},
function(response, status) {
geocode_results[current_address] = new Array();
geocode_results[current_address]['status'] = status;
if (!response || status != google.maps.GeocoderStatus.OK) {
if(status == google.maps.GeocoderStatus.ZERO_RESULTS){
geocode_results[current_address]['lat'] = 0;
geocode_results[current_address]['lng'] = 0;
current_address++;
} else {
timeouts++;
if(timeouts>6){
alert("You have reached the limit of of requests that you can make to google from this IP address in one day, please wait 24 hours to continue");
}
}
} else {
timeouts = 0;
var top_location = response[0];
var lat = Math.round(top_location.geometry.location.lat() * 1000000)/1000000;
var lng = Math.round(top_location.geometry.location.lng() * 1000000)/1000000;
geocode_results[current_address]['lat'] = lat;
geocode_results[current_address]['lng'] = lng;
geocode_results[current_address]['l_type'] = top_location.geometry.location_type;
var marker = markers[current_address] = new google.maps.Marker({
position: new google.maps.LatLng(lat,lng),
map: map,
title:"Line " + (current_address+1)
});
var infowindow = infowindows[current_address] = new google.maps.InfoWindow({
content: addresses[current_address] + "<br/>Latitude:" + lat + "<br/>Longitude:" + lng
});
google.maps.event.addListener(markers[current_address], 'click', function() {
infowindow.open(map,marker);
});
current_address++;
}
var wait = geocodeWait+(timeouts * geocodeWait); //if it keeps timeing out increase wait time
setTimeout("geocode()",wait);
});
} else {
document.getElementById("progress").innerHTML = "finished";
displayResults();
document.getElementById("progress").innerHTML = "";
fitAll();
}
}
function displayResults()
{
var response = ""
for(var i=0;i<addresses.length;i++)
{
response += addresses[i] + "\t" + geocode_results[i]['lat'] + "\t" + geocode_results[i]['lng'] + "\n";
}
document.getElementById("resultados").value += response + "\n";
}