谷歌地图JS API v3标记颜色

3

我正在使用一款非常棒的代码库,它可以让我在地图上拥有很多带CSS样式的标记信息框。此外,我想要三种不同颜色的标记来表示地图上的三条不同的河流。虽然有很多文章解释如何给标记着色,但如果我使用这些方法,我的代码会崩溃。我需要帮助来为每个位置分配特定的颜色。除了许多更多的位置之外,这就是我的代码和默认红色标记:

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个回答

2
感谢Hungerstar提出同样好的方案。不幸的是,如果我要使用你的代码,我就需要重写我已经完成的大部分工作。通过一个朋友的帮助和这个链接的结合,我需要完成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",

最后,您将获得图标。

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


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 );

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接