0

我正在尝试添加侧边栏。我正在使用name从数据库中获取的侧边栏链接变量,它可以作为数组提供给我。我无法循环两次,因为它放入QUERY_LIMIT谷歌数据库。

任何人都可以为我提供有关如何使用此功能的逻辑吗?

这是我的脚本:

<script type="text/javascript">

var markers = [];
var side_html = "";
var icon1 = "prop.png";
    var icon2 = "office.png";
    var locations = <?php echo $add_js ?>;
    var name = <?php echo $name_js ?>

    //function that will be used to handle clicks on the links in the sidebar
      function myclick(num) {
         google.maps.event.trigger(locations[num], "click");
      }

    function geocodeAddress(i) 
    {
        geocoder.geocode({'address' : locations[i]},
        function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
              map.setCenter(results[0].geometry.location);
              createMarker(results[0].geometry.location, i);
             } else {
             alert('Geocode was not successful for the following reason: ' + status);
                    }
            });
     }    

           function createMarker(latlng,i) {
           var marker = new google.maps.Marker({
                                    map : map,
                        icon : icon1,
                                    position : latlng
                });
     var infowindow = new google.maps.InfoWindow({
         size: new google.maps.Size(150,50),
         disableAutoPan: true
         });

      google.maps.event.addListener(marker, 'mouseover', function() {
           marker.setIcon(icon2);
           infowindow.setContent(locations[i]);
           infowindow.open(map, marker);

       });

       google.maps.event.addListener(marker, 'mouseout', function() {
              marker.setIcon(icon1);
              infowindow.setContent(locations[i]); 
              infowindow.close(map, marker);

         });


      return marker;
    }




    var map = new google.maps.Map(document.getElementById('map'), {
        zoom : 10,
        center : new google.maps.LatLng(28.6139, 77.2089),
        mapTypeId : google.maps.MapTypeId.ROADMAP
    });

    //var infowindow = new google.maps.InfoWindow({size: new google.maps.Size(150,50)});

    var geocoder = new google.maps.Geocoder();

    for (var i = 0 ; i < locations.length; i++) {
         geocodeAddress(i);


    // link to the sidebar HTML that will open the info window for the record being processed    
    side_html += '<a href="javascript:myclick(' + i + ')">' + name + '</a><br />';

    document.getElementById("side_bar").innerHTML = side_html;

    }//end of for loop
</script>

我在输出中得到了正确的标记,但是作为侧边栏链接,我每次都得到整个名称数组,并且链接甚至没有响应点击事件。

4

2 回答 2

1

如果“名称”是一个数组,这应该工作:

    for (var i = 0 ; i < locations.length; i++) {
      geocodeAddress(i);
      // link to the sidebar HTML that will open the info window for the record being processed    
      side_html += '<a href="javascript:myclick(' + i + ')">' + name[i] + '</a><br />';
    }//end of for loop
    document.getElementById("side_bar").innerHTML = side_html;
于 2013-10-30T12:38:05.650 回答
1

终于解决了我的问题!

在使用它们显示在侧边栏链接之前,我需要先拆分我的字符串数组。

  var name = names[i].split(",");

最终解决方案代码:

    for (var i = 0 ; i < locations.length; i++) 
    {
    geocodeAddress(i);

    var name = names[i].split(",");

    side_html += '<a href="javascript:myclick(' + i + ')">' + name + '</a><br />';
    }

    document.getElementById("side_bar").innerHTML = side_html;
于 2013-10-31T08:37:45.953 回答