简体   繁体   English

将信息窗口添加到Google地图

[英]adding infowindow to google maps

I'm trying to add some infowindow content to my markers on a google map. 我正在尝试向Google地图上的标记添加一些信息窗口内容。 I can query my server, get some data, put the markers on the map. 我可以查询服务器,获取一些数据,将标记放在地图上。 That works. 这样可行。 What doesn't work is that nothing happens when I click on the marker. 不起作用的是,当我单击标记时什么也没有发生。 I would think that the infowindow would popup. 我认为信息窗口会弹出。 Unfortunately, it has been so long since I have done google maps programming, I am effectively starting over. 不幸的是,距离我完成Google Maps编程已有很长时间了,我实际上是从头开始。 For some reason, the marker's click event is not being called. 由于某种原因,未调用标记的click事件。 Any suggestions regarding my dumbness are appreciated. 关于我的愚蠢的任何建议表示赞赏。 TIA TIA

<script>
    var map, geocoder;
    var Markers = [];
    function initMap() {
        map = new google.maps.Map(document.getElementById('map'), {
            center: { lat: 0.0, lng: 0.0 },
            zoom: 12
        });
        if (!Modernizr.geolocation) {
            alert("Your browser sucks. Get a new one, maybe one that is up to date and supports GPS.")
            return;
        }
        else {
            navigator.geolocation.getCurrentPosition(show_map);
        }
    }
    function show_map(position) {
        map.setZoom(12);
        var Latitude = position.coords.latitude;
        var Longitude = position.coords.longitude;
        map.setCenter({ lat: Latitude, lng: Longitude });
        var bounds = map.getBounds();
        var url = "/api/xxxxxxxxjsonendpoint";
        var lowerLeft = bounds.getSouthWest();
        var upperRight = bounds.getNorthEast();
        var lat0 = lowerLeft.lat();
        var lng0 = lowerLeft.lng();
        var lat1 = upperRight.lat();
        var lng1 = upperRight.lng();
        var geocoder = new google.maps.Geocoder();
        var data = { LowerLeftLat: lat0, LowerLeftLng: lng0, UpperRightLat: lat1, UpperRightLng: lng1 };
        $.get(url, data, function (result) {
            for (var i = 0; i < result.length; i++) {
                var address = result[i].Address1 + " " + (result[i].Address2 != null ? result[i].Address2 : "") + " " + result[i].City + " " + result[i].Province + " " + result[i].PostalCode + " " + result[i].Country;
                var marker = new google.maps.Marker({
                    position: geocodeAddress(geocoder, map, address),
                    map: map,
                    title: address
                });
                var infowindow = new google.maps.InfoWindow({
                    content: i
                });
                makeInfoWindowEvent(map, infowindow, "test" + i, marker);
            }
        });
    }
    function geocodeAddress(geocoder, resultsMap, address) {
        geocoder.geocode({ 'address': address }, function (results, status) {
            if (status === 'OK') {
                resultsMap.setCenter(results[0].geometry.location);
                var marker = new google.maps.Marker({
                    map: resultsMap,
                    position: results[0].geometry.location
                });
            } else {
                alert('Geocode was not successful for the following reason: ' + status);
            }
        });
    }
    function makeInfoWindowEvent(map, infowindow, contentString, marker) {
        google.maps.event.addListener(marker, 'click', function () {
            infowindow.setContent(contentString);
            infowindow.open(map, marker);
        });
    }
</script>

Here is the most recent update of my code. 这是我的代码的最新更新。 Still no worky........ 还是没用........

