簡體   English   中英

附加到Google Maps API上各個標記的唯一信息框

[英]Unique Info boxes attached to individual markers on google maps api

我正在嘗試為我的網站編寫一個功能,該功能將允許用戶在google地圖上單擊,然后會出現一個標記。 當標記出現時,用戶將單擊所述標記並能夠填寫一些信息。 我有這個工作。

但是,當用戶單擊另一個標記進行編輯時,先前標記的窗口不再打開。 如何編輯信息窗口,保存它,然后編輯另一個信息窗口,並能夠隨時單擊打開信息窗口?

這是我的JS代碼:

function initialize() {
  var latlng = new google.maps.LatLng(54.5, -7.0);
  var options = {
    zoom: 7,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  var map = new google.maps.Map(document.getElementById("map"), options);
  var html = "<table>" +
    "<tr><td>Country:</td> <td><input type='text' id='country'/> </td> </tr>" +
    "<tr><td>City:</td> <td><input type='text' id='city'/></td> </tr>" +
    "<tr><td>Duration:</td> <td><input type='text' id='duration'/></td> </tr>" +
    "<tr><td>Category:</td> <td><select id='category'>" +
    "<option value='city' SELECTED>City Break</option>" +
    "<option value='beach'>Beach Holiday</option>" +
    "<option value='romantic'>Romantic Holiday</option>" +
    "<option value='activity'>Activity Holiday</option>" +
    "<option value='site'>Site Seeing</option>" +
    "</select> </td></tr>" +
    "<tr><td></td><td><input type='button' value='Save & Close' onclick='saveData()'/></td></tr>";
  infowindow = new google.maps.InfoWindow({
    content: html
  });

  google.maps.event.addListener(map, "click", function(event) {
    marker = new google.maps.Marker({
      position: event.latLng,
      map: map
    });
    infowindow.setContent(html);
    google.maps.event.addListener(marker, "click", function() {
      infowindow.open(map, marker);
    });
  });
}

function saveData() {
  var country = escape(document.getElementById("country").value);
  var duration = escape(document.getElementById("duration").value);
  var category = document.getElementById("category").value;
  var latlng = marker.getPosition();

  var url = "phpsqlinfo_addrow.php?country=" + country + "&city" + city + "&duration=" + duration +
    "&category=" + category + "&lat=" + latlng.lat() + "&lng=" + latlng.lng();
  downloadUrl(url, function(data, responseCode) {
    if (responseCode == 200 && data.length >= 1) {
      infowindow.close();
      document.getElementById("message").innerHTML = "Location added.";
    }
  });
}

function downloadUrl(url, callback) {
  var request = window.ActiveXObject ?
    new ActiveXObject('Microsoft.XMLHTTP') :
    new XMLHttpRequest;

  request.onreadystatechange = function() {
    if (request.readyState == 4) {
      request.onreadystatechange = doNothing;
      callback(request.responseText, request.status);
    }
  };

  request.open('GET', url, true);
  request.send(null);
}

function doNothing() {}

您應該將infowindow的初始化放置在addListener內,並為marker和infowindow創建一個局部變量。

google.maps.event.addListener(map, "click", function(event) {
    var marker = new google.maps.Marker({
          position: event.latLng,
          map: map
        });

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

    infowindow.setContent(html);

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

暫無
暫無

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

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