简体   繁体   English

单击另一个标记时自动关闭InfoWindow Google Maps API

[英]Auto close InfoWindow Google Maps API when click another marker

I have problem with my Google Maps API v3 script. 我的Google Maps API v3脚本存在问题。 I am using CodeIgniter framework. 我正在使用CodeIgniter框架。 The problem is when I clicked a marker, and then click another marker, InfoWindow Google Maps on previous marker didn't close. 问题是当我点击一个标记,然后点击另一个标记时,InfoWindow Google Maps在之前的标记上没有关闭。 This is my code: 这是我的代码:

<script type="text/javascript">
      var peta;
      var gambar_kantor = new Array();
      var nama     = new Array();
      var kategori = new Array();
      var alamat   = new Array();
      var telpon   = new Array();
      var x        = new Array();
      var y        = new Array();
      var i;
      var url;
      var gambar_marker;
      var gambar_kantor;
      var baseurl  = "<?php echo base_url() ?>";

      function map_init() {
          var map = new google.maps.LatLng(-6.990411, 110.422542);


          var myStyles =[
          {
              featureType: "poi",
              elementType: "labels",
              stylers: [
                    { visibility: "off" }
              ]
          }
          ];

          var petaoption = {
              zoom: 12,
              center: map,
              mapTypeId: google.maps.MapTypeId.ROADMAP,
              styles: myStyles 
              };

          peta = new google.maps.Map(document.getElementById("map_canvas"),petaoption);


          getdatabase();
      }

      function getdatabase(){
          var markers = [];
          var info= [];


          <?php
        $query = $this->db->query("SELECT l.id, l.nama, l.gambar, l.alamat, l.telp, l.latittude, l.longitude, k.nama_kategori, k.ikon
                                    FROM lokasi as l, kategori as k
                                    WHERE l.kategori=k.id");
          $i = 0;
          $js = "";

          foreach ($query->result() as $value) {

          $js .= 'nama['.$i.'] = "'.$value->nama.'";
                  alamat['.$i.'] = "'.$value->alamat.'";
                  telpon['.$i.'] = "'.$value->telp.'";
                  x['.$i.'] = "'.$value->latittude.'";
                  y['.$i.'] = "'.$value->longitude.'";
                  set_icon("'.$value->ikon.'");

                  var point = new google.maps.LatLng(parseFloat(x['.$i.']),parseFloat(y['.$i.']));

                  var contentString = "<table>"+
                                              "<tr>"+
                                                  "<td align=center><br><b>" + nama['.$i.'] + "</b></td>"+
                                              "</tr>"+
                                              "<tr>"+
                                                  "<td align=center width=300px>" + alamat['.$i.'] + "</td>"+
                                              "</tr>"+
                                              "<tr>"+
                                                  "<td align=center> Telp: " + telpon['.$i.'] + "</td>"+
                                              "</tr>"+
                                          "</table>";

                  var currentInfoWindow = null;

                  var infowindow = new google.maps.InfoWindow({
                      content: contentString

                  });


                  var marker = new google.maps.Marker({
                          position: point,
                          map: peta,
                          icon: gambar_marker,
                          clickable: true
                      });


                  markers.push(marker);
                  info.push(infowindow);


                  google.maps.event.addListener(markers['.$i.'], "click", function() {
                    info['.$i.'].open(peta,markers['.$i.']); 
                  });
               ';
            $i++;   
      }

      // echo JS
      echo $js;
      ?>
  }

Any solution to make InfoWindow auto close when another marker clicked? 点击另一个标记时,任何使InfoWindow自动关闭的解决方案? I have tried several solutions on Stackoverflow and Google but didn't worked. 我在Stackoverflow和Google上尝试了几种解决方案,但没有奏效。 Thanks for your kindly help :) 谢谢你的帮助:)

You could declare just one infowindow, outside of your loop. 您可以在循环之外声明一个infowindow。 Then each marker, when clicked, opens that infowindow on itself, filling it with the proper contents. 然后,每个标记在单击时打开该信息本身,并用适当的内容填充它。

In the following example I set the contentString property on the marker itself. 在下面的示例中,我在标记本身上设置了contentString属性。

var infowindow = new google.maps.InfoWindow();

<?php
foreach ($query->result() as $value) {
....
?>


    var marker = new google.maps.Marker({
                   position: point,
                   map: peta,
                   icon: gambar_marker,
                   clickable: true,
                   content: contentString
                 });


    markers.push(marker);

    google.maps.event.addListener(marker, "click", function() {
        infowindow.setContent(marker.content);
        infowindow.open(peta,marker); 
    });

<?php
  }
?>

Maybe this link will be of use to you? 也许这个链接会对你有用吗?

Previously answered: Close infowindow when another marker is clicked 之前的回答: 单击另一个标记时关闭infowindow

Make sure to declare the global variable outside the main google maps code in your JS: 确保在JS中的主谷歌地图代码之外声明全局变量:

file: var lastOpenedInfoWindow;

Good luck! 祝好运!

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

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