<script>
    var map, geocoder;
    var Markers = [];
    var infowindow;
    function initMap() {
        map = new google.maps.Map(document.getElementById('map'), {
            center: { lat: 0.0, lng: 0.0 },
            zoom: 12
        });
        infowindow = new google.maps.InfoWindow();
        if (!Modernizr.geolocation) {
            alert("Your browser sucks. Get a new one, maybe one that is up to date and supports GPS.")
            return;
        }
        else {
            navigator.geolocation.getCurrentPosition(show_map);
        }
    }
    function show_map(position) {
        map.setZoom(12);
        var Latitude = position.coords.latitude;
        var Longitude = position.coords.longitude;
        map.setCenter({ lat: Latitude, lng: Longitude });
        var bounds = map.getBounds();
        var url = "/api/xxxxxxx/yyyyyyyyyy";
        var lowerLeft = bounds.getSouthWest();
        var upperRight = bounds.getNorthEast();
        var lat0 = lowerLeft.lat();
        var lng0 = lowerLeft.lng();
        var lat1 = upperRight.lat();
        var lng1 = upperRight.lng();
        var geocoder = new google.maps.Geocoder();
        var data = { LowerLeftLat: lat0, LowerLeftLng: lng0, UpperRightLat: lat1, UpperRightLng: lng1 };
        $.get(url, data, function (result) {
            for (var i = 0; i < result.length; i++) {
                var address = result[i].Address1 + " " +
                    (result[i].Address2 != null ? result[i].Address2 : "") +
                    " " + result[i].City + " " + result[i].Province + " " +
                    result[i].PostalCode + " " + result[i].Country;
                var marker = new google.maps.Marker({
                    position: geocodeAddress(geocoder, map, address),
                    map: map,
                    title: address,
                    content: address
                });

                makeInfoWindowEvent(infowindow, "test" + i, marker);
            }
        });
    }
    function geocodeAddress(geocoder, resultsMap, address) {
        geocoder.geocode({ 'address': address }, function (results, status) {
            if (status === 'OK') {
                resultsMap.setCenter(results[0].geometry.location);
                var marker = new google.maps.Marker({
                    map: resultsMap,
                    position: results[0].geometry.location
                });
            } else {
                alert('Geocode was not successful for the following reason: ' + status);
            }
        });
    }
    function makeInfoWindowEvent(infowindow, contentString, marker) {
        (function (zinfowindow, zcontentString, zmarker) {
            zinfowindow.setContent(zcontentString);
            google.maps.event.addListener(zmarker, 'click', function () {
                zinfowindow.open(map, zmarker);
            });
        })(infowindow, contentString, marker);
    }
</script>
function makeInfoWindowEvent(map, infowindow, contentString, marker) {
    infowindow.setContent(contentString);
    google.maps.event.addListener(marker, 'click', function () {
        infowindow.open(map, marker);
    });
}

Your code crash because when the listener is calling, the value of marker and infowindow have already changed. 您的代码崩溃,因为在调用侦听器时, markerinfowindow的值已经更改。 You can try something like this (just change the makeInfoWindowEvent function): 您可以尝试执行以下操作(只需更改makeInfoWindowEvent函数):

function makeInfoWindowEvent(map, infowindow, contentString, marker) {
    google.maps.event.addListener(marker, 'click', function () {
        infowindow.setContent(contentString);
        infowindow.open(map, marker);
        console.log (contentString);
        console.log (marker);
    });
}

Normally, the output will be always the same for contentString and marker . 通常, contentStringmarker的输出将始终相同。

In order to pass the real value of the marker , contentString and infowindow , you have to create an IIFE . 为了通过实际价值markercontentStringinfowindow ,你必须创建一个IIFE Like this, the value of the variables will be copy inside the function: 这样,变量的值将被复制到函数内部:

function makeInfoWindowEvent(map, infowindow, contentString, marker) {
    (function (zinfowindow, zcontentString, zmarker) {
       zinfowindow.setContent(zcontentString);
       google.maps.event.addListener(zmarker, 'click', function () {
         zinfowindow.open(map, zmarker);
       });
    })(infowindow, contentString, marker);
}

However, you do not need to pass map as parameter because the value of map is always the same. 但是,您不需要将map作为参数传递,因为map的值始终相同。

Tell me if you have some questions. 告诉我您是否有疑问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM