如何在谷歌地图的infoWindow

时间:2017-12-07 18:41:48

标签: javascript google-maps-api-3

sample_id显示在infoWidow中,但如何在href标记内传递?

(function (marker, sample) {
    google.maps.event.addListener(marker, "mouseover", function (e) {
    infoWindow.setContent(sample.id+ ' - ' + sample.name + '</br>' + 
    '<a href="SampleDetail.aspx?sample_id="'+ sample.id + '> Detail...</a>' );
    infoWindow.open(map, marker); 
    });
})(marker, sample);

2 个答案:

答案 0 :(得分:1)

您遇到不匹配的字符串引号问题:

 infoWindow.setContent(sample.id+ ' - ' + sample.name + '</br>' + 
'<a href="SampleDetail.aspx?sample_id="'+ sample.id + '> Detail...</a>' );
                                      ^
                                      |
                                     this
infoWindow.setContent(sample.id+ ' - ' + sample.name + '</br>' + 
'<a href="SampleDetail.aspx?sample_id='+ sample.id + '"> Detail...</a>' );
                                                      ^
                                                      |
                                         should be here

这有效:

(function(marker, sample) {
    google.maps.event.addListener(marker, "mouseover", function(e) {
      infoWindow.setContent(sample.id + ' - ' + sample.name + '</br>' +
        '<a href="SampleDetail.aspx?sample_id=' + sample.id + '"> Detail...</a>');
      infoWindow.open(map, marker);
    });
  })(marker, sample);

proof of concept fiddle

代码段

function initialize() {
  var map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
    var infoWindow = new google.maps.InfoWindow();
  var marker = new google.maps.Marker({
    position: map.getCenter(),
    map: map
  });
  var sample = {
    id: 5,
    name: "frank"
  };
  (function(marker, sample) {
    google.maps.event.addListener(marker, "mouseover", function(e) {
      infoWindow.setContent(sample.id + ' - ' + sample.name + '</br>' +
        '<a href="SampleDetail.aspx?sample_id=' + sample.id + '"> Detail...</a>');
      infoWindow.open(map, marker);
    });
  })(marker, sample);
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas"></div>

答案 1 :(得分:0)

尝试这样的事情:

"<a href='SampleDetail.aspx?sample_id=" + sample.id + "'> Detail...</a>"