我的地图上的隐形风格

时间:2012-12-28 14:02:44

标签: google-maps google-maps-api-3 google-maps-markers google-maps-styled

我正在使用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));

    }

1 个答案:

答案 0 :(得分:1)

使用您的代码,我看到了标记。由于您未在地图上使用该样式,因此该样式不可见。

documentation

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

// 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函数)进行修复,并且是常见问题解答。

working example based off your code