变量范围:在回调函数中看不到

发布于 2024-11-03 17:42:48 字数 2036 浏览 1 评论 0原文

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

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

第一个问题:对于标记,我添加了一个事件侦听器以便能够显示信息窗口。我使用标记的标题来检索朋友的信息...这只是一个愚蠢的解决方法,因为在检索朋友的 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);
          }
        });
      });
    }

知道可能出了什么问题吗?

Basically, I have several friends that I want to:
- see on a map
- list in a table

I have a loop on the friends list, for each iteration I:
- create a google maps marker
- add friend in a list

FIRST PROBLEM: For the marker I add an event listener to be able to display an infowindow. I use the title of the marker to be able to retrieve info on the friend... that's only a silly wokaround because "id" is not known within the ajax method retrieving the friend. This is my first problem. I'd like to be able to use title for a real title (not an id) and manage to get the id within the ajax callback in another way.

SECOND PROBLEM For the list, I add a "li" entry for each friend and add an event listener on the li.a link to be able to display the friend details. "id" is not seen neither.

Those 2 problems are of the same familly.

Below is the code I've done. Obviously something wrong but cannot figure out what I need to change to access id within the callback functions.

note: I use jquery mobile for a HTML5 application.

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);
          }
        });
      });
    }

Any idea of what could be wrong ?

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

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

发布评论

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

评论(3

墨洒年华 2024-11-10 17:42:48
//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);
//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);
杀お生予夺 2024-11-10 17:42:48

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

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

如果你使用,

 document["something"] = id;

你将能够

var something = document["something"];

从任何地方

获取,这并不是说将所有内容都放在文档对象中是一个很好的设计决策,但是让你知道;)

I did not read you whole problem because I need to hurry up but

the document object is always visible in scope

if you use

 document["something"] = id;

you will be able to get the

var something = document["something"];

from anywhere

not that it is a good design decision to have everything in the document object but to let you know ;)

二智少女 2024-11-10 17:42:48

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

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

Is marker not the variable for the google maps marker?

So you should be able to access marker.title to get the id.

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