Google 地图:更改标记子集的颜色

Google Maps: change color on subset of markers

<!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"></script>
</head> 
<body>
  <div id="map" style="width: 600px; height: 800px;"></div>

  <script type="text/javascript">
    var locations = [
      ['address 1', latitude coordinate, longitude coordinate, 1],
      ['address 2', latitude coordinate, longitude coordinate, 2],
      ['address 3', latitude coordinate, longitude coordinate, 3],
      ...
      ['address 58', latitude coordinate, longitude coordinate, 58],
      ['address 59', latitude coordinate, longitude coordinate, 59],
      ['address 60', latitude coordinate, longitude coordinate, 60],
      ,
    ];

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

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

    var marker, i;

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

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

问(1): 地址 1-5 可以有绿色 pin/marker 而地址 6-60 可以保持默认的红色 pin/marker 吗?

能够使用

将所有引脚更改为绿色、黄色等
marker = new google.maps.Marker({
  icon: 'http://...'
});

然而这改变了所有而不是 select 少数(pins/markers)。

在 Google Maps API v3 中,您可以利用 google.maps.Marker class 的 setIcon 功能来设置标记图标。

例子

var locations = [
     ['Interfaith Medical Center', 40.67836, -73.937781, 1],
     ['Kingsbrook Jewish Medical Center', 40.65935, -73.933232, 2],
     ['Kingsboro Psychiatric Center', 40.656192, -73.937523, 3],
     ['Kings County Hospital Center', 40.657201, -73.944647, 4],
     ['University Hospital of Brooklyn', 40.655378, -73.945763, 5],
     ['New York Methodist Hospital', 40.667098, -73.978336, 6],
     ['Brookdale University Hospital and Medical Center', 40.655411, -73.912418, 7],
     ['Wyckoff Heights Medical Center', 40.704148, -73.917607, 8],
     ['Woodhull Medical Center', 40.698731, -73.942759, 9],
     ['The Brooklyn Hospital Center', 40.690206, -73.97825, 10]
];

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

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

var marker, i;

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

    if (i < 5) {  //set marker icon to green for the first 5 markers
        marker.setIcon('http://maps.google.com/mapfiles/ms/icons/green-dot.png');
    }

    google.maps.event.addListener(marker, 'click', (function (marker, i) {
        return function () {
            infowindow.setContent(locations[i][0]);
            infowindow.open(map, marker);
        }
    })(marker, i));
}
<script src="http://maps.google.com/maps/api/js?sensor=false"
            type="text/javascript"></script>
<div id="map" style="width: 600px; height: 480px;"></div>