Google Maps change content of infowindow

前提是你 提交于 2019-12-23 06:10:05

问题


I have a bunch of markers with infowindows in my google maps. Initially I set the content to something with a <input type="text" ...> and a submit button. On the onclick="return submitForm();" of the submit button I call a function which should change the content of the infowindow which it does. But when I close the infowindow and open it again, the text is being reset. How can I make it stay? infowindow.setContent("This HTML content is being reset after reopening the infowindow");

infowindow.open(map, marker);

Thanks a lot!

_

Edit:

Initialization:

function addCoordinate(lat, lon, text){
    var marker = new google.maps.Marker({
        position: (new google.maps.LatLng(lat, lon)),
        title: '#' + path.getLength(),
        map: map,
        icon: image3
    });

    google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent('<input type="text" id="wpname" value="'+ name +'" style="width:200px"><input type="hidden" id="wplat" value="'+ lat +'"><input type="hidden" id="wplon" value="'+ lon +'"><input type="submit" value="OK" onclick="return submitForm();"><br>'+ text +'<br><a href="javascript:removeCoordinate('+lat+', '+lon+');">Remove</a>');
        infowindow.open(marker.get('map'), marker);
    });
}

Function to be called:

function submitForm() {
    infowindow.setContent("This HTML content is being reset after reopening the infowindow");
    infowindow.open(map, marker);
}

Whenever I click the submit button the content should change from the form to "This HTML content is being reset [...]". When I close the infowindow and open it again, there should still be the message "This HTML content is being reset [...]".

Right now it is being set back to the form with the submit button.


回答1:


You must store the content in a way that gives you a relation to the clicked marker. The best way would be to store it directly as a property of the marker(and update this marker-property in submitForm)

function initialize() {

  var goo = google.maps,
    map = new goo.Map(document.getElementById("map-canvas"), {
      center: new goo.LatLng(-34.397, 150.644),
      zoom: 8
    }),

    infowindow = new google.maps.InfoWindow;

  function addCoordinate(lat, lon, text) {
    var node = document.createElement('div'),
      marker = new goo.Marker({
        position: (new goo.LatLng(lat, lon)),

        map: map,
        content: node
      });
    node.innerHTML =
      '<input type="text" id="wpname" value="' + name + '" style="width:200px">' +
      '<input type="hidden" id="wplat" value="' + lat + '">' +
      '<input type="hidden" id="wplon" value="' + lon + '">' +
      '<input type="submit" value="OK" ><br>' + text +
      '<br><a href="#">Remove</a>';

    function submitForm() {
      marker.set('content', 'This marker has been stored');
      goo.event.trigger(marker, 'click')
    }

    function removeCoordinate() {
      marker.setMap(null);
    }

    goo.event.addDomListener(node.querySelector('input[type=submit]'), 'click', submitForm)
    goo.event.addDomListener(node.querySelector('a'), 'click', removeCoordinate)


    goo.event.addListener(marker, 'click', function() {
      infowindow.setContent(this.get('content'));
      infowindow.open(marker.get('map'), marker);
    });
  }
  goo.event.addListener(map, 'click', function(e) {
    addCoordinate(e.latLng.lat(), e.latLng.lng(), 'some text');
  });

  window['alert']('click on the map to add a marker')

}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map-canvas {
  height: 100%;
  margin: 0;
  padding: 0
}
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3"></script>

<div id="map-canvas"></div>


来源:https://stackoverflow.com/questions/31494380/google-maps-change-content-of-infowindow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!