마커 표시/숨기기 버튼 하나만 만들려고합니다. 코드는 위치 배열에서 마커 배열을 만들고 처음에는지도에 표시합니다. 버튼을 클릭하여 마커를 숨기면 마커가 숨겨 지지만 다시 클릭하면 마커가 보이지 않습니다. 나는 초보자에 불과하지만 오랫동안이 일에 매달 렸습니다. 도와주세요.하나의 버튼으로 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