Google Map API 3 creating different colors for mar

2020-05-05 00:53发布

问题:

I used this code in API 2 but I can not find equivalent code for API 3. I want to create different colors for markers depending on severity so they are not static values. I have problem how to create GICON, G_DEFAULT_ICON, GSize and addOverlay. If someone tell me how to convert this exact code I will be thankful. I do not want to use micons/color-dot.png because of the specific colors I am using.

var severity = parseFloat(markers[i].getAttribute("severity"));
var severityIcon = new GIcon(G_DEFAULT_ICON);
var color;
if (severity == 0) color = "66FF33";
else if (severity == 1) color = "990099";
else if (severity == 2) color = "00CCFF";

severityIcon.image = "http://www.googlemapsmarkers.com/v1/" + color;
severityIcon.iconSize = new GSize(15, 30);

markerOptions = { icon:severityIcon };


  var marker = createMarker(point,label,alarm,markerOptions);
  map.addOverlay(marker);

回答1:

GIcon is no longer part of GMap API V3. But Gabriel Schneider created a Marker extension which solves your problem.

StyledMarker with documentation and examples.

This example demonstrates color changes for the default GMap icons with a letter added. Example

I think you'd want something like:

var styleMaker1 = new StyledMarker({styleIcon:new StyledIcon(StyledIconTypes.MARKER,{color:"66FF33"}),position:myLatLng,map:map});
... // etc.