2011-04-27 81 views
0

基本上,我有几个朋友,我想:
- 看地图
上 - 名单表变量范围:回调函数中没有看到

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

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

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

这两个问题是相同的familly。

下面是我所做的代码。显然有些问题,但无法弄清楚我需要在回调函数中更改访问ID。

注意:我使用jQuery的手机为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); 
      } 
     }); 
     }); 
    } 

任何想法可能是错误的?

回答

0

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

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

+0

@mcnemesis我做访问marker.title因为我设置“的称号,以获得ID: ID“标记选项。事情是我想使用标记的标题除了id之外的其他名称(例如名称)。在标题中设置id是我发现能够稍后获取id的唯一方式,但这不是我所需要的。 – Luc 2011-04-27 22:46:23

+0

啊我明白了。你可能想编辑你的问题,使其更清楚。 – dkarzon 2011-04-27 22:51:06

+0

我更新了代码。 – Luc 2011-04-28 12:17:46

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); 
+0

太好了,非常感谢列表内容。关于标记的东西,我在上面提到的评论中添加了你,因为这不是我所需要的,因为我想将标题用于除ID之外的其他内容。 – Luc 2011-04-27 22:48:45

+0

那么,接受一个答案? – nemesisfixx 2011-04-28 07:56:52

+0

谢谢,我没有看到你的更新。 – Luc 2011-04-28 12:19:33

1

,因为我需要抓紧时间,但

文档对象总是处于范围

可见,如果你使用

document["something"] = id; 

你会我没有看到你整个问题能够得到的

var something = document["something"]; 

从任何地方

并不在于它是一个很好的设计决策必须在文档对象,而且让你知道一切;)

+0

@ marino-simic,这是一个很好的技巧,我会检查这个。非常感谢。 – Luc 2011-04-27 22:50:03

+0

@ marino-simic这也不起作用,只考虑文档[“current_id”]的最后一个值 – Luc 2011-04-28 12:17:26