如何更改 Google 地图标记的颜色?
我正在使用 Google 地图 API 构建一张充满标记的地图,但我希望一个标记能够从其他标记中脱颖而出。我认为最简单的方法是将标记的颜色更改为蓝色,而不是红色。这是一件简单的事情还是我必须以某种方式创建一个全新的图标?如果我确实必须创建一个新图标,最简单的方法是什么?
I'm using the Google Maps API to build a map full of markers, but I want one marker to stand out from the others. The simplest thing to do, I think, would be to change the color of the marker to blue, instead of red. Is this a simple thing to do or do I have to create a whole new icon somehow? If I do have to create a new icon, what's the easiest way to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
对于 Google Maps API 版本 3,最简单的方法可能是获取自定义图标集,就像 Benjamin Keen 在此处创建的图标集:
http://www.benjaminkeen.com/google-maps-coloured-markers/
如果您将所有这些图标放在与地图相同的位置页面上,您只需在创建标记时使用适当的图标选项即可对标记进行着色:
这非常简单,也是我当前正在从事的项目所使用的方法。
With version 3 of the Google Maps API, the easiest way to do this may be by grabbing a custom icon set, like the one that Benjamin Keen has created here:
http://www.benjaminkeen.com/google-maps-coloured-markers/
If you put all of those icons at the same place as your map page, you can colorize a Marker simply by using the appropriate icon option when creating it:
This is super-easy, and is the approach I'm using for the project I'm working on currently.
Material Design
2019 年 3 月编辑现在具有程序化引脚颜色,
纯 JAVASCRIPT,无图像,支持标签
不再依赖已弃用的图表 API
Material Design
EDITED MARCH 2019 now with programmatic pin color,
PURE JAVASCRIPT, NO IMAGES, SUPPORTS LABELS
no longer relies on deprecated Charts API
MapIconMaker:Google 地图 v2 的库
一种方法是使用
MapIconMaker(死链接)。有一个示例这里(死链接)。 Google 地图默认图标的宽度为 20 像素,高度为 34 像素,因此您可以使用类似的东西来模拟:您甚至可以将其包装在某些函数中,以使事情变得更加轻松:
这就是我个人用于创建的所有标记的方式。我更喜欢随心所欲地改变颜色。
更新:默认图标的十六进制颜色是“#FE7569”。此外,您可以在标记上设置图像,而不是使用新图标创建新标记。因此,如果您想要一个功能来突出显示,您可以使用上面的功能来使用类似的功能:
StyledMarker:Google Maps v3的库
由于V2不久前被V3取代,我想我应该更新这个答案。我创建了一个自定义标记库,可以在 V3 实用程序库 上找到该库
此处(死链接)。它允许不同的颜色和形状,您也可以在标记上放置文本。它通过使用 Google Charts API 来工作,该 API 具有创建 Google 地图类型标记的方法。如果您想直接使用 Google Charts API,请随意查看源代码。然而,该库的特点是它负责为您定义这些标记图像的可点击区域,因此,例如,带有文本的较长气泡将具有人们期望的可点击区域,例如
此示例(死链接) 。MapIconMaker: a library for Google Maps v2
One way is to use the
MapIconMaker(deadlink). There's an examplehere(deadlink). Google Maps default icons are 20px width and 34px height, so you could use something like this to emulate:You could even wrap it in some function to make things even easier on yourself:
That's what I personally use for all markers I create. I prefer to have the option to change colors of a whim.
Update: The Hex color of the default icon is "#FE7569". Also, you can setImage on a Marker rather than creating a new Marker with a new icon. So if you want a function to highlight you could go with something like this, using the function above:
StyledMarker: a library for Google Maps v3
Since V2 was replaced by V3 sometime ago I thought I should update this answer. I created a library for custom markers that can be found on the V3 Utility Library
here(deadlink). It allows for different colors and shapes, and you can place text on the marker as well. It works by using the Google Charts API which has methods for creating Google Maps type markers. Feel free to look at the source code if you'd rather use the Google Charts API directly.The thing about that library, however, is that it takes care of defining the clickable regions of these marker images for you, so, for instance, the longer bubble with text will have the clickable regions one expects, like
this example(deadlink).由于地图 v2 已弃用,您可能对 v3 地图感兴趣:https://developers .google.com/maps/documentation/javascript/markers#simple_icons
对于 v2 地图:
http://code.google.com/apis/maps/documentation/overlays.html#Icons_overview
您可以让一组逻辑执行所有“常规”引脚,另一组逻辑执行以下操作:使用定义的新标记的“特殊”引脚。
Since maps v2 is deprecated, you are probably interested in v3 maps: https://developers.google.com/maps/documentation/javascript/markers#simple_icons
For v2 maps:
http://code.google.com/apis/maps/documentation/overlays.html#Icons_overview
You would have one set of logic do all the 'regular' pins, and another that does the 'special' pin(s) using the new marker defined.
就我个人而言,我认为 Google Charts API 生成的图标看起来很棒,并且很容易动态定制。
请参阅我在 Google 地图上的回答API 3 - 默认(点)标记的自定义标记颜色
Personally, I think the icons generated by the Google Charts API look great and are easy to customise dynamically.
See my answer on Google Maps API 3 - Custom marker color for default (dot) marker
2023 解决方案:高级标记
2023 Solution: Advanced Markers
???? Hi Google Maps JavaScript API developer here.
Google recently announced the new Advanced Markers, it has some really cool features including customizing the default marker's color, glyph, glyph color and scale etc. And we've been trying to improve the performance.
The new APIs are generally available now, we would love your feedback! ????
我发现的最简单的方法是使用 BitmapDescriptorFactory.defaultMarker() 文档甚至有一个设置颜色的示例。从我自己的代码来看:
The simplest way I found is to use BitmapDescriptorFactory.defaultMarker() the documentation even has an example of setting the color. From my own code:
要自定义标记,您可以通过此在线工具进行操作: https://materialdesignicons.com/
在您的情况下,您需要的地图标记可在此处找到:https://materialdesignicons。 com/icon/map-marker,您可以在线自定义。
如果您只想将默认的红色更改为蓝色,可以加载此图标:http://maps.google.com/mapfiles/ms/icons/blue-dot.png
在此线程中提到过:https://stackoverflow.com/a/32651327/6381715
To customize markers, you can do it from this online tool: https://materialdesignicons.com/
In your case, you want the map-marker which is available here: https://materialdesignicons.com/icon/map-marker and which you can customize online.
If you simply want to change the default Red color to Blue, you can load this icon: http://maps.google.com/mapfiles/ms/icons/blue-dot.png
It's been mentioned in this thread: https://stackoverflow.com/a/32651327/6381715
这篇相对较新的文章 提供了一个简单的示例,其中包含一组有限的 Google 地图彩色图标。
有关使用蓝色和橙色地图标记的真实网站示例,请参阅此网站并查看 Google 下的脚本地图。
This relatively recent article provides a simple example with a limited Google Maps set of colored icons.
For a real website example using blue and orange map markers, see this website and view the script under the Google map.