javascript 标记

by Tan Le Tian

谭乐天

如何使用JavaScript更改Google Maps标记的颜色 (How to change the color of Google Maps markers with JavaScript)

使它们成为粉红色,蓝色,绿色,黄色或紫色! (Make them pink, blue, green, yellow or purple!)

By default, the Google Maps marker is red in color. This article will show how to add different color markers to Google Maps. So, let’s get started. ?

默认情况下,Google地图标记为红色。 本文将介绍如何向Google地图添加不同的颜色标记。 因此,让我们开始吧。 ?

1.载入Google地图 (1. Load Google Maps)

Create an HTML file which loads Google Maps by following Google Maps API official docs: Hello World.

通过遵循Google Maps API官方文档,创建一个加载Google MapsHTML文件:Hello World 。

Your code will look something like the code snippet below.

您的代码看起来像下面的代码片段。

Note: Remember to change YOUR_API_KEY to your actual Google Maps API key.

注意:请记住,将YOUR_API_KEY更改为实际的Google Maps API密钥。

2.添加不同的颜色标记 (2. Add different color markers)

To add a blue color marker, we need to change the icon of the marker. This is done by adding an icon property and specifying a URL for it like below.

要添加蓝色标记,我们需要更改标记的图标。 这可以通过添加icon属性并为其指定URL来完成,如下所示。

icon: {                               url: "http://maps.google.com/mapfiles/ms/icons/blue-dot.png"                           }

Note that we specify blue-dot.png at the end of the URL to get a blue marker. To add a green marker simply change it to green-dot.png so that the URL will be http://maps.google.com/mapfiles/ms/icons/green-dot.png .

请注意,我们在网址末尾指定blue-dot.png以获得蓝色标记。 要添加绿色标记,只需将其更改为green-dot.png ,以使URL为http://maps.google.com/mapfiles/ms/icons/green-dot.png

Some other colors available:

其他一些可用的颜色:

  1. pink: pink-dot.png

    粉色: pink-dot.png

  2. yellow: yellow-dot.png

    黄色: yellow-dot.png

  3. purple: purple-dot.png

    紫色: purple-dot.png

To get the URL of more marker icons, please refer to this website.

要获取更多标记图标的URL,请访问此网站 。

3.包装添加标记功能 (3. Wrap into add marker function)

To make the code cleaner, we can define an addMarker function which takes in latLng and color of the marker. Note that we store the markers added in the a global markersArray in case we need to perform any operations on the markers later.

为了使代码更addMarker ,我们可以定义一个addMarker函数,该函数接受latLng和标记的color 。 请注意,如果以后需要对标记执行任何操作,我们markersArray添加的标记存储在全局markersArray中。

Open the HTML file in the browser. It should look like this:

在浏览器中打开HTML文件。 它看起来应该像这样:

You can get the full final version of the code from here. Please let me know how it goes in the comments below.

您可以从此处获得代码的完整最终版本。 请在下面的评论中让我知道如何进行。

Feel free to check out another Google Maps tutorial I have written:Implement click on JavaScript Google Map to add draggable markers with polyline

随时查看我编写的另一本Google Maps教程: 实现对JavaScript Google Map的单击以使用多义线添加可拖动标记

翻译自: https://www.freecodecamp.org/news/how-to-change-javascript-google-map-marker-color-8a72131d1207/

javascript 标记

javascript 标记_如何使用JavaScript更改Google Maps标记的颜色相关推荐

  1. javascript排序_鸡尾酒在JavaScript中排序

    javascript排序 Just want the code? Scroll all the way down for two versions of the code: 只需要代码? 一直向下滚动 ...

  2. javascript学习_真正学习javascript

    javascript学习 So, you think you know JavaScript. You can write conditional statements galore, have ma ...

  3. rails pry使用_在Rails中使用Google Maps

    rails pry使用 地图是导航世界的方式. 他们具有检查地球上每个位置的能力,从而为路线和土壤成分等服务开辟了道路. 在本文中,我将介绍Google Maps API(和一些替代方法)并将其与Ra ...

  4. javascript指南_熟练掌握JavaScript的指南

    javascript指南 So you're trying to learn JavaScript but are inundated with all the different syntax an ...

  5. javascript排序_鸡尾酒用javascript排序

    javascript排序 Just want the code? Scroll all the way down for two versions of the code: 只需要代码? 一直向下滚动 ...

  6. javascript知识点_一点点JavaScript知识是第1部分很危险的事情

    javascript知识点 几乎是一个数据库的奇怪故事 (The Strange Tale of the Almost-a-Database) 这不是教程,这是一个警告性的故事. (This is n ...

  7. javascript调试_如何提高JavaScript调试技能

    javascript调试 Almost all software developers who have written even a few lines of code for the Web ha ...

  8. javascript组件_是的,JavaScript运行Swift。 无论如何都要构建您的组件库。

    javascript组件 Here's a question I've heard a few times recently: 这是我最近几次听到的一个问题: "What if we cre ...

  9. javascript控制台_如何使用JavaScript控制台改善工作流程

    javascript控制台 by Riccardo Canella 里卡多·卡内拉(Riccardo Canella) 如何使用JavaScript控制台改善工作流程 (How you can imp ...

最新文章

  1. 雇佣最优秀的开发者?培养可能是更好的选择
  2. php解析乱码字符串,PHP substr 截取字符串出现乱码问题解决方法[utf8与gb2312]
  3. python自学行吗-python自学行吗?给编程初学者零基础入门的建议
  4. BZOJ4401: 块的计数 思维题
  5. 中国互联网公司大盘点
  6. nginx配置文件被删怎么找回_恢复误删除的Nginx日志
  7. 在动态库内部获取动态库路径
  8. 网页打开慢升级服务器宽带,网速快打开网页慢怎么办_网络测速很快但是上网很慢如何解决-win7之家...
  9. 素数的线性筛 欧拉函数
  10. 如何评价红米 Note 11T系列手机?网友:没有不足
  11. 空间注意力 通道注意力_注意力发生了什么变化?
  12. linux 7 开启远程桌面,CentOS 7 开启VNC远程桌面
  13. [转载]通过 call gate 访问目标 code segment
  14. 【语音算法】语音的预处理--端点检测
  15. 自行车碟刹与V刹的区别以及优缺点
  16. 使用 BOL API 创建 SAP CRM IBASE 数据
  17. c++ 按分割符(忽略多次出现)切割string字符串
  18. 隐藏窗口和任务栏图标的实现
  19. java代码实现瑟夫环问题
  20. 为什么我电脑一开机显示器不亮要重启显示器才亮

热门文章

  1. Java毕设项目OA办公系统
  2. 关于多视觉几何MVG中,仿射变换的SVD分解问题的程序验证
  3. BZOJ 4892: [Tjoi2017]DNA(SA+RMQ / SAM)
  4. 地址转化net配置(公司网络通外网实验)(图文讲解)
  5. 制作持久化的 Kali U盘
  6. 青竹画材科创板IPO进程终止:原计划募资4亿元,刘其通为控股股东
  7. stream流去除对象的值_I/O流(过滤流、对象序列化、字符流)
  8. 计算机设备机房防静电标准,机房防静电地板设计标准是什么
  9. 火星坐标与地球坐标之间的相互转换的工具类
  10. 信奥赛一本通 C++题解 2036【例5.3】开关门