简体   繁体   English

如何使用Google Maps API动态删除标记?

[英]How to remove markers dynamically with Google Maps API?

I'm writing a program that take in an address, state, or zip code. 我正在编写一个包含地址,州或邮政编码的程序。 Queries our database take the results and push them to maps and markers with Google Maps API. 查询我们的数据库以获取结果,并使用Google Maps API将其推送到地图和标记。

Here is the function to add a marker to the page: 这是向页面添加标记的功能:

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);
    }
  });
}

Here is how I'm pushing the addresses to the function: 这是我将地址推送到函数的方式:

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++;
                               }
                                       }
});

} 

Basically with this function according to their input it will return the correct store outputs them into a div and unordered list. 基本上,根据此函数的输入,它将返回正确的存储输出,并将它们输出到div和无序列表中。 I'm passing the number of rows to the inner HTML of a hidden div "#row_c" then I run through a loop which grabs all the addresses from every div outputted to the screen. 我将行数传递给隐藏的div“ #row_c”的内部HTML,然后运行一个循环,该循环从输出到屏幕的每个div抓取所有地址。

Here is an example of how I get the information for when a user inputs the state abbreviation. 这是当用户输入状态缩写时如何获取信息的示例。

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>";
          }

      } 

How can I delete previous markers? 如何删除以前的标记? For example if a user goes and searches 'CA' that's great everything works fine and dandy. 例如,如果用户去搜索“ CA”,那很好,那么一切就很好而且很花哨。 But if the same user want to search anything else - let's say a specific zip code in California - it does the search but all the previous markers are still there, making their second search pointless. 但是,如果同一用户想要搜索其他内容(例如,加利福尼亚州的特定邮政编码),它会进行搜索,但所有先前的标记仍然存在,从而使第二次搜索毫无意义。

First you need to store all your markers in an array. 首先,您需要将所有标记存储在数组中。

var hotSpotMapMarkers = [];

Then as you create your markers, you store them in the array at the same time you assign them to the map. 然后,当您创建标记时,会将它们存储在数组中,同时将它们分配给地图。

hotSpotMapMarkers.push(<your marker object>);

Then, the next time you refresh your map, remove all the markers first. 然后,下次您刷新地图时,请先删除所有标记。 Something like this: 像这样:

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

This code is from an app I've written that does exactly this. 这段代码来自我编写的一个应用程序。 By the way, there are plenty of examples, which can be found by using Google, that shows how to do this. 顺便说一句,可以通过使用Google找到很多示例,这些示例演示了如何执行此操作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM