Google地图:如何在单击标记时显示信息窗口

时间:2016-11-20 06:00:25

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

我是谷歌地图API的新手,并认为我遗漏了一些基本的东西。我从JSON文件加载位置并在地图上创建标记。我希望能够点击标记并显示信息窗口。现在,当我点击标记时,不会显示任何内容。

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
<title>ListingCharts3</title>
<style>
    /* Always set the map height explicitly to define the size of the div
    * element that contains the map. */
    #map {
        height: 100%;
    }
    /* Optional: Makes the sample page fill the window. */
    html, body {
        height: 100%;
        margin: 0;
        padding: 0;
    }
</style>
</head>
<body>
<div id="map"></div>

<script>
    var map;
    var infowindow;

    function initMap() {
        map = new google.maps.Map(document.getElementById('map'),
        {
            zoom: 4,
            center: new google.maps.LatLng(41, -96),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        });

        map.data.loadGeoJson('https://DATA_FILE_URL.json');
    }

    // Loop through the results array and place a marker for each set of coordinates.
    window.eqfeed_callback = function (results) {
        for (var i = 0; i < results.features.length; i++) {
            var coords = results.features[i].geometry.coordinates;
            var latLng = new google.maps.LatLng(coords[1], coords[0]);
            var marker = new google.maps.Marker
            ({
                position: latLng,
                map: map,
                title: results.features[i].address
            });

            //var content = results.features[i].address;
            var content = "foo";

            var infowindow = new google.maps.InfoWindow()

            google.maps.event.addListener(marker, 'click', (function (marker, content, infowindow) {
                return function () {
                    infowindow.setContent(content);
                    infowindow.open(map, marker);
                };
            })(marker, content, infowindow));
        }
    }
</script>

<script async defer
        src="https://maps.googleapis.com/maps/api/js?key=MY_API_KEY&callback=initMap">
</script>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

一位朋友帮我解决了,这段代码有效:

var map;

function initMap() {
  map = new google.maps.Map(document.getElementById('map'), {
    zoom: 4,
    center: new google.maps.LatLng(41, -96),
    mapTypeId: google.maps.MapTypeId.ROADMAP
  });

    var infowindow = new google.maps.InfoWindow();

  map.data.loadGeoJson('https://JSON_FILE_URL.json', null, function (features) {

  map.data.addListener('click', function(event) {
  console.log(event);
      var myHTML =  event.feature.getProperty("address") + ", " + 
                                event.feature.getProperty("state") + ", " +
                    event.feature.getProperty("zipcode");
      infowindow.setContent("<div style='width:auto; text-align: center;'>" + myHTML + "</div>");
      infowindow.setPosition(event.feature.getGeometry().get());
      infowindow.setOptions({pixelOffset: new google.maps.Size(0,-30)});
      infowindow.open(map);
  });
 });
}

<div id="map"></div>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=MY_API_KEY&callback=initMap">

答案 1 :(得分:0)

我可以看到您加载GeoJSON数据并使用Google地图JavaScript API的data layer。我想知道为什么不将事件用于数据层,例如:

map.data.addListener('click', function(event) {
    if (event.feature.getGeometry().getType() === 'Point') {
        //Here your stuff for info window
        var content = "foo";
        var infowindow = new google.maps.InfoWindow();
        infowindow.setContent(content);
        infowindow.setPosition(event.feature.getGeometry().get());
        infowindow.open(map);
    }
}