javascript - 谷歌地图 : How to display infowindows when you click on markers

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

我是 Google Maps API 的新手,我认为我缺少一些基本的东西。我正在从 JSON 文件加载位置并在 map 上创建标记。我希望能够单击标记并显示信息窗口。现在,当我单击标记时,不会显示任何内容。

<!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>

最佳答案

friend 帮我解决了这个问题,这段代码有效:

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">

关于javascript - 谷歌地图 : How to display infowindows when you click on markers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40701178/

相关文章:

google-maps-api-3 - 是否可以更改Google Maps Drawing Manager图标?

javascript - google.maps.MapOptions + JSON : avoid eval()?

javascript - 在主干模型/ View 上处理更复杂的验证逻辑(必填字段等)的最佳方法?

javascript - Uncaught Error : connect ETIMEDOUT (net. 套接字)

javascript - 我如何监听 jQuery daterangepicker 中的开始日期变化

python - 如何将 Google map 嵌入到 wxPython 程序中?

javascript - Vuejs 2使用事件从事件总线接收到的数据将数据分配给变量

javascript - 将参数传递给回调函数

ios - 在用户点击 Google Maps iOS 应用程序的任何地方添加标记?

javascript - 通过 AJAX 加载 Google Maps API,控制台错误