尝试将Geocoder用于几个包含100个地址的数组

时间:2016-08-23 21:49:54

标签: javascript jquery google-maps

我使用运行Geocoder的javascript程序获取最多100个地址数组的纬度和经度,并且运行正常。但是,如果我想处理超过100的地址列表,我必须多次运行该程序,每次填充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数组中拆分地址数组并调用geocode()函数的函数,但它只处理最后一个数组

    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 */
            }
    }

这是geocode()函数:

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";

}

1 个答案:

答案 0 :(得分:0)

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

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 */
            }
    }

并且accept是地理编码方法中的参数

function geocode(address) 

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

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

为什么您需要将地址拆分为仅包含100个地址的数组?使用150个地址的数组也应该有效。

您可以尝试将它们直接发送到地理编码函数,而不是遍历temp_addresses

geocode(temp_addresses);