Google Maps JS API v3标记颜色

时间:2012-03-22 08:18:50

标签: google-maps-api-3

我正在使用一个很棒的代码库,它允许我使用css样式的信息框来获得许多特定的标记。另外,我想在地图上有三种不同类型的彩色标记,表示3条不同的河流。虽然有很多帖子解释了如何为标记着色,但如果我使用这些方法,我的代码会崩溃。我需要有关如何为每个位置指定特定颜色的帮助。除了更多的位置,这里是我的代码与默认的红色标记:

var locations = [
          ['<div id="mm-img"><a href="<?php echo $this->getThemePath()?>/mile-marker-img/Ed-00.jpg" target="_blank"><img src="<?php echo $this->getThemePath()?>/mile-marker-img/Ed-00-sm.jpg" /></a><h3>Mile Marker 0 East</h3><p>Old east channel river mouth, now East Waterway. Spokane street fishing pier. Location of historic river mouth, east channel Duwamish River.<br /><span style="font-size:10px;line-height:300%">photo: UW University Libraries</span></p></div>', 47.573600, -122.343585, 1],

          ['<div id="mm-img"><a href="<?php echo $this->getThemePath()?>/mile-marker-img/Ed-01.jpg" target="_blank"><img src="<?php echo $this->getThemePath()?>/mile-marker-img/Ed-01-sm.jpg" /></a><h3>Mile Marker 1 East</h3><p>Kellog Island, Federal Center South, Diagonal Way, Oxbow Building, Shoreline Access. Proposed mile marker design for new Corps of Engineers building on the Duwamish River.</p></div>', 47.560398, -122.341732, 2],

          ['<div id="mm-img"><a href="<?php echo $this->getThemePath()?>/mile-marker-img/BR-LWO.jpg" target="_blank"><img src="<?php echo $this->getThemePath()?>/mile-marker-img/BR-LWO-sm.jpg" /></a><h3>Old Lake Washington Outlet</h3><p>Renton airport. Near Black River resort. View of Resort on Lake Washington prior to 1916, when the lake emptied via the Black River into the Duwamish.<br /><span style="font-size:10px;line-height:300%">photo: Renton Historical Museum</span></p></div>', 47.491100, -122.217169, 38]     
        ];

 var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 13,
          center: new google.maps.LatLng(47.524501, -122.319785),
          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));
        }

3 个答案:

答案 0 :(得分:2)

感谢Hungerstar提出同样出色的解决方案。不幸的是,如果我要使用你的编码,我将不得不重写我已经完成的大部分工作。通过朋友的帮助和this link的组合,我必须完成4个步骤才能获得不同颜色的标记,其中所有内容都在列中的位置。

1)您有

的位置
[popup with a bunch of text, 47.491100, -122.217169, "blue", 38]

2)你输入变量数组,描述你得到的东西

var icons = new Array();
icons["red"] = new google.maps.MarkerImage("http://labs.google.com/ridefinder/images/mm_20_red.png",

3)真正偷偷摸摸的部分,我只需要从另一个人的代码中复制的代码就是如何命名颜色

if (!icons[iconColor]) {
icons[iconColor] = new google.maps.MarkerImage("http://labs.google.com/ridefinder/images/mm_20_"+ iconColor +".png",

4)最后,你得到带

的图标
map: map,
icon: getMarkerImage(locations[i][3])

答案 1 :(得分:1)

答案 2 :(得分:0)

不确定选择哪种颜色的标准。它是位置数组中的最后一个值吗?

我为我的标记使用了一个png精灵,所以对于每个标记我都有这样的东西:

var redIcon = new google.maps.MarkerImage( 
    ABSPATH + 'images/map-icons.png',
    new google.maps.Size( 20, 25 ),
    new google.maps.Point( 0, 60 ),
    new google.maps.Point( 10, 85 )
);

然后确定要使用的颜色并将其附加到标记:

if ( locations[i][3] == 38 ) {
    icon = redIcon;
} else {
    icon = bluIcon;
}

marker.setIcon( icon );