3

我是超级绿色,需要一些项目帮助。由于我对 html 和 javascript 的了解有限,我能够制作一个包含多个位置的谷歌地图。这是我的代码(如果它不好,请原谅我,但它有效):

<!DOCTYPE html>
<html> 
<head> 
<meta http-equiv="content-type" content="text/html; charset=UTF-8" /> 
<title>Google Maps Multiple Markers</title> 
<script src="http://maps.google.com/maps/api/js?sensor=false" 
      type="text/javascript"
  src="https://maps.googleapis.com/maps/api/js?libraries=visualization&key="my key"&sensor=false">
</script>
   <style type="text/css">
     html { height: 100% }
     body { height: 100%; margin: 0px; padding: 0px }
     #map { height: 100% }
</style>
</head> 
<body> 
<div id="map"></div> 

<script type="text/javascript">

var locations = [
[47.003088, -96.929448],
[46.817268, -96.810024],
[46.842064, -96.796163],
[46.644816, -96.408616],
[46.860142, -96.751281],
[46.789894, -96.807715],
[46.844050, -96.856140],
[46.921464, -96.780208],
[46.585881, -96.843754],
[46.860947, -96.913269],
[46.844127, -96.813441],
[46.852872, -96.891369],
[46.828628, -96.775868]
];

var map = new google.maps.Map(document.getElementById('map'), {
  zoom: 8,
  center: new google.maps.LatLng(46.827, -96.846),
  mapTypeId: google.maps.MapTypeId.ROADMAP
});

var marker, i;

for (i = 0; i < locations.length; i++) {  
  marker = new google.maps.Marker({
    position: new google.maps.LatLng(locations[i][0], locations[i][1]),
    map: map
  });
 }
</script>
</body>
</html>

我真正想做的是让标记仅以更近的缩放级别出现。缩小时我不需要它们。如果他们在我接近时出现,那将是完美的。

4

1 回答 1

3

我认为您将不得不像这样绑定 zoom_changed 事件

google.maps.event.addListener(map, 'zoom_changed', function() {
   var zoom = map.getZoom();
    if( zoom > minValue && zoom < maxValue) {
         //show markers if not already there
    }
    else {
        //remove markers if already there
    }
});

编辑

var locations = [
    [47.003088, -96.929448],
    [46.817268, -96.810024],
    [46.842064, -96.796163],
    [46.644816, -96.408616],
    [46.860142, -96.751281],
    [46.789894, -96.807715],
    [46.844050, -96.856140],
    [46.921464, -96.780208],
    [46.585881, -96.843754],
    [46.860947, -96.913269],
    [46.844127, -96.813441],
    [46.852872, -96.891369],
    [46.828628, -96.775868]
];

var map = new google.maps.Map(document.getElementById('map'), {
  zoom: 8,
  center: new google.maps.LatLng(46.827, -96.846),
  mapTypeId: google.maps.MapTypeId.ROADMAP
});


var markers = [];

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

 var minValue = , maxValue = ;

 google.maps.event.addListener(map, 'zoom_changed', function() {
   var zoom = map.getZoom();
    if( zoom > minValue && zoom < maxValue) {
       for (var i = 0; i < locations.length; i++) {    
         markers[i].setMap(map);
       }
    }
    else {
       for (var i = 0; i < locations.length; i++) {    
         markers[i].setMap(null);
       }
    }
});
于 2013-04-26T20:58:18.260 回答