单击标记谷歌地图时无法弹出信息窗口

发布于 2024-12-15 16:46:02 字数 1329 浏览 2 评论 0 原文

我做错了什么?我基本上是想在单击标记时弹出一个信息窗口,一个非常简单的窗口。标记显示正常,但标记单击事件没有得到响应。我很确定 InfoWindow 代码不在正确的位置..相应标记的地址由 jquery 为每个..

var geocoder;
var map;    
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(39.88445,-86.11084);
var myOptions = {
  zoom: 10,
  center: latlng,
  mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"),
  myOptions);


$('span.Address .ms-rtestate-field').each(function(index) {
    var addy = $(this).text();
    geocoder.geocode( { 'address': addy}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({
            map: map, 
            position: results[0].geometry.location,
            title:addy
        });
        }else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });


    // Creating an InfoWindow          
    var infowindow = new google.maps.InfoWindow({
    content: 'Hello world'
   });

    // Adding a click event to the marker
    google.maps.event.addListener(marker, 'click', function() {
    return function(){
     // Opening the InfoWindow
    infowindow.open(map, marker);
    }
   });

});

What am I doing wrong?? I am basically trying to pop open an info window, a very simple one, on clicking the markers.. The markers are showing up fine but the markers click event is not being responded..I am pretty sure the InfoWindow code is not in the right spot .. the addresses for correspoding markers are being fed in by jquery for each..

var geocoder;
var map;    
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(39.88445,-86.11084);
var myOptions = {
  zoom: 10,
  center: latlng,
  mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"),
  myOptions);


$('span.Address .ms-rtestate-field').each(function(index) {
    var addy = $(this).text();
    geocoder.geocode( { 'address': addy}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({
            map: map, 
            position: results[0].geometry.location,
            title:addy
        });
        }else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });


    // Creating an InfoWindow          
    var infowindow = new google.maps.InfoWindow({
    content: 'Hello world'
   });

    // Adding a click event to the marker
    google.maps.event.addListener(marker, 'click', function() {
    return function(){
     // Opening the InfoWindow
    infowindow.open(map, marker);
    }
   });

});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遥远的绿洲 2024-12-22 16:46:02

您只需要一个 infowindow 实例,然后在 onclick 事件期间使用 setContent() 方法更改内容。此外,您应该将事件处理程序放入地理编码回调函数中,以确保在地理编码完成时它能够连接。

试试这个:

// Creating an InfoWindow          
var infowindow = new google.maps.InfoWindow({});

$('span.Address .ms-rtestate-field').each(function(index) {
    var addy = $(this).text();
    geocoder.geocode( { 'address': addy}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);

            var marker = new google.maps.Marker({
                map: map, 
                position: results[0].geometry.location,
                title:addy
            });

            // Adding a click event to the marker
            google.maps.event.addListener(marker, 'click', function() {
                infowindow.setContent("Hello World!");
                infowindow.open(map, this);
            }); 

        }else {
            alert("Geocode was not successful for the following reason: "
                  + status);
        }
    });

});

You only need one instance of the infowindow and then change the content during the onclick event using the setContent() method. Also, you should put the event handler in the geocode callback function to make sure it gets connected when the geocoder completes.

Try this:

// Creating an InfoWindow          
var infowindow = new google.maps.InfoWindow({});

$('span.Address .ms-rtestate-field').each(function(index) {
    var addy = $(this).text();
    geocoder.geocode( { 'address': addy}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);

            var marker = new google.maps.Marker({
                map: map, 
                position: results[0].geometry.location,
                title:addy
            });

            // Adding a click event to the marker
            google.maps.event.addListener(marker, 'click', function() {
                infowindow.setContent("Hello World!");
                infowindow.open(map, this);
            }); 

        }else {
            alert("Geocode was not successful for the following reason: "
                  + status);
        }
    });

});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文