1

我正在使用 Google Maps For Javascript。我重新组织了所有选项和工作真实位置。但是当我添加样式和标记时,我看不到效果。我该如何解决?

    var locations = [
      ['MyLocation', 41.084951,29.016048],
    ];


    var waStyle = [
            {
                featureType: "all",
                stylers: [
                    { saturation: -100 }
                ]
            }
        ];

    var map = new google.maps.StyledMapType(waStyle,{name: "MyLoc"});

    var img = new google.maps.MarkerImage("/themes/bc/images/bc_pin.png");

    var map = new google.maps.Map(document.getElementById('map_detail'), {
      zoom: 10,
      center: new google.maps.LatLng(41.084951,29.016048),
      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));

    }
4

1 回答 1

1

使用您的代码,我看到了标记。该样式不可见,因为您没有在地图上使用它。

文件

要实际使用该样式(您的代码带有一些注释并从上面引用的文档中添加了代码):

// Create a new StyledMapType object, passing it the array of styles,
// as well as the name to be displayed on the map type control.
var styledMap = new google.maps.StyledMapType(waStyle,{name: "MyLoc"});

var img = new google.maps.MarkerImage("mapIcons/marker1.png");

// Create a map object, and include the MapTypeId to add
// to the map type control.
map = new google.maps.Map(document.getElementById('map_canvas'), {
  zoom: 10,
  center: new google.maps.LatLng(41.084951,29.016048),
  mapTypeId: google.maps.MapTypeId.ROADMAP,
  mapTypeControlOptions: {
    mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'map_style']
  }
});

//Associate the styled map with the MapTypeId and set it to display.
map.mapTypes.set('map_style', styledMap);
map.setMapTypeId('map_style');

要使用您定义的标记图标,请将其包含在 google.maps.Marker 构造函数中:

  marker = new google.maps.Marker({
    position: new google.maps.LatLng(locations[i][1], locations[i][2]),
    icon: img,
    map: map
  });

请注意,如果您有多个标记,则将 infoWindow 与标记关联时会遇到问题,这可以通过函数闭包(createMarker 函数)修复,并且是常见问题解答。

基于您的代码的工作示例

于 2012-12-28T14:31:23.740 回答