簡體   English   中英

Google Map Api v3:單擊信息窗口中的按鈕時觸發操作

[英]Google Map Api v3 : fire an action when clicking a button in a infowindow

我使用“右鍵單擊”操作在地圖上添加標記。 每個標記都有其特定的信息窗口,每個信息窗口都具有一個表單。

當我創建多個標記時,我的代碼僅觸發第一個信息窗口值,而不觸發良好的值。

google.maps.event.addListener(map, 'rightclick', function(event) {
  var marker = new google.maps.Marker({
    map: map,
    draggable : true,
    position: event.latLng,
    animation: google.maps.Animation.DROP,
  });

  var geocoder = new google.maps.Geocoder();

  geocoder.geocode({'latLng': event.latLng}, function(results, status) {
    if(status == google.maps.GeocoderStatus.OK) {
      var html = "<div class='infobox'>";
      html += "<p>";
      html += "<strong>Lieu : </strong>";
      html += results[0]['formatted_address'];
      html += "</p>";

      html += "<p>";
      html += "<strong>Nombre de place (~) : </strong>";
      html += "<input type='text' class='form_input' name='nb_place' id='nb_place' />";
      html += "</p>";

      html += "<p>";
      html += "<strong>Point d'arrimage : </strong>";
      html += "<input type='checkbox' id='point_arrim' name='point_arrim' />";
      html += "</p>";

      html += "<input type='hidden' id='formatted_address' name='formatted_address' value='"+results[0]['formatted_address']+"' />";
      html += "<input type='hidden' id='geoloc_lat' name='geoloc_lat' value='"+event.latLng.nb+"' />";
      html += "<input type='hidden' id='geoloc_long' name='geoloc_long' value='"+event.latLng.ob+"' />";
      html += '<input type="button" onclick="saveData()" class="info_button" value="Ajouter l\'emplacement"/>'
      html += "</div>";


      var infowindow = new google.maps.InfoWindow({
        map: map,
        content : html
      });
      infowindow.open(map, marker);

      google.maps.event.addListener(infowindow, 'domready', function(){
        console.log("test");
        console.log($(this));
      });
    }
  });
});

function saveData() {
  var nb_place = escape(document.getElementById("nb_place").value);
  var point_arrim = escape(document.getElementById("point_arrim").checked);
  var formatted_address = document.getElementById("formatted_address").value;
  var geoloc_lat = document.getElementById("geoloc_lat").value;
  var geoloc_long = document.getElementById("geoloc_long").value;

  if(!nb_place) nb_place = null;

  var url = "phpsqlinfo_addrow.php?nb_place=" + nb_place + "&point_arrim=" + point_arrim +
    "&formatted_address=" + formatted_address + "&geoloc_lat=" + geoloc_lat + "&geoloc_long=" + geoloc_long;
  downloadUrl(url, function(data, responseCode) {
    if (responseCode == 200 && data.length <= 1) {
      infowindow.close();
      document.getElementById("message").innerHTML = "Location added.";
    }
  });
}

實際上,我想動態觸發與單擊的信息窗口按鈕相關的值。 我看着“准備就緒”的動作,但無法弄清楚。

使

var marker=[];
var infowindow=[];

在全球范圍內。 並嘗試

google.maps.event.addListener(map, 'rightclick', function(event) {
        marker.push(new google.maps.Marker({position: event.latLng, map: map}));
        var markIndex = marker.length-1;
    .......................
   ..........................
   infowindow[markIndex] = new google.maps.InfoWindow({
          content: html
   });
   infowindow[markIndex].open(map,marker[markIndex]);

  //You can also add the info window again while clicking on marker if you need

  google.maps.event.addListener(marker[markIndex], 'click', function(e) {
            infowindow[markIndex].open(map,marker[markIndex]);
  }); 
});

另外,在創建html varibale時,向其中添加infowindow索引

html += '<input type="button" onclick="saveData('+markIndex+')" class="info_button" value="Ajouter l\'emplacement"/>';

現在我們可以在saveData函數中關閉特定的信息窗口

function saveData(index) {
........
 if (responseCode == 200 && data.length <= 1) {
          infowindow[index].close();
          document.getElementById("message").innerHTML = "Location added.";
        }
}

您可以在此處查看用於創建多個信息窗口的工作演示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM