地図上にマーカーを表示/非表示するボタンを1つ作成しようとしています。コードは、場所の配列の中からマーカーの配列を作成し、最初にそれらを地図上に表示します。マーカーを隠すためにボタンをクリックするとマーカーは隠れますが、マーカーを表示するためにもう一度クリックすると何もしません。私はちょうど初心者ですが、私はこれに長い間立ち往生してきました。助けてください。1つのボタンでGoogleマップにマーカーの配列を表示/非表示する方法
var locations = [
\t ['1', 33.727190, -117.851863],
\t ['2', 34.094715, -117.773466],
\t ['3', 34.143758, -118.782985],
\t ['4', 33.732112, -117.845280],
\t ['5', 33.136157, -117.156101],
\t ['6', 33.875900, -118.034982],
\t ['7', 33.871597, -118.242668],
\t ['8', 33.979397, -118.047032],
\t ['9', 33.710725, -117.859015]
];
var locationsMarkers = [];
function initMap() {
\t var map = new google.maps.Map(document.getElementById('map'), {
\t \t center: {lat: 34.052234, lng: -118.243685},
\t \t zoom: 8
\t });
\t setMapOnLocations(map)
}
function setLocations(locations) {
\t for (var i = 0; i < locations.length; i++) {
\t \t var marker = new google.maps.Marker({
\t \t \t position: new google.maps.LatLng(locations[i][1],locations[i][2]),
\t \t \t map: map
\t \t });
\t \t locationsMarkers.push(marker);
\t }
}
function setMapOnLocations(map) {
\t setLocations(locations);
\t for (var i = 0; i < locationsMarkers.length; i++) {
\t \t locationsMarkers[i].setMap(map);
\t }
}
function clearLocations() {
\t setMapOnLocations(null);
}
function showLocations() {
\t setMapOnLocations(map);
}
$(document).ready(function(){
\t var x = false;
\t $("#button").on('click', function(){
\t \t if (!x){
\t \t \t clearLocations()
\t \t \t x = true;
\t \t }
\t \t else {
\t \t \t showLocations()
\t \t \t x = false;
\t \t }
\t });
});
簡単な説明は間違いを理解するのに役立ちました。 – brigysl