1

I have a function that loads my map to keep my map static.

<script>
var delArray = new Array();
var gm;
var map;
var iw;
var oms;

window.onload = function(){
    gm = google.maps;
        map = new gm.Map(document.getElementById('map_canvas'), {
            mapTypeId: gm.MapTypeId.TERRAIN,
            center: new gm.LatLng(-29.335205, 24.793563),
            scrollwheel: false, 
            zoom: 6
        });
    iw = new gm.InfoWindow(); 
        oms = new OverlappingMarkerSpiderfier(map, 
            {markersWontMove: true, markersWontHide: true});
}
</script>

I then use another function to construct my spiderfier data.

<script>
function spider(mapData){
    var usualColor = 'eebb22'; 
    var spiderfiedColor = 'ffee22'; 
    var iconWithColor = function(color) { 
        return 'http://chart.googleapis.com/chart?chst=d_map_xpin_letter&chld=pin|+|' + 
        color + '|000000|ffff00'; 
    } 
    var shadow = new gm.MarkerImage( 
        'https://www.google.com/intl/en_ALL/mapfiles/shadow50.png', 
         new gm.Size(37, 34),  // size   - for sprite clipping 
         new gm.Point(0, 0),   // origin - ditto 
         new gm.Point(10, 34)  // anchor - where to meet map location 
    ); 

    oms.addListener('click', function(marker) { 
         iw.setContent(marker.desc); 
         iw.open(map, marker); 
    }); 
    oms.addListener('spiderfy', function(markers) { 
        for(var i = 0; i < markers.length; i ++) { 
            markers[i].setIcon(iconWithColor(spiderfiedColor)); 
            markers[i].setShadow(null); 
        }  
        iw.close(); 
     }); 

     oms.addListener('unspiderfy', function(markers) { 
         for(var i = 0; i < markers.length; i ++) { 
             markers[i].setIcon(iconWithColor(usualColor)); 
             markers[i].setShadow(shadow); 
         } 
     }); 

    var bounds = new gm.LatLngBounds(); 
        for (var i = 0; i < mapData.length; i ++) { 
        var datum = mapData[i]; 
        var loc = new gm.LatLng(datum[0], datum[1]); 
        bounds.extend(loc); 
            var marker = new gm.Marker({ 
                position: loc, 
                title: datum[2], 
                animation: google.maps.Animation.DROP,
                 map: map, 
                 icon: iconWithColor(usualColor), 
                 shadow: shadow 
            }); 
       marker.desc = datum[3]; 
       oms.addMarker(marker);
       delArray.push(marker); 
       } 
       map.fitBounds(bounds); 


 // for debugging/exploratory use in console 
 window.map = map; 
 window.oms = oms;  
}
</script>

And Another to remove markers from the map:

<script>
function delMe(){
if(delArray){
    for(i =0; i <= delArray.length; i++){
        delArray[i].setMap(null);
    }
    this.delArray = new Array();
 }
}
</script>

My map data (mapData) comes from a php script and passed on via Jason. And that's also where I call my delete function right before I call my spider (map) function. This I do to clear the map before I pass the new data.

$( document ).ready(function() {
    delMe();
    var pdata = $js_array;
    spider(pdata);              
});

Now, my problem is that all data is displaying perfectly but after calling the delMe() function it clears the markers 100% but then my map become unresponsive it's not loading new data when calling the spider() function with new data.

I can overcome this problem by reloading/creating the map again, but I want to avoid that and only use a static map. And if I don't delete markers it just fill the map with 100's of markers mixing the old and new.

I am a bit of a noob when it comes to javascript/jquery, any help will be much appreciated!.

4

1 回答 1

0

看起来removeMarker您的函数中缺少一个 OMS 调用delMe,应该是这样的:

function delMe(){
  if (delArray){
    for (i =0; i <= delArray.length; i++){
      oms.removeMarker(delArray[i]);
      delArray[i].setMap(null);
    }
    this.delArray = [];
  }
}

(您可能还有其他问题,但这是一个开始)。

从您写的内容看不清楚,但是您使用的是JS开发者控制台吗?谷歌“[您的浏览器] 开发者控制台”以获取更多信息——它可以让您查看错误是否导致您的地图无响应。

于 2014-09-29T09:28:51.593 回答