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. OpenCV Python在计算机视觉中的应用
  2. 机器学习进阶-优化的近邻算法
  3. jquery mobile 技巧总结
  4. 车载激光扫描系统检校
  5. 同步异步数据采集卡参数选择
  6. 软件在线升级系统的设计与实现
  7. dma读nand_使用DMA方式读取spi flash问题求助
  8. 知乎获2000赞的Java 多线程超详细总结笔记,看这一篇彻底搞懂线程池
  9. 中文写代码?开始不信后来用中文写了剧情小游戏!嗯,真香~
  10. Unity-URP-基于模板的延迟渲染
  11. 计算机演示文稿步骤,计算机二级Msoffice演示文稿(解题步骤)总结.doc
  12. 数据分析2——核心思维技巧
  13. 解析USGS网站页面中的地震空间数据
  14. git 的批量克隆 批量切换分支 批量pull
  15. C++之余弦函数与宏定义
  16. unity3d 混淆
  17. office 高效办公智慧树_智慧树知到《OFFICE高效办公》见面课答案
  18. 使用yarn代替npm
  19. windows7系统能安MATLAB几,matlab7.0安装win7系统详细使用方法
  20. HW | 安装RTXA6000的注意事项 driver不匹配PKCS#7 signature not signed with a trusted key的修复

热门文章

  1. 48V输入智能磁吸灯-欧创芯OC5266 60V 1.5A 高恒流精度LED 恒流驱动器
  2. 软件项目开发中的三个“不应做”事项
  3. 有限差分法和有限体积法的区别
  4. out了吧,这份最新阿里、腾讯、华为、字节跳动等大厂的薪资和职级对比,你都没看过?
  5. 34 Places to Get Design Inspiration - Online and Offin
  6. Matlab读取pfm文件
  7. 使用uber数据集和kelpergl做路网动态可视化
  8. 2017互联网十大未解之谜,你能回答几个?
  9. [CSS]分享几条漂亮的下划线样式
  10. 排查not eligible for getting processed by all BeanPostProcessors