我正在遍历数组数组。每个元素都有三个值。第三个值可以是High
,Med
或Low
我想输出
http://maps.google.com/mapfiles/ms/icons/red-dot.png for High
http://maps.google.com/mapfiles/ms/icons/yellow-dot.png for Med
http://maps.google.com/mapfiles/ms/icons/green-dot.png for Low
当价值为High
(输出红色)或输出绿色时,我所拥有的效果非常好。
for( i = 0; i < markers.length; i++ ) {
var position = new google.maps.LatLng(markers[i][0], markers[i][1]);
bounds.extend(position);
marker = new google.maps.Marker({
position: position,
icon: markers[i][2] === 'High'? 'http://maps.google.com/mapfiles/ms/icons/red-dot.png' : 'http://maps.google.com/mapfiles/ms/icons/green-dot.png',
map: map,
title: "",
});
}
问题
如何如上所述为High, Med, Low
工作?
答案 0 :(得分:4)
您可以使用对象:
def __init__(self, name, server, province):
stats2 = get_info_doll(province, server, name, "2")
self.attributename = self.Doll2(stats2)
然后使用var icons = {
"High": "http://maps.google.com/mapfiles/ms/icons/red-dot.png",
"Med": "http://maps.google.com/mapfiles/ms/icons/yellow-dot.png",
"Low": "http://maps.google.com/mapfiles/ms/icons/green-dot.png",
};
或类似的东西。
答案 1 :(得分:3)
创建简单的地图对象:
var icons = {
High: "http://maps.google.com/mapfiles/ms/icons/red-dot.png",
Med: "http://maps.google.com/mapfiles/ms/icons/yellow-dot.png",
Low: "http://maps.google.com/mapfiles/ms/icons/green-dot.png"
};
并像这样使用它:
for ( i = 0; i < markers.length; i++ ) {
var position = new google.maps.LatLng(markers[i][0], markers[i][1]);
bounds.extend(position);
marker = new google.maps.Marker({
position: position,
icon: icons[markers[i][2]],
map: map,
title: "",
});
}
答案 2 :(得分:2)
我知道这不是一个很好的阅读,但如果您想使用仅三元运算符,请将(markers[i][2] === 'Med'? '': '')
条件用于markers[i][2] === 'High'?
的虚假条件。
icon: markers[i][2] === 'High'? 'http://maps.google.com/mapfiles/ms/icons/red-dot.png'
: (markers[i][2] === 'Med'? 'http://maps.google.com/mapfiles/ms/icons/yellow-dot.png'
:'http://maps.google.com/mapfiles/ms/icons/green-dot.png')