【问题标题】:Variable scope : not seen within callback function变量范围:在回调函数中看不到
【发布时间】:2011-04-27 22:00:15
【问题描述】:

基本上,我有几个想要交的朋友:
- 在地图上查看
- 在表格中列出

我在朋友列表上有一个循环,每次迭代我:
- 创建谷歌地图标记
- 在列表中添加朋友

第一个问题:对于标记,我添加了一个事件侦听器以便能够显示信息窗口。我使用标记的标题来检索朋友的信息......这只是一个愚蠢的解决方法,因为在检索朋友的 ajax 方法中不知道“id”。这是我的第一个问题。我希望能够将 title 用于真实标题(不是 id)并设法以另一种方式在 ajax 回调中获取 id。

第二个问题 对于列表,我为每个朋友添加一个“li”条目,并在 li.a 链接上添加一个事件侦听器,以便能够显示朋友的详细信息。也没有看到“id”。

这两个问题是同族的。

下面是我完成的代码。显然出了点问题,但无法弄清楚我需要更改什么才能在回调函数中访问 id。

注意:我将 jquery mobile 用于 HTML5 应用程序。

for(j=0;j<friends.length;j++){
      var lat = friends[j][1];
      var long = friends[j][2];
      var id = friends[j][3];
      var latLng = new google.maps.LatLng(lat,long);
      var marker = new google.maps.Marker({ position: latLng,
                               map: map,
                               title: id
                              });

      // Add friends to list
      $("#friend_list ul").append('<li><a id="details' + id + '" rel="external" data-transition="slide">' + id + '</a></li>');

      // Add event handler when details:id is clicked
      $('#details' + id).click(function(){
        alert(id + ' clicked');   // DOES NOT WORK AS ID IS NOT KNOWN IN THIS METHOD
      });

      // Add listener on click action for marker created above
      // I PASS THE TITLE OF SELF AS ID IS NOT KNOWN IN THIS METHOD
      // IF I use: url: "/friend/" + id => ONLY THE LAST VALUE OF THE ID IS TAKEN INTO ACCOUNT 
      google.maps.event.addListener(marker, 'click', function() {
        self = this;
        $.ajax({
          url: "/friend/" + self.title,  
          success: function(details){
            var message = details["lastname"] + ' ' + details["firstname"];
            var infowindow = new google.maps.InfoWindow(
              { content: message,
                size: new google.maps.Size(50,50)
              }
            );
            infowindow.open(map,self);
          }
        });
      });
    }

知道什么地方出了问题吗?

【问题讨论】:

    标签: jquery html jquery-mobile


    【解决方案1】:
    //Here is how the ids can be picked off of the list items when clicked:
    //...your..loop
    var id = friends[j][3];
    
    $('#friendlist').append('<li><a href="#" id="details'+ id +'">Friend</a></li>')
    $('#details'+id).click(function() { alert(this.id.replace('details',''))})
    
    //and for the gmarker:
    var latLng = new google.maps.LatLng(lat,long);
    var gmarker = new google.maps.Marker({ position: latLng,title: 'desired title here' });
    
    //then create a secondary id for the marker -- sort of hash?
    //simple case - each marker has unique latlng, so just concat them n do:
    //based on Marino Šimić's trick
    document[''+lat+''+long] = id
    
    GEvent.addListener(gmarker, "click", function(marker, latlng) {
        alert('marker id:'+document[''+latlng.lat()+''+latlng.lng()] ) //your desired id here
    });
    
    //and then add the marker to the map
    map.addOverlay(gmarker);
    

    【讨论】:

    • 太好了,感谢您提供的清单。关于标记的东西,我在上面的评论中添加了你,因为这不是我需要的,因为我想将标题用于 id 以外的其他东西。
    • 在这种情况下为什么不使用 id 作为参数呢?我有错误 GEvent 未定义。
    • 确保正确引用了 google maps api 脚本,即&lt;script src="http://maps.google.com/maps?file=api&amp;amp;v=2&amp;amp;key=abcdefg&amp;sensor=true_or_false" type="text/javascript"&gt;&lt;/script&gt; 在那里。就我而言,我在推断使用 API V.2
    【解决方案2】:

    我没有完整阅读你的问题,因为我需要快点,但是

    文档对象在范围内始终可见

    如果你使用

     document["something"] = id;
    

    您将能够获得

    var something = document["something"];
    

    从任何地方

    将所有内容都包含在文档对象中并不是一个好的设计决策,而是让您知道;)

    【讨论】:

    • @marino-simic,这是个好技巧,我会检查一下。非常感谢。
    • @marino-simic 这也不起作用,只考虑 document["current_id"] 的最后一个值
    【解决方案3】:

    marker 不是谷歌地图标记的变量吗?

    所以你应该可以访问 marker.title 来获取 id。

    【讨论】:

    • @mcnemesis 我确实访问 marker.title 来获取 id,因为我在标记选项中设置了“title: id”。事情是我想将标记的标题用于除 id 之外的其他东西(例如名称)。在标题中设置 id 是我发现以后能够取回 id 的唯一方法,但这不是我需要的。
    • 啊,我明白了。您可能需要编辑您的问题以使其更清楚。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-22
    • 2017-11-12
    • 2012-01-27
    • 1970-01-01
    • 2013-08-07
    • 2014-10-06
    • 1970-01-01
    相关资源
    最近更新 更多