我使用Google Maps API创建了3个标记,但我只想让一个标记一次处于活动状态,因为每个标记代表游戏中的评分序列。例如,如果得分为0,我只希望芝加哥标记处于活动状态。我迷失了如何做到这一点任何帮助都会很棒。以下是我的一些代码:
<script>
var map;
var score;
function initMap() {
var chicago = {lat: 41.8781, lng: -87.6298};
var indianapolis = {lat: 39.7684, lng: -86.1581};
var oklahomaCity = {lat: 35.4819, lng: -97.5084};
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 0.0, lng: 0.0},
zoom: 1
});
var chicagoMarker = new google.maps.Marker({
position: chicago,
map: map
});
var oklahomaCityMarker = new google.maps.Marker({
position: oklahomaCity,
map:map
});
var indianapolisMarker = new google.maps.Marker({
position: indianapolis,
map:map
});
chicagoMarker.setVisible(false);
indianapolisMarker.setVisible(false);
oklahomaCityMarker.setVisible(false);
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCuvsCAF0gVmwv6AF0SoA3xBjV66RG4r7o&callback=initMap"
async defer></script>
答案 0 :(得分:2)
仅当socre为0时,使用map属性创建标记,并使用setMap(map)创建assugn var map;
var score;
function initMap() {
var chicago = {lat: 41.8781, lng: -87.6298};
var indianapolis = {lat: 39.7684, lng: -86.1581};
var oklahomaCity = {lat: 35.4819, lng: -97.5084};
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 0.0, lng: 0.0},
zoom: 1
});
var chicagoMarker = new google.maps.Marker({
position: chicago
});
var oklahomaCityMarker = new google.maps.Marker({
position: oklahomaCity
});
var indianapolisMarker = new google.maps.Marker({
position: indianapolis
});
if ( score == 0) {
chicagoMarker.setMap(map)
}
}
</script>