>im 使用指定的图标图像在谷歌地图 API 中执行多重标记取决于我为图标创建了 IF 语句的值,但它没有显示 im 在我的控制台上出现错误
未捕获的语法错误:意外的令牌
在这一行
if (cicon[i][0]<= 1.49) {
这是我的整个剧本
<script>
var cicon = <?php echo json_encode($js_array1); ?>;//added
var locations = <?php echo json_encode($js_array); ?>;
console.log(cicon);//added
console.log(locations);//added
var image1 = "/IntensityMeter/icon_img/number_1.png";
var image2 = "/IntensityMeter/icon_img/number_2.png";
var image3 = "/IntensityMeter/icon_img/number_3.png";
var image4 = "/IntensityMeter/icon_img/number_4.png";
var image5 = "/IntensityMeter/icon_img/number_5.png";
var image6 = "/IntensityMeter/icon_img/number_6.png";
var image7 = "/IntensityMeter/icon_img/number_7.png";
var image8 = "/IntensityMeter/icon_img/number_8.png";
var image9 = "/IntensityMeter/icon_img/number_9.png";
var image10 = "/IntensityMeter/icon_img/number_10.png";
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 5,
center: new google.maps.LatLng(14.599512, 120.984222),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][0], locations[i][1]),
if (cicon[i][0]<= 1.49) {
icon: image1,
} elseif (cicon[i][0]<=2.49){ icon : image2
}elseif (cicon[i][0]<=3.49){ icon : image2
}elseif (cicon[i][0]<=4.49){ icon : image2
}elseif (cicon[i][0]<=5.49){ icon : image2
}elseif (cicon[i][0]<=6.49){ icon : image2
}
map: map
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
</script>
不能在隐式对象定义中使用if
语句,也不能使用elseif
(改用else if
)。要摆脱第一个错误,请改用函数。
var cicon = <?php echo json_encode($js_array1); ?>;//added
var locations = <?php echo json_encode($js_array); ?>;
console.log(cicon);//added
console.log(locations);//added
var image1 = "/IntensityMeter/icon_img/number_1.png";
var image2 = "/IntensityMeter/icon_img/number_2.png";
var image3 = "/IntensityMeter/icon_img/number_3.png";
var image4 = "/IntensityMeter/icon_img/number_4.png";
var image5 = "/IntensityMeter/icon_img/number_5.png";
var image6 = "/IntensityMeter/icon_img/number_6.png";
var image7 = "/IntensityMeter/icon_img/number_7.png";
var image8 = "/IntensityMeter/icon_img/number_8.png";
var image9 = "/IntensityMeter/icon_img/number_9.png";
var image10 = "/IntensityMeter/icon_img/number_10.png";
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 5,
center: new google.maps.LatLng(14.599512, 120.984222),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][0], locations[i][1]),
// Notice function here
cicon: (function(){
if (cicon[i][0]<= 1.49) {
return image1;
} else if (cicon[i][0]<=2.49){ return image2
}else if (cicon[i][0]<=3.49){ return image2
}else if (cicon[i][0]<=4.49){ return image2
}else if (cicon[i][0]<=5.49){ return image2
}else if (cicon[i][0]<=6.49){ return image2
}
})(),
map: map
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
导致错误的原因是使用if
语句来定义对象键。例如,如果要根据变量a
的值创建新对象,这将返回错误:
someObject = {
someParam: someValue,
someOtherParam: someOtherValue,
if(a < 5){
param3: 'a lower than five'
}else{
param3: 'a biger or equal to five'
}
};
您只能在某些函数的范围内使用if
语句。正确的方法是:
someObject = {
someParam: someValue,
someOtherParam: someOtherValue,
param3: (function(){if(a < 5){
return 'a lower than five'
}else{
return 'a biger or equal to five'
}
})()
};