Google maps 谷歌地图-单击任何标记后,当前位置将消失

Google maps 谷歌地图-单击任何标记后,当前位置将消失,google-maps,Google Maps,我有一个问题,正如标题所说,基本上情况是我使用xml文件(来自数据库)填充了一些位置 因此,当我点击任何标记时,我的当前位置标记消失,不知道为什么 这是我的密码: function initMap() { var map = new google.maps.Map(document.getElementById('map'), { center: {lat: -34.397, lng: 150.644}, zoom: 12 }); var infoWindow = new googl

我有一个问题,正如标题所说,基本上情况是我使用xml文件(来自数据库)填充了一些位置

因此,当我点击任何标记时,我的当前位置标记消失,不知道为什么

这是我的密码:

function initMap() {


var map = new google.maps.Map(document.getElementById('map'), {
  center: {lat: -34.397, lng: 150.644},
  zoom: 12
});
var infoWindow = new google.maps.InfoWindow({map: map});



// Try HTML5 geolocation.
if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(function(position) {
    var pos = {
      lat: position.coords.latitude,
      lng: position.coords.longitude
    };

    infoWindow.setPosition(pos);
    infoWindow.setContent('Location found.');
    map.setCenter(pos);
  }, function() {
    handleLocationError(true, infoWindow, map.getCenter());
  });
} else {
  // Browser doesn't support Geolocation
  handleLocationError(false, infoWindow, map.getCenter());
}

  // Change this depending on the name of your PHP or XML file
  downloadUrl('https://www.mysite/dataMaps.php', function(data) {
    var xml = data.responseXML;
    var markers = xml.documentElement.getElementsByTagName('marker');
    Array.prototype.forEach.call(markers, function(markerElem) {
      var name = markerElem.getAttribute('name');
      var address = markerElem.getAttribute('address');
      var type = markerElem.getAttribute('type');
      var point = new google.maps.LatLng(
          parseFloat(markerElem.getAttribute('lat')),
          parseFloat(markerElem.getAttribute('lng')));

      var infowincontent = document.createElement('div');
      var strong = document.createElement('strong');
      strong.textContent = name
      infowincontent.appendChild(strong);
      infowincontent.appendChild(document.createElement('br'));

      var text = document.createElement('text');
      text.textContent = address
      infowincontent.appendChild(text);
      var icon = customLabel[type] || {};
      var marker = new google.maps.Marker({
        map: map,
        position: point,
        label: icon.label
      });
      marker.addListener('click', function() {
        infoWindow.setContent(infowincontent);
        infoWindow.open(map, marker);
        //map.setCenter(marker.getPosition());

      });
    });
  });



}

有什么建议吗?

我明白你的意思,我只是补充了一句

var infoWindow=new google.maps.infoWindow({map:map})

在marker onclick函数中

    marker.addListener('click', function() {
     var infoWindow = new google.maps.InfoWindow({map: map});
            infoWindow.setContent(infowincontent);
            infoWindow.open(map, marker);
var infoWindow=new google.maps.infoWindow({map:map}


现在的问题是,一旦我单击任何标记,它们中任何一个的信息窗口始终存在,因此如果我单击所有标记,现在它们都存在,我需要手动关闭它们

是您的“当前位置”由/在信息窗口中显示?您好,是的,它在第一次出现时显示为:“找到位置”。并且我的标记存在,但是如果我选择由te数据库填充的任何其他标记,我的当前位置(显示为“找到位置”的位置)消失您只有一个信息窗口(它被标记单击功能重用)。如果您想保留原始信息窗口,请创建一个新窗口用于标记(并可能禁用原始窗口上的单击关闭功能…)我已将其包装在一个函数中,但现在我的数据库标记不可用,我无法在此处共享我的代码。但是var map=new google.maps.map(document.getElementById('map'),{and var infoWindow=new google.maps.infoWindow({map:map});和if(navigator.geolocation){添加到函数中