Javascript,更改谷歌地图标记颜色

时间:2012-06-16 14:02:58

标签: javascript google-maps google-maps-markers

我是否知道通过Javascript更改Google地图标记颜色的方法..我是新来的,任何帮助都将非常感谢,谢谢。

我使用以下代码创建标记

 marker = new google.maps.Marker({
     position: new google.maps.LatLng(locations[i][1], 
     locations[i][2]),
     animation: google.maps.Animation.DROP,
     map: map
 });

9 个答案:

答案 0 :(得分:157)

在Google Maps API v3中,您可以尝试更改标记图标。例如,使用绿色图标:

marker.setIcon('http://maps.google.com/mapfiles/ms/icons/green-dot.png')

或作为标记初始化的一部分:

marker = new google.maps.Marker({
    icon: 'http://...'
});

其他颜色:

答案 1 :(得分:35)

您可以使用strokeColor属性:

var marker = new google.maps.Marker({
    id: "some-id",
    icon: {
        path: google.maps.SymbolPath.FORWARD_CLOSED_ARROW,
        strokeColor: "red",
        scale: 3
    },
    map: map,
    title: "some-title",
    position: myLatlng
});

有关其他可能性,请参阅this page

答案 2 :(得分:10)

您可以使用此代码,它可以正常工作。

 var pinImage = new google.maps.MarkerImage("http://www.googlemapsmarkers.com/v1/009900/");

 var marker = new google.maps.Marker({
            position: yourlatlong,
            icon: pinImage,
            map: map
        });

see Example here

答案 3 :(得分:8)

你可以使用谷歌图表api并使用rgb代码生成任何颜色:

示例:带#ddd颜色的标记:

http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=%E2%80%A2|ddd

如上所述包含

 var marker = new google.maps.Marker({
    position: marker,
    title: 'Hello World',
    icon: 'http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=%E2%80%A2|ddd'
});

答案 4 :(得分:3)

我有4艘船在一张地图上设置,因此我使用Google Developers示例然后将其扭曲

https://developers.google.com/maps/documentation/javascript/examples/icon-complex

在下面的功能中,我设置了3个颜色选项:

function setMarkers(map, locations) {
...
var image = {
    url: 'img/bullet_amarelo.png',
    // This marker is 20 pixels wide by 32 pixels tall.
    size: new google.maps.Size(40, 40),
    // The origin for this image is 0,0.
    origin: new google.maps.Point(0,0),
    // The anchor for this image is the base of the flagpole at 0,32.
    anchor: new google.maps.Point(0, 40)
  };
  var image1 = {
            url: 'img/bullet_azul.png',
            // This marker is 20 pixels wide by 32 pixels tall.
            size: new google.maps.Size(40, 40),
            // The origin for this image is 0,0.
            origin: new google.maps.Point(0,0),
            // The anchor for this image is the base of the flagpole at 0,32.
            anchor: new google.maps.Point(0, 40)
          };
  var image2 = {
          url: 'img/bullet_vermelho.png',
          // This marker is 20 pixels wide by 32 pixels tall.
          size: new google.maps.Size(40, 40),
          // The origin for this image is 0,0.
          origin: new google.maps.Point(0,0),
          // The anchor for this image is the base of the flagpole at 0,32.
          anchor: new google.maps.Point(0, 40)
        };
  var image3 = {
          url: 'img/bullet_verde.png',
          // This marker is 20 pixels wide by 32 pixels tall.
          size: new google.maps.Size(40, 40),
          // The origin for this image is 0,0.
          origin: new google.maps.Point(0,0),
          // The anchor for this image is the base of the flagpole at 0,32.
          anchor: new google.maps.Point(0, 40)
        };
...
}

在FOR轰鸣声中,我为每艘船设置了一种颜色:

for (var i = 0; i < locations.length; i++) {
...
    if (i==0) var imageV=image;
    if (i==1) var imageV=image1;
    if (i==2) var imageV=image2;
    if (i==3) var imageV=image3;
...
# remember to change icon: image to icon: imageV
}

最终结果:

http://www.mercosul-line.com.br/site/teste.html

答案 5 :(得分:2)

