每个只运行一次

时间:2012-03-22 03:21:03

标签: javascript jquery google-maps-api-3

首先让我解释一下。我在页面上有几个地址,我放入一个数组。然后我想要查看该数组并用经度和纬度替换每个地址。​​

问题是我的尝试只运行一次。

$(function () {
 var addr = 0;
 var shops = [];
 var addressPoint;
 var latlng = new google.maps.LatLng(38, -97);
 var myOptions = {
     zoom: 8,
     center: latlng,
     mapTypeId: google.maps.MapTypeId.ROADMAP
 };
 map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
 window.onload = function () {
     var options = [];
     $('.address').each(function () {
         if (!$(this).is(":empty")) {
             options.push($(this).text());
             txt = $(this).text();
         } else {}
     });
     alert(options);
     $.each(options, function () {
         var addr = ("'" + this + "'");
         searchAddr(addr);
     });

     function searchAddr(addr) {
         $('#map_canvas').gmap({
             'callback': function () {
                 var self = this;
                 alert(addr);
                 self.search({
                     'address': addr
                 }, function (results, status) {
                     if (status === 'OK') {
                         addressPoint = results[0].geometry.location;
                         alert(addressPoint);
                         options = $.map(options, function () {
                             return results[0].geometry.location;
                         });
                         self.get('map').panTo(results[0].geometry.location);
                         alert(options);
                         return false;
                     }
                 });
             }
         });
     }
 };
 }(jQuery));

1 个答案:

答案 0 :(得分:1)

你是否使用网络嗅探器确认它只发射一次?因为看起来您正在使用$.map命令中的单个值替换列表中的每个值。也许如果你将地址点放在不同的数组中,它会有所帮助:

$(function () {
 var addr = 0;
 var shops = [];
 var addressPoint;
 var latlng = new google.maps.LatLng(38, -97);
 var myOptions = {
     zoom: 8,
     center: latlng,
     mapTypeId: google.maps.MapTypeId.ROADMAP
 };
 var points = [];
 map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
 window.onload = function () {
     var options = [];
     $('.address').each(function () {
         if (!$(this).is(":empty")) {
             options.push($(this).text());
             txt = $(this).text();
         } else {}
     });
     alert(options);
     $.each(options, function () {
         var addr = ("'" + this + "'");
         searchAddr(addr);
     });

function searchAddr(addr) {
         $('#map_canvas').gmap({
             'callback': function () {
                 var self = this;
                 alert(addr);
                 self.search({
                     'address': addr
                 }, function (results, status) {
                     if (status === 'OK') {
                         addressPoint = results[0].geometry.location;
                         alert(addressPoint);
                         points.push(addressPoint);
                         });
                         self.get('map').panTo(addressPoint);
                         alert(points);
                         return false;
                     }
                 });
             }
         });
     }
};
 }(jQuery));