当前位置:  开发笔记 > 编程语言 > 正文

群集上的Google Maps InfoWindow

如何解决《群集上的GoogleMapsInfoWindow》经验,为你挑选了1个好方法。

我有一张有很多标记的地图.所有这些标记都有InfoWindow.使用Markers Cluster Lib(http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/src/markerclusterer.js),我可以获得在点击时放大的群集.
一些标记具有完全相同的坐标,因此即使达到最大变焦,它们也会变成一个簇.到目前为止,这一切都很好,除了我想在点击集群时打开InfoWindow,该集群在缩放时从不分割成标记.在这个InfoWindow中,我想根据它包含的标记显示信息.

到目前为止这是我的代码.InfoWindow在Markers上运行良好,但在单击Clusters时不显示InfoWindow.

function initialize(lat, lng) {
   var myLatlng = new google.maps.LatLng(lat,lng);
   var mapOptions = {
      mapTypeControl: false,
      center: myLatlng,
      zoom: 14,
      maxZonn:15
   };

    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

    google.maps.event.addListener(map, 'idle', function() {
        getMarkers(map.getBounds());  

    });

};

function getMarkers(bounds){
    var filter = build_filter();
    var bounds  = {
        'swlat':bounds.getSouthWest().lat(),
        'swlng':bounds.getSouthWest().lng(),
        'nelat':bounds.getNorthEast().lat(),
        'nelng':bounds.getNorthEast().lng()
    };

    data = {
        'bounds': bounds
    }


 $.ajax({
    type: "POST",
    dataType: 'json',
    async: false,
    url: "",
    data: data,
    cache: true,
    success: function (json) {
        addMarkers2Map(json);

    }
    });
}    


function addMarkers2Map(data){

    $('#properties_counter').html(data.length);
    var markers = []; 
    for (var i = 0; i < data.length; ++i) {
        // set the marker position
        var latLng = new google.maps.LatLng(data[i].lat, data[i].lng);
        console.log(data[i].lat);


        // drop the marker
        var marker = new MarkerWithLabel({
            position: latLng,
            map: map,
            labelContent: data[i].price,
            labelAnchor: new google.maps.Point(27, 35),
            title: data[i].title,
            labelClass: "map-markers",
            zIndex: i
            // icon: ' '

        });

        markers.push(marker);

        var infowindow = null;
        buildInfoWindow(marker,map,data[i], i);

    }

    var markerCluster = new MarkerClusterer(map, markers);

    google.maps.event.addListener(markerCluster, 'click', function() {

            infowindow.open(map,markerCluster);

    });
}

function buildInfoWindow(marker, map, data, index){
    var strVar="";
    strVar += "
"; strVar += data.name+"<\/i> | "+data.age+" <\/i> | "+data.gender+" <\/i>"; strVar += "

我怎样才能做到这一点?



1> geocodezip..:

向MarkerClusterer添加"clusterclick"侦听器,在触发该事件时打开infowindow.

概念证明小提琴

结果地图的屏幕截图

代码段:

var gm_map;
var markerArray = [];
var infoWindow = new google.maps.InfoWindow();

function initialize() {
  var marker, i;

  var options_googlemaps = {
    minZoom: 4,
    zoom: 18,
    center: new google.maps.LatLng(59.328631, 13.485688),
    maxZoom: 18,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    streetViewControl: false
  }

  gm_map = new google.maps.Map(document.getElementById('google-maps'), options_googlemaps);


  var options_markerclusterer = {
    gridSize: 20,
    maxZoom: 18,
    zoomOnClick: false,
    imagePath:  'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'
  };



  var markerCluster = new MarkerClusterer(gm_map, clusterMarkers, options_markerclusterer);

  google.maps.event.addListener(markerCluster, 'clusterclick', function(cluster) {

    var markers = cluster.getMarkers();

    var array = [];
    var num = 0;

    for (i = 0; i < markers.length; i++) {

      num++;
      array.push(markers[i].getTitle() + '
'); } if (gm_map.getZoom() <= markerCluster.getMaxZoom()) { infoWindow.setContent(markers.length + " markers
" + array); infoWindow.setPosition(cluster.getCenter()); infoWindow.open(gm_map); } }); for (i = 0; i < clusterMarkers.length; i++) { var marker = clusterMarkers[i]; google.maps.event.addListener(marker, 'click', (function(marker) { return function() { infoWindow.setContent(this.getTitle()); infoWindow.open(gm_map, this); } })(marker)); } } google.maps.event.addDomListener(window, 'load', initialize); var clusterMarkers = [ new google.maps.Marker({ position: new google.maps.LatLng(59.381059, 13.504026), map: gm_map, title: "P1220214 1.JPG" }), new google.maps.Marker({ position: new google.maps.LatLng(59.338683, 13.492057), map: gm_map, title: "P1220214 2.JPG" }), new google.maps.Marker({ position: new google.maps.LatLng(59.340715, 13.49631), map: gm_map, title: "P1220214 3.JPG" }), new google.maps.Marker({ position: new google.maps.LatLng(59.327232, 13.487384), map: gm_map, title: "P1220214 4.JPG" }), new google.maps.Marker({ position: new google.maps.LatLng(59.379034, 13.516566), map: gm_map, title: "P1220214 5.JPG" }), new google.maps.Marker({ position: new google.maps.LatLng(59.328631, 13.485688), map: gm_map, title: "P1220214 6.JPG" }), new google.maps.Marker({ position: new google.maps.LatLng(59.328657, 13.485591), map: gm_map, title: "P1220214 7.JPG" }), new google.maps.Marker({ position: new google.maps.LatLng(59.328501, 13.485782), map: gm_map, title: "P1220214 8.JPG" }) ]
.photo-map {
  background-color: #222222;
  height: 500px;
  width: 100%;
}




推荐阅读
ar_wen2402851455
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有