2014-01-18 1 views
0

큰 문제가 있습니다. 나는 새로운 마커를 $ .getJSON 에 의해 동적으로 넣는다. 그러나 나는 또한 맵상의 모든 마커를 삭제하는 함수를 만들고 싶다. 이 내가 전에 함수 XX3

 <script type="text/javascript" 
     src="http://maps.googleapis.com/maps/api/js?keyenter code here=true"> 
    </script> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
    <script type="text/javascript"> 
     var map; 
     function initialize() { 
     var mapOptions = { 
      center: new google.maps.LatLng(40.76455136505513, -73.98056030273438), 
      zoom: 13, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 
     map = new google.maps.Map(document.getElementById("map_canvas"), 
      mapOptions); 
     } 
    </script> 
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>  
    </head> 
    <body onload="initialize()"> 
    <div id='map-ui'> 
    <ul> 
     <li><a onclick="javascript:xx3()" id='find here'>FIND HERE</a></li> 
     <li><a onclick="javascript:muovi()" id='filter-foodx'>skate</a></li> 
     <li><a onclick="javascript:xx4()" id='filter-foodx'>shop</a></li> 
     <li><a onclick="javascript:tutto()" class="active" id='filter-alxl'>show all events</a></li> 
    </ul> 
</div> 
    <div id="map_canvas" style="width:100%; height:100%"></div> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
     $.getJSON("/vaij.php?la=40.76455136505513&lg=-73.98056030273438", function(json1) { 
      $.each(json1, function(key, data) { 
      var latLng = new google.maps.LatLng(data.la, data.lg); 
      // Creating a marker and putting it on the map 
      var marker = new google.maps.Marker({ 
       position: latLng, 
       title: data.namesp 
      }); 
      marker.setMap(map); 
      }); 
     }); 
     }); 
    </script> 
    <script> 

    function clearMarkers() { 
    new google.maps.Marker(null); 
} 

function muovi() { 

     var latLng = new google.maps.LatLng(45.59426285330727 , 8.918224610396585); //Makes a latlng 
     map.panTo(latLng); //Make map global 
    } 



    function xx3() { 




    $.getJSON("/vaij.php?la=45.59426285330727&lg=8.918224610396585", function(json1) { 
     var markers = []; 
      $.each(json1, function(key, data) { 
      var latLng = new google.maps.LatLng(data.la, data.lg); 
      // Creating a marker and putting it on the map 
      var marker = new google.maps.Marker({ 
       position: latLng, 
       title: data.namesp 
      }); 
      marker.setMap(map); 
      }); 
     }); 

} 

function xx4() { 
    map.clearOverlays(); 


var markersArray = []; 

function clearOverlays() { 
    if (markersArray) { 
    for (i in markersArray) { 
     markersArray[i].setMap(null); 
    } 
    } 
} 
} 

    </script> 
+0

을 추천? 마커를 삭제하려고하는 함수를 작성하려고 시도 했습니까? 그렇다면 게시하고 작동하는 것과 그렇지 않은 것을 설명해주십시오. 그렇지 않다면 왜 아직 시도하지 않았습니까? –

+2

나중에 마커를 지울 수 있도록 마커를 추가 할 때 마커의 전체 배열을 유지해야하는 것처럼 들립니다. 이것 [SO 질문] (http://stackoverflow.com/questions/1544739/google-maps-api-v3-how-to-remove-all-markers)을 참조하십시오. –

답변

2

를 제거하는 기능이다 마커 배열. 각 마커를 inix 함수의 배열에 추가하십시오. deleteMarkers 함수에서 마커 배열을 반복하고 각 마커에 대해 setMap(null)을 사용하여 제거합니다.

그래서, 당신의 기능의 외부 다음 inix 기능에서

var markers = []; 

(당신이 당신의 마커를 만든 후) 다음 deleteMarkers 기능에서

markers.push(marker); 

:

for (var i=0; i<markers.length; i++) { 

    markers[i].setMap(null); 
} 
0

예 동적으로 새로운 마커 넣어 삭제 마커이 기능을 사용하려면 때문에! 나는 initialize()에 대해 생각했다! 하지만 나는 다른지도를 사용하고 싶지 않습니다. 나는이 같은 생각이 : 세계를 만들 :

<script type="text/javascript"> 

var map; 
    function initialize() { 
    var mapOptions = { 
     center: new google.maps.LatLng(40.76455136505513, -73.98056030273438), 
     zoom: 13, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    map = new google.maps.Map(document.getElementById("map_canvas"), 
     mapOptions); 
    inix(); 
    } 

    var testing ; 


    function inix() { 

     $.getJSON("/vaij.php?la=40.76455136505513&lg=-73.98056030273438", function(json1) { 
      $.each(json1, function(key, data) { 
      var latLng = new google.maps.LatLng(data.la, data.lg); 
      // Creating a marker and putting it on the map 
      var marker = new google.maps.Marker({ 
       position: latLng, 
       title: data.namesp 
      }); 
      marker.setMap(map); 
      testing=marker; 
      console.log(testing[0]); 
      }); 
     }); 
    } 
    function deleteMarkers() { 


    for (var b=0; b<=10; b++) 
    { 
    testing.setMap(null); 
    } 
    } 

    </script> 

문제는 deleteMarkers 당신이 답변으로 게시 된 의견 중 하나에 명시된대로 코드를 사용하여 하나 개의 마커

0

되세요 늙은 대학 시험 좀 해봤 니?

function clearMarkers() { 
    setAllMap(null); 
} 

으로는 정확히 무엇을 요구하고 있습니다 ... 그래서 here