【问题标题】:Showing multiple markers on google maps在谷歌地图上显示多个标记
【发布时间】:2013-08-27 08:36:17
【问题描述】:

我的位置查找器显示多个标记,但我不希望这样。 我对 Google API 不太擅长,所以希望你能帮助我。

人们需要填写他们的地址,然后他需要显示位置。不显示多个标记。 (只有一个)

这里是 JS:

      var geocoder;
      var map;

      function initialize() {
        geocoder = new google.maps.Geocoder();
        var latlng = new google.maps.LatLng(52.132633, 5.291266);
        var myOptions = {
          zoom: 7,
          center: latlng,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById("map_canvas"),
            myOptions);
      }

      function codeAddress() {
        var sAddress = document.getElementById("inputTextAddress").value;
        document.getElementById("map_canvas").style.removeProperty('display');
        geocoder.geocode( { 'address': sAddress}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {

                map.setCenter(results[0].geometry.location);
                map.setZoom(10);

                var marker = new google.maps.Marker({
                    map: map, 
                    position: results[0].geometry.location
                });
            } else {
                alert("Helaas, er is iets fout gegaan. Foutcode: " + status);
            }
          });
      }

【问题讨论】:

    标签: javascript google-maps google-maps-api-3 google-api


    【解决方案1】:

    如果您想控制标记,则需要将它们添加到数组中,然后在添加新标记之前将其全部删除:

      markersArray = []
      function codeAddress() {
        var sAddress = document.getElementById("inputTextAddress").value;
        document.getElementById("map_canvas").style.removeProperty('display');
        geocoder.geocode( { 'address': sAddress}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
    
                map.setCenter(results[0].geometry.location);
                map.setZoom(10);
    
                var marker = new google.maps.Marker({
                    map: map, 
                    position: results[0].geometry.location
                });
                RemoveMarkers();
                markersArray.push(marker)
            } else {
                alert("Helaas, er is iets fout gegaan. Foutcode: " + status);
            }
          });
      }
    
    
      function RemoveMarkers(){
            for(i=0;i<markersArray.length;i++){
                 markersArray[i].remove() // I don't remember exactly the name of the gmaps method     
            }
      }
    

    我没有测试过代码,但这是你需要的想法!

    【讨论】:

    • 感谢@jbartolome 的回答!它仍然无法正常工作。请参阅此处的示例:bvweijen.nl/23g/web/nogps.php
    • 太棒了!我很高兴它有帮助! :)
    猜你喜欢
    • 2017-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-04
    • 1970-01-01
    • 1970-01-01
    • 2012-11-14
    相关资源
    最近更新 更多