2017-05-23 38 views
0

我是初學者在JavaScript現在我在跟蹤應用程序在工作project.now我想改變谷歌地圖標記顏色randomly.here是我的代碼如何隨機設置Google地圖標記顏色?

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); 
      }); 
     }); 
     }); 
    } 

我得到很多用戶在谷歌地圖相同的顏色。現在我想隨機更改標記顏色。任何人都可以幫助我。

回答

0
  • 用多種顏色創建一個數組。
  • 使用Math.floor(Math.random() * SIZE_OF_ARRAY) + 1來生成該值。
  • 使用該隨機值從數組中動態選擇顏色。
0

讓所有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)]

+0

它的工作好了感謝名單@巴拉的阿亞什 –