2016-08-23 48 views
-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"; 

} 

回答

0

您可以尝试将地址数组更改为局部变量而不是全局变量。

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; 

       var 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(addresses); /* function is recursive but is only executed when 'x' get last value */ 
      } 
    } 

而且接受的是,在地理编码方法的参数

function geocode(address) 

您将需要从文件顶部删除以下行

var addresses = null; /* array that will be used by geocode() function */ 

到底为什么你需要拆分地址转换为只有100个地址的数组?使用150个地址的数组也应该起作用。

而是循环访问,你可以尝试直接送他们到地理编码功能

geocode(temp_addresses); 
的temp_addresses的