javascript哈希表问题

发布于 2024-11-24 23:12:43 字数 1127 浏览 0 评论 0原文

我试图通过迭代列表并检索一些信息来向谷歌地图添加标记。我正在使用原型库。代码如下:

    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 技术交流群。

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

发布评论

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

评论(3

葬花如无物 2024-12-01 23:12:43

足够简单,无需 .each 即可进行测试

for (var i=0, item; item = recommendedList[i]; i++) {
    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]);    
}

您还可以大大简化它,除非您“需要”那些其他数组:

for (var i=0, item; item = recommendedList[i]; i++) {
    marker[item.location.ID] = new google.maps.Marker({
      new google.maps.LatLng(item.location.lat, item.location.lng),
      map
    });
}

Simple enough to test without the .each

for (var i=0, item; item = recommendedList[i]; i++) {
    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]);    
}

You can also simplify this quite a lot, unless you "need" those other arrays:

for (var i=0, item; item = recommendedList[i]; i++) {
    marker[item.location.ID] = new google.maps.Marker({
      new google.maps.LatLng(item.location.lat, item.location.lng),
      map
    });
}
闻呓 2024-12-01 23:12:43

我认为这可能是您正在使用的数组访问器。使用完 pointmyMarkerOptionsmarker 数组后,您将如何处理它们?

您可以尝试将它们声明为吗?

var point = {};
var myMarkerOptions = {};
var 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 and marker arrays once you're done with them?

Can you try declaring them as

var point = {};
var myMarkerOptions = {};
var marker = {};

That'll let you refer to them as point[item.location.ID] (etc). I think with the item.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.

梦过后 2024-12-01 23:12:43

每个迭代器的原型不支持 JSON 或对象数据类型。

Prototype each iterator doesn't support JSON or object data type.

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