我建议使用Google Charts API,因为您可以使用十六进制颜色代码指定文本,文本颜色,填充颜色和轮廓颜色,例如#FF0000为红色。您可以按如下方式调用它:

function getIcon(text, fillColor, textColor, outlineColor) {
  if (!text) text = '•'; //generic map dot
  var iconUrl = "http://chart.googleapis.com/chart?cht=d&chdp=mapsapi&chl=pin%27i\\%27[" + text + "%27-2%27f\\hv%27a\\]h\\]o\\" + fillColor + "%27fC\\" + textColor + "%27tC\\" + outlineColor + "%27eC\\Lauto%27f\\&ext=.png";
  return iconUrl;
}

然后,当您创建标记时,您只需设置图标属性,其中myColor变量是十六进制值(减去哈希符号):

var marker = new google.maps.Marker({
  position: new google.maps.LatLng(locations[i][1], locations[i][2]),
  animation: google.maps.Animation.DROP,
  map: map,
  icon: getIcon(null, myColor, myColor2, myColor3)
});

如果您只需要设置文字和填充颜色,则可以使用http://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=•|FF0000作为备用网址,这可以更容易解密。

khurram的回答是指重定向到Google Charts API的第三方网站。这意味着如果那个人取下他们的服务器,你就会被软管。我更喜欢Google API提供的灵活性以及直接访问Google的可靠性。只需确保为每种颜色指定一个值,否则它将无法正常工作。

答案 6 :(得分:1)

我真的很喜欢BahadırYağan给出的答案,除了我不喜欢依赖谷歌或外部API提供的有限图标来生成我的标记图标。这是最初的解决方案:

var marker = new google.maps.Marker({
    id: "some-id",
    icon: {
        path: google.maps.SymbolPath.FORWARD_CLOSED_ARROW,
        strokeColor: "red",
        scale: 3
    },
    map: map,
    title: "some-title",
    position: myLatlng
});

这是我改进的解决方案:

var marker = new google.maps.Marker({
            position: myLatlng,
            icon:{
                path: 'm 12,2.4000002 c -2.7802903,0 -5.9650002,1.5099999 -5.9650002,5.8299998 0,1.74375 1.1549213,3.264465 2.3551945,4.025812 1.2002732,0.761348 2.4458987,0.763328 2.6273057,2.474813 L 12,24 12.9825,14.68 c 0.179732,-1.704939 1.425357,-1.665423 2.626049,-2.424188 C 16.809241,11.497047 17.965,9.94 17.965,8.23 17.965,3.9100001 14.78029,2.4000002 12,2.4000002 Z',
                fillColor: '#00FF00',
                fillOpacity: 1.0,
                strokeColor: '#000000',
                strokeWeight: 1,
                scale: 2,
                anchor: new google.maps.Point(12, 24),
            },
        });

我想要一个特定的图钉图标,所以我去了一个用inkscape,然后打开SVG文件并将svg路径复制到代码的路径中,但这适用于你放在“路径”中的任何SVG路径图标对象的属性。

结果如下:

enter image description here

答案 7 :(得分:1)

要扩展接受的答案,您可以使用MarkerImages上的API创建任何颜色的自定义http://chart.googleapis.com

除了更改颜色外,这还会更改标记的形状,这可能是不必要的。

const marker = new window.google.maps.Marker(
        position: markerPosition,
        title: markerTitle,
        map: map)
marker.addListener('click', () => marker.setIcon(changeIcon('00ff00'));)

const changeIcon = (newIconColor) => {
    const newIcon = new window.google.maps.MarkerImage(
        `http://chart.googleapis.com/chart?                                      
        chst=d_map_spin&chld=1.0|0|${newIconColor}|60|_|%E2%80%A2`,
        new window.google.maps.Size(21, 34),
        new window.google.maps.Point(0, 0),
        new window.google.maps.Point(10, 34),
        new window.google.maps.Size(21,34)
    );
    return newIcon
}

来源:free udacity course on google maps apis

答案 8 :(得分:-1)

var map_marker = $(“。map-marker”)。children(“img”)。attr(“src”)         var pinImage = new google.maps.MarkerImage(map_marker);

     var marker = new google.maps.Marker({
      position: uluru,
      map: map,
      icon: pinImage
    });
  }