javascript哈希表问题
我试图通过迭代列表并检索一些信息来向谷歌地图添加标记。我正在使用原型库。代码如下:
var point = new Array();
var myMarkerOptions = new Array();
var marker = new Array();
recommendedList.each(function(item){
point[item.location.ID] = new google.maps.LatLng(item.location.lat, item.location.lng);
myMarkerOptions[item.location.ID] = {
position: point[item.location.ID],
map: map
};
marker[item.location.ID] = new google.maps.Marker(myMarkerOption[item.location.ID]);
});
其中推荐列表是以下形式的 JSON 响应:
[
{"artist":"artist1","location":{"lat":"50.952226","lng":"5.34832","ID":28}},
{"artist":"artist2","location":{"lat":"52.362287","lng":"4.883965","ID":32}},
...
]
但是,这不起作用。 我知道问题与 JSON 或谷歌地图无关,因为我尝试了使用以下代码的更简单版本并且它有效:
var myLatlng = new google.maps.LatLng(recommendedList[0].location.lat,recommendedList[0].location.lng);
var marker = new google.maps.Marker({
position: myLatlng,
map: map
});
所以问题一定出在迭代和哈希映射中。 任何人都可以看到问题吗?谢谢!
I'm trying to add markers to a google maps by iterating through a list and retrieving some information. I'm using the prototype library. The code is the following:
var point = new Array();
var myMarkerOptions = new Array();
var marker = new Array();
recommendedList.each(function(item){
point[item.location.ID] = new google.maps.LatLng(item.location.lat, item.location.lng);
myMarkerOptions[item.location.ID] = {
position: point[item.location.ID],
map: map
};
marker[item.location.ID] = new google.maps.Marker(myMarkerOption[item.location.ID]);
});
where the recommendedList is a JSON response of the form:
[
{"artist":"artist1","location":{"lat":"50.952226","lng":"5.34832","ID":28}},
{"artist":"artist2","location":{"lat":"52.362287","lng":"4.883965","ID":32}},
...
]
However, this is not working.
I know that the problem is not about the JSON or the google map, because I tried a simpler version with the following code and it worked:
var myLatlng = new google.maps.LatLng(recommendedList[0].location.lat,recommendedList[0].location.lng);
var marker = new google.maps.Marker({
position: myLatlng,
map: map
});
So the problem must be in the iteration and the hash maps.
Anyone can see the problem? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
足够简单,无需 .each 即可进行测试
您还可以大大简化它,除非您“需要”那些其他数组:
Simple enough to test without the .each
You can also simplify this quite a lot, unless you "need" those other arrays:
我认为这可能是您正在使用的数组访问器。使用完
point
、myMarkerOptions
和marker
数组后,您将如何处理它们?您可以尝试将它们声明为吗?
这会让您将它们称为
point[item.location.ID]
(等等)。我认为,由于item.ID
属性是一个数字,JS 会尝试设置数组的数字索引,而不是在哈希中创建新属性。I think it could be the array accessors you're using. What are you doing the
point
,myMarkerOptions
andmarker
arrays once you're done with them?Can you try declaring them as
That'll let you refer to them as
point[item.location.ID]
(etc). I think with theitem.ID
property being a number, JS is trying to set that numeric index of the array, rather than creating a new property in your hash.每个迭代器的原型不支持 JSON 或对象数据类型。
Prototype each iterator doesn't support JSON or object data type.