Google地图api

时间:2016-01-05 10:36:15

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

我正在尝试为我的网站编写一个功能,允许用户点击谷歌地图,会出现一个标记。当标记出现时,用户将点击所述标记并能够填写一些信息。我有这个工作。

但是,当用户点击另一个标记进行编辑时,前一个标记的窗口不再打开。如何编辑信息窗口,保存,然后编辑另一个信息窗口,并能够在任何时候点击打开信息窗口?

这是我的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() {}

1 个答案:

答案 0 :(得分:0)

您应该将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);
        });
    });
}