繁体   English   中英

如何随机设置Google地图标记颜色?

[英]How Can i set google map marker color randomly?

我现在是javascript的新手,我正在跟踪应用程序project.now,我想随机更改google map标记的颜色。这是我的代码

if(type == 'user')
          {
          var marker = new google.maps.Marker({
            map: map,
             icon: 'http://maps.google.com/mapfiles/ms/icons/yellow.png',
            position: point,
            label: icon.label
          });
        }
        else
        {
           var marker = new google.maps.Marker({
            map: map,
             icon: 'http://maps.google.com/mapfiles/ms/icons/green.png',
            position: point,
            label: icon.label
          });
           // var pt = new google.maps.LatLng(point);

        }

          marker.addListener('click', function() {
            infoWindow.setContent(infowincontent);
            infoWindow.open(map, marker);
          });
        });
      });
    }

我在google map中得到很多相同颜色的用户。现在我想随机更改标记颜色。任何人都可以帮助我。

  • 创建具有多种颜色的数组。
  • 使用Math.floor(Math.random() * SIZE_OF_ARRAY) + 1生成值。
  • 使用该随机值从值数组中动态选择颜色。

用您可以拥有的所有src创建一个数组

var icons = ["http://maps.google.com/mapfiles/ms/icons/yellow.png",
            "http://maps.google.com/mapfiles/ms/icons/red.png",
         ...etc
];

并在icon:您可以放置items[Math.floor(Math.random()*icons .length)]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM