我正在为谷歌地图创建两个不同的图标.但是只显示一个图标.一个名为facility的下拉选择将允许一个基于值'8'的图标,所有其他值将创建另一个图标.
var marker = createMarker(latlng, name, address, city, state, zipcode, telephone, images, url, facility);
function createMarker(latlng, name, address, city, state, zipcode, telephone, images, url, facility) {
var html = "<div id='infodiv'>" + name + "<br/><h4>" + address + "<br/>" + city + ", " + state + " " + zipcode + "<br/>" + (formatPhone(telephone)) + "</h4><div class='infoimage'><img src='" + images + "'></div><div class='footer'><a href='http://" + url + "'>" + url + "</a></div></div>";
var marker = new google.maps.Marker({
icon: icon2,
icon: (facility == 8) ? icon : icon,
map: map,
position: latlng
});
Run Code Online (Sandbox Code Playgroud)
这是图标:
var icon = new google.maps.MarkerImage("./images/plum_flag.png",
new google.maps.Size(35, 52), new google.maps.Point(0, 0),
new google.maps.Point(0, 52));
//callup the orange icon
var icon2 = new google.maps.MarkerImage("./images/orange_flag.png",
new google.maps.Size(26, 39), new google.maps.Point(0, 0),
new google.maps.Point(0, 39));
Run Code Online (Sandbox Code Playgroud)
显示的唯一图标是'var icon = new google.maps.MarkerImage("./ images/plum_flag.png",'无论选择选项是什么......
(facility == 8) ? icon : icon,
Run Code Online (Sandbox Code Playgroud)
意思是:如果facility是8,使用icon,否则使用icon.不是很有用,因为它总是使用icon.
相反,你可能想要:
(facility == 8) ? icon : icon2,
Run Code Online (Sandbox Code Playgroud)
你可能会感到困惑,icon指的icon: icon2是你设置的前一个.但是,两者都icon指的是完全相同的东西,所以你不会看到任何区别.
由于您icon使用三元运算符进行定义,icon:因此也可以省略第一个icon:定义(第二个定义无论如何都会覆盖它).