将MarkerCluster添加到Google地图,不显示

时间:2012-06-18 18:44:51

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

我正在尝试将MarkerCluster功能添加到带有标记的现有Google地图中。我使用API​​指令尝试添加最简单的示例来启动。不幸的是,它使地图消失了。

我假设我将markerClsuter变量放在错误的位置,但我无法确定。我的JavaScript不好,所以有点混乱。

为长篇代码道歉,但我想提供尽可能多的上下文。

有任何见解。

function mainGeo()
{
     if (navigator.geolocation) 
        {
          navigator.geolocation.getCurrentPosition( mainMap, error, {maximumAge: 30000, timeout: 10000, enableHighAccuracy: true} );
    }
    else
    {
          alert("Sorry, but it looks like your browser does not support geolocation.");
    }
}


var stories = {{story_Json|safe}};
var geocoder;
var map;

var markers = [];

function loadMarkers(stories){
    for (i=0;i<stories.length;i++) {
        var story = stories[i];

        (function(story) {
            var pinColor = "69f2ff";
                var pinImage = new google.maps.MarkerImage("http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=%E2%80%A2|" + pinColor,
                    new google.maps.Size(21, 34),
                    new google.maps.Point(0,0),
                    new google.maps.Point(10, 34));
          var point = new google.maps.LatLng(story.latitude, story.longitude);
          var marker = new google.maps.Marker({position: point, map: map, icon: pinImage});
          var infowindow = new google.maps.InfoWindow({
            content: '<div >'+
                '<div >'+
                '</div>'+
                '<h2 class="firstHeading">'+story.headline+'</h2>'+
                '<div>'+
                '<p>'+story.author+'</p>'+
                '<p>'+story.copy+'</p>'+

                '</div>'+
                '</div>'

          });
          google.maps.event.addListener(marker, 'click', function() {
            infowindow.open(map,this);
          });
        })(story);
    }
var markerCluster = new MarkerClusterer(map, markers);
}



 function mainMap(position)
 {
       geocoder = new google.maps.Geocoder();
       // Define the coordinates as a Google Maps LatLng Object
       var coords = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);

       // Prepare the map options
       var mapOptions =
      {
                  zoom: 15,
                  center: coords,
                  mapTypeControl: false,
                  navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
                  mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        // Create the map, and place it in the map_canvas div
        map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

        // Place the initial marker
        var marker = new google.maps.Marker({
                  position: coords,
                  map: map,
                  title: "Your current location!"
        });

        loadMarkers(stories);

    }


  function codeAddress() {
    var address = document.getElementById("address").value;
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
        });
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
  }

2 个答案:

答案 0 :(得分:0)

"simple" example using MarkerClusterer的工作副本显然不包含您未提供的数据。

答案 1 :(得分:0)

当您的地图消失时,很可能是因为某处出现语法错误。也许你错过了逗号或半冒号。当您编辑标记并且标记代码不正确时,通常地图加载和标记不会。

另请注意,如果您向Marker添加了新代码,则标记群集和标记管理器也需要更改核心变量。

例如,当我决定使用带标签的标记创建新功能时,除非我还为标记管理器添加了新功能,否则标记管理器不适用于我。