3

현재 아래 코드를 사용하여지도에 아이콘을 추가하고 있습니다. javascript 명령을 사용하여 언제든지 하나를 제거 할 수 있기를 원합니다. 이것이 가능한가?Google지도에서 자바 스크립트로 많은 마커 중 하나를 제거하십시오

ex. 장소 5 마커 .. 다른 하나를 유지하면서 3 하나를 제거했습니다.

$('#map').show(); 
var geocoder = new google.maps.Geocoder(); 
var address = document.getElementById("address").value +", " + document.getElementById("city").value; 

geocoder.geocode({ 'address': address}, function(results, status) { 
if (status == google.maps.GeocoderStatus.OK) { 
    var lat = results[0].geometry.location.lat(); 
    var lng = results[0].geometry.location.lng(); 
    locationsall[counter] = new Array(); 
    locationsall[counter][0] = address; 
    locationsall[counter][1] = lat; 
    locationsall[counter][2] = lng; 
    var mapOptions = { 
     zoom: 13, 
     center: new google.maps.LatLng(lat, lng), 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    } 

var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); 
var marker, i; 
for (i = 0; i < locationsall.length; i++) { 
    marker = new google.maps.Marker({ 
    position: new google.maps.LatLng(locationsall[i][1], locationsall[i][2]), 
    map: map, 
    title: 'You are here!' 
    }); 
} 
counter++; 
} else { 
    alert("Geocode was not successful for the following reason: " + status); 
} 

}}; }

편집 1 : for loop

 }).text("Remove").click(function() { 
    var index2 = $tr2.index(); 
    $("#locationDeals input[type='hidden']").each(function() { 
     this.name = this.name.replace(/\[(\d+)\]/, function(str2, number2) { 
      return "[" + (+number2 > index2 - 1 ? number2 - 1 : number2) + "]"; 
     }); 
    }); 
    //locationsall[locations.indexOf(location)].setMap(null); 
    $tr2.remove(); 
    locations.splice(locations.indexOf(location), 1); 
    return false; 
     }).appendTo($tdRemoveRow2); 

답변

2

외부 추가 된 모든 마커를 개최 것 Array을 정의합니다. 예 : allMarkers = []

마커를 만든 후 for loop 안에있는 것을 배열로 밀어 넣습니다. 예 : allMarkers[0].setMap(null), 또는 세 번째 마커 : allMarkers[2].setMap(null)

+0

위의 제안은지도에서 마커 만 제거한다는 점을 기억하십시오. 그것은 쓰레기 수거되지 않습니다. 그렇게하려면 배열의 항목을 삭제해야합니다. – Nils

+0

당신은 절대적으로 맞습니다. 그러나 나는 필요 이상으로 영업 이익을 혼란시키지 않기로했다.) – doesterr

+0

그래서 나는 그것을 시도했지만 그것은 내 상황에는 효과가 없었다. 그것을하는 방법에 대한 조언이 있습니까? (edit1 참조). 기본적으로 테이블을 생성하고 주소를 매핑하는 두 개의 texbox가 있습니다. 각 tr에는 제거 버튼이 있습니다. 제거를 클릭하면 표에서 ti가 제거됩니다. 나는지도에서 그것을 제거하기 위해 그것을 얻으려고 노력하고있다. – Den

1

예,이 가능

첫 번째 마커를 제거 할 allMarkers.push(marker). 기본적으로 마커는지도 자체가 아니라 오버레이입니다. 이러한 오버레이를 배열에 넣은 다음 setMap()을 사용하여 null을 숨길 수 있습니다.

전체 설명서 here.

관련 문제