75

May I know a way to change the Google Map marker color via Javascript.. I am new at this and any help would be much appreciated, Thank you.

I used the following code to create a marker

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

9 回答 9

176

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

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

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

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

其他颜色:

等等。

于 2012-06-16T14:06:05.537 回答
40

您可以使用该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
});

有关其他可能性,请参阅此页面

于 2012-12-23T17:14:26.953 回答
15

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

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

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

看这里的例子

于 2013-11-19T09:54:19.453 回答
15

您可以使用 google chart 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'
});
于 2017-02-10T04:47:06.753 回答
9

我真的很喜欢 Bahadır Yağ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路径图标对象。

结果如下:

在此处输入图像描述

于 2018-04-01T20:42:42.490 回答
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 bellow 中,我为每艘船设置了一种颜色:

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

于 2015-07-15T20:37:09.523 回答
3

我建议使用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更容易破译的 ,作为备用 URL。

khurram 的回答是指重定向到 Google Charts API 的第 3 方网站。这意味着如果那个人关闭了他们的服务器,你就会被淹没。我更喜欢 Google API 提供的灵活性以及直接访问 Google 的可靠性。只需确保为每种颜色指定一个值,否则它将不起作用。

于 2016-11-10T17:11:19.217 回答
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
}

资料来源:google maps apis 上的免费 udacity 课程

于 2018-07-31T21:17:39.313 回答
-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
    });
  }
于 2017-11-01T08:41:06.200 回答