从变量设置标记图标网址

时间:2019-05-04 10:51:22

标签: javascript icons google-maps-markers

我想根据IF语句的结果动态更改标记的图标。

当markers [i] [0]的值等于红色时,应使用红色标记图标的url。如果是蓝色,则应使用蓝色图标。

var markers = [ ['abc','123','456','red'],['def','124','556','blue'] ];

for( i = 0; i < markers.length; i++ ) {
   var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
   bounds.extend(position);
   marker = new google.maps.Marker({
     position: position,
     map: map,
     title: markers[i][0],

     if( markers[i][1] = 'red') {
     var jel = '{url: "http://maps.google.com/mapfiles/ms/icons/red-dot.png"}';
     }
     else {
     var sign = '{url: "http://maps.google.com/mapfiles/ms/icons/blue-dot.png"}';
     }
     icon: sign
   });
}

Consolse说:

" Uncaught SyntaxError: Unexpected token [ " 
at the line (11) where I write  "markers[i][1] = 'red')"

我该怎么办?

谢谢!

1 个答案:

答案 0 :(得分:0)

var markers = [ ['abc','123','456','red'],['def','124','556','blue'] ];

for( i = 0; i < markers.length; i++ ) {
   var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
   bounds.extend(position);
   marker = new google.maps.Marker({
     position: position,
     map: map,
     title: markers[i][0],
     icon: markers[i][3] === 'red'
      ? '{url: "http://maps.google.com/mapfiles/ms/icons/red-dot.png"}'
      : '{url: "http://maps.google.com/mapfiles/ms/icons/blue-dot.png"}'
   });
}