根据要求加载Google Map标记HTML

时间:2011-10-11 08:28:06

标签: javascript asp.net ajax google-maps

是否可以仅在用户实际点击时才加载Google地图标记的内容(即“工具提示”中显示的HTML)?

我在我们的网站上嵌入了一个不错的谷歌地图,它运行良好 - 一次只有200个标记。但现在我们想要显示大量的标记(3900+),目前所有用于显示工具提示的微小HTML页面都会在添加标记时设置。理想情况下,这个HTML将使用AJAX加载。

1 个答案:

答案 0 :(得分:2)

是的,通过向标记添加点击监听器,您需要在单击添加到地图的标记时加载所需的数据。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3&sensor=true_or_false"
type="text/javascript"></script>

<div id="map_canvas"> </div>

<script>
     var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
      var myOptions = {
        zoom: 4,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      }
      map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);


      var marker = new google.maps.Marker({
          position: myLatlng, 
          map: map,
          title:"Hello World!"
      });
      google.maps.event.addListener(marker, 'click', function() {

      // When clicked on the marker I use jquery to request some Html 
      // then show in the contents of an info window
        $.ajax({ 
           url: 'http://www.test.com/get-data/',
           dataType: 'html',
           success: function (data) {
               var infowindow = new google.maps.InfoWindow(
                { content: data,
               size: new google.maps.Size(50,50),
               position: marker.position
              });
            infowindow.open(map);
           }

        });

      });

</script>