gpt4 book ai didi

javascript - 类内的 if 语句

转载 作者:行者123 更新时间:2023-11-28 18:48:49 25 4
gpt4 key购买 nike

我正在尝试使用 Google Maps API 创建医院 map 。圆的半径是该医院的床位数。另外,我希望不同类型的医院有不同的颜色。这可以通过 IF 或 SWITCH 语句来完成,但都不起作用。这里我附上带有 IF 语句的示例。

问题出在语句if (citymap[city].tip = 1)(下面注释掉)。

我确定问题是在这个地方错误使用了函数。

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Circles</title>
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script>

var citymap = {
KBC_ZAGREB: {
center: {lat: 45.823554, lng: 16.005904},
beds: 1975,
contentString: "KBC ZAGREB, 1975 kreveta",
tip: 1
},
KBC_SPLIT: {
center: {lat: 43.503908, lng: 16.457924},
beds: 1521,
contentString: "KBC SPLIT, 1521 kreveta",
tip: 1
},
KBC_RIJEKA: {
center: {lat: 45.332623, lng: 14.425665},
beds: 1191,
contentString: "KBC RIJEKA, 1192 kreveta",
tip: 1
},
KBC_OSIJEK: {
center: {lat: 45.558230, lng: 18.711740},
beds: 1160,
contentString: "KBC OSIJEK, 1160 kreveta",
tip: 1
},
KBC_SESTRE_MILOSRDNICE: {
center: {lat: 45.815438, lng: 15.953599},
beds: 1207,
contentString: "KBC SESTRE MILOSRDNICE, 1207 kreveta",
tip: 1
},
KB_DUBRAVA: {
center: {lat: 45.834369, lng: 16.035823},
beds: 625,
contentString: "KB DUBRAVA, 625 kreveta",
tip: 2
},
KB_MERKUR: {
center: {lat: 45.820832, lng: 15.997447},
beds: 345,
contentString: "KB MERKUR, 345 kreveta",
tip: 2
},
KB_SVETI_DUH: {
center: {lat: 45.820140, lng: 15.938853},
beds: 554,
contentString: "KB SVETI DUH, 554 kreveta",
tip: 2
}

};

function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 13,
center: {lat: 45.811076, lng: 15.979270},
mapTypeId: google.maps.MapTypeId.ROADMAP
});

for (var city in citymap) {

var cityCircle = new google.maps.Circle({

strokeColor: '#FF0000',
fillColor: '#FF0000',

// if (citymap[city].tip = 1){
// strokeColor: '#FF0000',
// fillColor: '#FF0000'
// } else if (citymap[city].tip = 2){
// strokeColor: '#3333cc',
// fillColor: '#3333cc'
// },

strokeOpacity: 0.8,
strokeWeight: 2,
fillOpacity: 0.35,
map: map,
position: citymap[city].center,
center: citymap[city].center,
radius: Math.sqrt(citymap[city].beds) * 10
});

var infowindow = new google.maps.InfoWindow({
content: citymap[city].contentString
});

cityCircle.addListener('click', function() {
infowindow.open(map, cityCircle);
});

}
}

</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?signed_in=true&callback=initMap"></script>
</body>
</html>

最佳答案

单等号=用于赋值,如果要比较可以使用双等号==比较两个值或三等号=== 还要检查这些比较值的类型,在您的情况下,您可以使用两个或三个符号:

if(citymap[city].tip == '1')
//OR
if(citymap[city].tip === '1'))

希望这有帮助。

关于javascript - 类内的 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34857698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com