使用ajax v3的谷歌地图信息窗口

发布于 2024-09-30 13:27:46 字数 1932 浏览 5 评论 0原文

我已经差不多明白了,但还不太管用。我试图通过 ajax 加载内容,它确实加载了正确的内容,我可以看到它在 firebug 中加载,但是,它给了我一个错误:infowindow 未定义。我的 load_content 有点不对劲

在 load_content 之前的两行注释掉的行可以加载一个带有一些文本的小窗口。

function initialize() {
  var myLatlng = new google.maps.LatLng(<%= @location_for_map.average(:lat) %>, <%= @location_for_map.average(:lng) %>);
  var myOptions = {
   zoom: 13,
   center: myLatlng,
   mapTypeControl: false,
   navigationControl: true,
   navigationControlOptions: {
    style: google.maps.NavigationControlStyle.SMALL
   },
     mapTypeId: google.maps.MapTypeId.ROADMAP
   }
  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  //var infowindow = new google.maps.InfoWindow();

  setMarkers(map, places);

 }

 function setMarkers(map, locations) {
   // Add markers to the map

  for (var i = 0; i < locations.length; i++) {
      var place = locations[i];
      var myLatLng = new google.maps.LatLng(place.lat, place.lng);
      var marker = new google.maps.Marker({
          position: myLatLng,
          map: map,
          icon: '/images/google_maps/numbers/'+(i+1)+'.png',
    html: place.name,
    id: place.id,
          title: place.name
      });

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

   google.maps.event.addListener(marker, 'click', function() {
     //infowindow.setContent(this.html);
     //infowindow.open(map,this);
     load_content(marker, this.id);

   });
  }
 }

 function load_content(marker, id){
   $.ajax({
     url: '/places/' + id + '.js',
     success: function(data){

       infowindow.setContent(data);
       infowindow.open(map, marker);
     }
   });
 } 

 function loadScript() {
     var script = document.createElement("script");
     script.type = "text/javascript";
     script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=initialize";
     document.body.appendChild(script);
 }

I've almost got it, but it doesn't quite work. I'm trying to load the content via ajax, and it does load the correct content, I can see it loading in firebug however, it gives me an error: infowindow not defined. I've got something out of place with load_content

The two commented out lines before load_content work to load a little window with some text.

function initialize() {
  var myLatlng = new google.maps.LatLng(<%= @location_for_map.average(:lat) %>, <%= @location_for_map.average(:lng) %>);
  var myOptions = {
   zoom: 13,
   center: myLatlng,
   mapTypeControl: false,
   navigationControl: true,
   navigationControlOptions: {
    style: google.maps.NavigationControlStyle.SMALL
   },
     mapTypeId: google.maps.MapTypeId.ROADMAP
   }
  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  //var infowindow = new google.maps.InfoWindow();

  setMarkers(map, places);

 }

 function setMarkers(map, locations) {
   // Add markers to the map

  for (var i = 0; i < locations.length; i++) {
      var place = locations[i];
      var myLatLng = new google.maps.LatLng(place.lat, place.lng);
      var marker = new google.maps.Marker({
          position: myLatLng,
          map: map,
          icon: '/images/google_maps/numbers/'+(i+1)+'.png',
    html: place.name,
    id: place.id,
          title: place.name
      });

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

   google.maps.event.addListener(marker, 'click', function() {
     //infowindow.setContent(this.html);
     //infowindow.open(map,this);
     load_content(marker, this.id);

   });
  }
 }

 function load_content(marker, id){
   $.ajax({
     url: '/places/' + id + '.js',
     success: function(data){

       infowindow.setContent(data);
       infowindow.open(map, marker);
     }
   });
 } 

 function loadScript() {
     var script = document.createElement("script");
     script.type = "text/javascript";
     script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=initialize";
     document.body.appendChild(script);
 }

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

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

发布评论

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

评论(3

陪你搞怪i 2024-10-07 13:27:46

这有效:

function setMarkers(map, locations) {
  // Add markers to the map

    for (var i = 0; i < locations.length; i++) {
        var place = locations[i];
        var myLatLng = new google.maps.LatLng(place.lat, place.lng);
        var marker = new google.maps.Marker({
            position: myLatLng,
            map: map,
            icon: '/images/google_maps/numbers/'+(i+1)+'.png',
            html: place.name,
            id: place.id,
            title: place.name
        });

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

        google.maps.event.addListener(marker, 'click', function() {
          load_content(map,this,infowindow);      
        });
    }
}

function load_content(map,marker,infowindow){
  $.ajax({
    url: '/places/' + marker.id + '.js',
    success: function(data){
      infowindow.setContent(data);
      infowindow.open(map, marker);
    }
  });
}   

This works:

function setMarkers(map, locations) {
  // Add markers to the map

    for (var i = 0; i < locations.length; i++) {
        var place = locations[i];
        var myLatLng = new google.maps.LatLng(place.lat, place.lng);
        var marker = new google.maps.Marker({
            position: myLatLng,
            map: map,
            icon: '/images/google_maps/numbers/'+(i+1)+'.png',
            html: place.name,
            id: place.id,
            title: place.name
        });

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

        google.maps.event.addListener(marker, 'click', function() {
          load_content(map,this,infowindow);      
        });
    }
}

function load_content(map,marker,infowindow){
  $.ajax({
    url: '/places/' + marker.id + '.js',
    success: function(data){
      infowindow.setContent(data);
      infowindow.open(map, marker);
    }
  });
}   
滿滿的愛 2024-10-07 13:27:46

您的 infowindow 声明仅在函数内部具有作用域。尝试声明具有全局作用域的 infowindow 变量。我相信这就是导致“信息窗口未定义”问题的原因。

Your infowindow declaration has scope only inside the function. Try declaring the infowindow variable with global scope. I believe that's what's causing the 'infowindow not defined' problem.

つ可否回来 2024-10-07 13:27:46

将此行移动

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

到任何函数之外,例如紧接在函数initialize(){}之后

Move this line:

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

to be outside any function, eg just after function initialize(){}

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