1

我正在尝试清除函数调用上所有标记的地图并加载新标记。

在堆栈上,我找到了这段代码,我在单击按钮时执行了该代码。它没有清除我的标记:

//clear markers
        google.maps.Map.prototype.clearMarkers = function() {
            for(var i=0; i < this.markers.length; i++){
                this.markers[i].setMap(null);
            }
            this.markers = new Array();
        };

清除标记后如何加载新标记?

我的谷歌地图是这样设置的:

 var locations = [['21st Amendment Brewery', 37.7824175, -122.3925921] ,['Allagash Brewing Company', 43.702697, -70.317975] ,['Anchor Brewing Company', 37.763487, -122.401367] ,['Anheuser-Busch', 38.5986606, -90.208751] ,['Ayinger Brewery', , ] ,['Bass Brewery', 52.8045853, -1.6281372] ,['Bear Republic Brewery', 38.6040452, -122.8599961] ,['Boston Beer Company', 42.314449, -71.103365] ,['Boulevard Brewing Company', 39.0821348, -94.5966532] ,['Brasserie des Franches-Montagnes', 47.2542808, 7.0025782] ,['Breckenridge Brewery', 39.723597, -105.000614] ,['Brewery Ommegang', 42.6818046, -74.9254739] ,['Brooklyn Brewery', 40.721535, -73.95745] ,['Brouwerij Artois', 50.8870692, 4.7025108] ,['Brouwerij Huyghe', 50.99832, 3.79404] ,['Buffalo Bills Brewery', 37.6738157, -122.0813029] ,['Butternuts Beer and Ale', 42.6290648, -75.1854868] ,['Caldera Brewing', 42.184409, -122.663147] ,['Cervecera Cuauhtmoc-Moctezuma', 25.6375417, -100.2946801] ,['Clipper City Brewing Company', 39.230827, -76.6751309] ,['Coors Brewing Company', 39.759876, -105.219026] ,['Cricket Hill Brewing Company', 40.872566, -74.296359] ,['D.G. Yuengling and Son', 40.6999592, -76.1747169] ,['Dogfish Head Craft Brewery', 38.770622, -75.310893] ,['DuClaw Brewing Company', 39.496656, -76.329843] ,['Erie Brewing Company', 42.111185, -80.112978] ,['Evolution Craft Brewing Company', 38.4622609, -75.5741469] ,['Firestone Walker Brewing Company', 35.595325, -120.693728] ,['Flying Dog Brewery', 39.361926, -77.424677] ,['Fordham Brewing Company', 39.1540034, -75.4883626] ,['Founders Brewing Company', 42.958468, -85.673516] ,['Fulton Brewing Company', 44.9849423, -93.2790206] ,['Goose Island Beer Company', 41.887107, -87.6720969] ,['Gordon Biersch Brewing', 37.354139, -121.89286] ,['Grand Canyon Brewing Company', 35.249954, -112.1891462] ,['Great Divide Brewing Company', 39.753646, -104.9884698] ,['Green Flash Brewing Company', 32.905722, -117.176517] ,['Grupo Modelo S.A. de C.V.', 19.422447, -99.209758] ,['Guinness', 53.34309, -6.290799] ,['Harpoon Brewery', 43.512997, -72.401527] ,['Heavy Seas', 39.230827, -76.6751309] ,['Heineken International', , ] ,['Hoppin Frog Brewery', 41.0245928, -81.4677107] ,['Jacob Leinenkugel Brewing Company', 44.943321, -91.3953049] ,['Lagunitas Brewing Company', 38.272435, -122.662346] ,['Magic Hat Brewing Company', 44.428405, -73.213148] ,['Maui Brewing Company', 20.972149, -156.677334] ,['Mendocino Brewing Company', 38.9734335, -123.1162429] ,['MillerCoors Brewing Company', 43.044464, -87.962562] ,['Narragansett Brewing Company', 41.8190249, -71.4097368] ,['New Belgium Brewing', 40.593415, -105.066874] ,['North Coast Brewing Company', 39.446471, -123.806238] ,['Old Dominion Brewing Company', 39.1540034, -75.4883626] ,['Oskar Blues Brewery', 40.139056, -105.121887] ,['Pabst Brewing Company', 29.4497864, -98.5386319] ,['Port Brewing Company', 33.141417, -117.150076] ,['Port City Brewing Company', 38.8081958, -77.1014693] ,['RJ Rockers Brewing Company', 34.948639, -81.935726] ,['Rock Art Brewery', 44.557791, -72.608085] ,['Rogue Ales', 44.620173, -124.0520565] ,['Schneider Weisse', 48.91755, 11.87451] ,['Scottish & Newcastle Breweries', 55.954155, -3.202774] ,['Shipyard Brewing', 43.6618784, -70.2489402] ,['Sierra Nevada Brewing Company', 39.7246028, -121.8157154] ,['Sixpoint Craft Ales', 40.6739317, -74.0119473] ,['Sleeman Breweries Ltd.', 43.4870365, -80.2067709] ,['Smuttynose Brewing Co.', 43.032502, -70.7947688] ,['Southern Tier Brewing Company', 42.088931, -79.350342] ,['Spaten-Franziskaner-Bräu', 48.14501, 11.55419] ,['Staatliches Hofbräuhaus (Hofbräu München)', 48.1373073, 11.580357] ,['Stone Brewing Company', 33.115682, -117.119552] ,['Stoudts Brewery', 40.2422441, -76.0540026] ,['Sudwerk', , ] ,['Surly Brewing Company', 45.04285, -93.324557] ,['The Bronx Brewery', 40.80828, -73.930669] ,['The Duck-Rabbit Craft Brewery', 35.600267, -77.597058] ,['The Lost Abbey', 33.141417, -117.150076] ,['The Saint Louis Brewery', 38.632968, -90.209769] ,['Tröegs Brewing Company', 40.304, -76.643519] ,['Unibroue', 45.466125, -73.290924] ,['Union Craft Brewing Company', 39.331938, -76.641971] ,['Victory Brewing Company', 40.0061038, -75.6941546] ,['Wells and Young&#39;s Brewing Company', 52.132011, -0.481523] ,['Weyerbacher Brewing Company', 40.673181, -75.224926] ,['Widmer Brothers Brewing', 45.540795, -122.675594] ,['Yards Brewing Company', 39.962616, -75.136253]  

        ];

    var map = new google.maps.Map(document.getElementById('map_canvas'), {
      zoom: 10,
      center: new google.maps.LatLng(-33.92, 151.25),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });


        var bounds = new google.maps.LatLngBounds();
    var infowindow = new google.maps.InfoWindow();

    var marker, i;

    for (i = 0; i < locations.length; i++) {
        var position = new google.maps.LatLng(locations[i][1], locations[i][2]);  
      marker = new google.maps.Marker({
        position: position,
        map: map
      });



      google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
          infowindow.setContent(locations[i][0]);
          infowindow.open(map, marker);
        }
      })(marker, i));

      bounds.extend(position);

    } //end for loop


    map.fitBounds(bounds);
4

2 回答 2

1

为了能够再次删除标记,您需要将它们保存在某个地方 - 这样您就可以在想要再次删除它们时循环它们。数组是一种方便的方法。

//create a (possibly global) array to store the markers
var markers = [];

...
//create the markers

for(...) {
     marker = new google.maps.Marker(...);
     markers.push(marker);
}

....
//destroy the markers

for (var i = markers.length-1; i >= 0; i--) {
     markers[i].setMap(null);
     delete markers[i];
}
于 2013-03-26T13:23:26.647 回答
0
for (var i = 0; i < markers.length; i++)
{
markers[i].setMap(null);
}
markers.length = 0;

首先删除所有标记,如上面的片段。然后你可以再次使用

 marker = new google.maps.Marker({
    position: position,
    map: map
  });

创建新的标记

于 2013-03-26T13:23:17.787 回答