spicynumber1
spicynumber1

Reputation: 51

Google Maps JS API v3 marker color

I'm using a fantastic library of code that allows me to have many specific markers with css styled info boxes. In addition, I'd like to have three different categories of colored markers signifying 3 different rivers on a map. Although there are many posts that explain how to color a marker, my code crashes if I use these approaches. I need help with how to assign the specific color to each location. Except for numerous more locations, here is my code with the default red marker:

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

Upvotes: 3

Views: 5084

Answers (3)

spicynumber1
spicynumber1

Reputation: 51

Thank you Hungerstar for coming up with an equally good solution. Unfortunately, if I was to use your coding, I would have to rewrite much of what I've already done. With the combination of a friend's help and this link, I had to complete 4 steps for me to get different colored markers with everything in columns in locations.

1) You have locations with

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

2) You put in the variable, array, describing what you are getting

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

3) The really sneaky part, code I had to simply copy from another guy's code, was how to name the color

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

4) and lastly, you get the icon with

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

Upvotes: 2

hungerstar
hungerstar

Reputation: 21675

Not sure what the criteria for select which color your going to use. Is it the last value in location array?

I use a png sprite for my markers so for each marker I have something like this:

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

Then you determine which color to use and attach it to the marker:

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

marker.setIcon( icon );

Upvotes: 0

Related Questions