如何将侧边栏链接到标记

时间:2013-10-30 11:24:51

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

我正在尝试添加侧边栏。我正在使用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>

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

2 个答案:

答案 0 :(得分:1)

如果“name”是一个数组,这应该有效:

    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;

答案 1 :(得分: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;