2014-10-01 3 views
1

주소, 주 또는 우편 번호를 사용하는 프로그램을 작성하고 있습니다. Google 데이터베이스에서 결과를 가져 와서 Google Maps API로지도 및 마커로 푸시합니다.Google Maps API로 마커를 동적으로 제거하는 방법은 무엇인가요?

다음
function codeAddress(address) { 

    geocoder.geocode({ 'address': address}, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
     map.setCenter(results[0].geometry.location); 
     var marker = new google.maps.Marker({ 
      map: map, 
      position: results[0].geometry.location 
     }); 

    } else { 
     alert('Geocode was not successful for the following reason: ' + status); 
    } 
    }); 
} 

내가 함수에 주소를 밀어 해요 방법은 다음과 같습니다 : 여기

페이지에 마커를 추가하는 기능입니다에 따라이 기능을 기본적으로

function get_matches() 
     { 
      var info_ = document.getElementById('address').value; 
    $.ajax({ url: 'find_store.php', 
     data: {info: info_}, 
     type: 'get', 
     success: function(output) { 
         var html = output; 
         $('#dress').html(html); 
           var num =$('#row_c').html(); 
           var counter = 0; 
           while(counter < num) 
           { 
            var addr = $('.addre#'+ counter).html(); 
            codeAddress(addr); 

            counter++; 
           } 
             } 
}); 

} 

자신의 그것을 입력하면 div와 unordered리스트에 정확한 상점 출력이 리턴 될 것입니다. 숨겨진 div "#row_c"의 내부 HTML에 행 번호를 전달할 때 화면에 출력되는 모든 div의 모든 주소를 가져 오는 루프를 실행합니다.

다음은 사용자가 상태 약어를 입력 할 때 정보를 얻는 방법의 예입니다.

else if($type == 2) 
     { 
      $row_count = 0; 
      $z_query = "select * from store_table where state like '%$accept%' and address like '%$accept%' limit 10"; 
      $result = mysql_query($z_query); 
      $num_rows = mysql_num_rows($result); 
      if($num_rows == 0) 
      { 
      echo"<script>alert(\"Your State look-up was unsuccessful, please try again\")</script>"; 

      } 
      $width = ($num_rows * 300)."px"; 
      if($num_rows > 0) 
      { 
       echo"<div id = 'state_container' style = \" margin:none; height:200px; backround-color:hsl(0,0%,95%); position:relative; padding:0px; overflow-x:hidden; overflow-x:scroll; overflow-y:none;\">"; 
       echo"<ul style = 'list-style-type:none; margin-top:0px; margin-left:0px; padding-left:0px; width:$width;'>"; 
       while($row = mysql_fetch_assoc($result)) 
       { 
        $state = "".$row['state'].""; 
        $store = "".$row['store_name'].""; 
        $phone = "".$row['phone'].""; 
        $addr = "".$row['address'].""; 
        $website = "".$row['website'].""; 
        $state = preg_replace('/\s+/', '', $state); 
        $phone = preg_replace('/\s+/', '', $phone); 
        $website = preg_replace('/\s+/', '', $website); 
        $state = stripslashes($state); 
        $store = stripslashes($store); 
        $phone = stripslashes($phone); 
        $addr = stripslashes($addr); 
        $website = stripslashes($website); 




        echo"<li style = 'float:left; margin:none; margin-left:5px; margin-right:10px;'>"; 
        echo"<br/><b>$store</b>"; 
        echo"<br />$phone"; 
        echo"<br /><div class = 'addre' id = '$row_count' style = 'margin:0px; padding:0px;'>$addr</div>"; 
        echo"<br /><a href='$website' style = 'color:#000000; text-decoration:none;'>$website</a>"; 
        echo"</li>"; 



       $row_count++; 
       } 
       echo"<script>$('#row_c').html($row_count)</script>"; 
       echo"</ul>"; 
       echo"</div>"; 
      } 

     } 

어떻게 이전 마커를 삭제할 수 있습니까? 예를 들어 사용자가 가서 'CA'를 검색하면 모든 것이 훌륭하게 멋지게 작동합니다. 그러나 동일한 사용자가 다른 검색을 원한다면 - 캘리포니아의 특정 우편 번호를 말하자면 검색을 수행하지만 이전 마커는 모두 그대로 있으므로 두 번째 검색을 무의미하게 만듭니다.

+0

당신이 사용하고있는 API 버전은 무엇? –

+0

가능한 복제본 : [Google Maps API v3 : 모든 아이콘을 삭제하는 방법] (http://stackoverflow.com/questions/1544739/google-maps-api-v3-how-to-remove-all-markers) – MrUpsidown

답변

1

먼저 모든 마커를 배열에 저장해야합니다.

그런 다음 마커를 만들 때 맵에 할당 할 때 마커를 배열에 저장합니다.

hotSpotMapMarkers.push(<your marker object>); 

그런 다음 다음에지도를 새로 고침 할 때 먼저 모든 아이콘을 제거하십시오. 다음과 같은 내용 :

// Clear all markers currently on the map 
for (var i = 0; i < hotSpotMapMarkers.length; i++) 
    hotSpotMapMarkers[i].setMap(null); 

이 코드는 내가 작성한 응용 프로그램에서 나온 것입니다. 그건 그렇고, Google을 사용하여 찾을 수있는 예제가 많이 있는데,이를 수행하는 방법을 보여줍니다.

+0

Google을 검색했는데 그 중 누구도 너무 많은 의미를 가졌는지 알 수 없습니다. 답장을 보내 주셔서 감사합니다. 나는 이것을 시도 할 것이다. –

0

for (var i = 0, marker; marker = markers[i]; i++) { marker.setMap(null); }

관련 문제