【问题标题】:Iterate over json to add marker遍历 json 以添加标记
【发布时间】:2014-12-08 20:50:07
【问题描述】:

我被这个问题困住了。我认为答案很简单,但我没有找到它...... 我想遍历 json 以在我的地图上添加标记。如何选择 value.lat 或 value.lng。 谢谢你的帮助。

[{
id: "1",
lat: "48.901196",
lng: "2.334080"
},
{
id: "2",
lat: "48.847008",
lng: "2.301430"
}]

 $.each(json, function (i, item) {
            $.each(item, function (key, value) {
                console.log(value.lat); //return undefined
                var myLatlng = new google.maps.LatLng(value.lat, value.lng);

                 new google.maps.Marker({
                    position: myLatlng,
                    map: map,
                    title: value.id
                });

            });
        });

【问题讨论】:

    标签: javascript jquery google-maps-api-3


    【解决方案1】:

    就用这个

     $.each(json, function (i, item) {
         alert(item.lat);
    ......
    

    【讨论】:

      【解决方案2】:

      摆脱内部$.each 循环,只需从item 中获取值,它引用数组中的当前对象。

      // ------------------------v------- this holds each object in the array
      $.each(json, function (i, item) {
          var myLatlng = new google.maps.LatLng(item.lat, item.lng);
      
           new google.maps.Marker({
              position: myLatlng,
              map: map,
              title: item.id
          });
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-12-27
        • 1970-01-01
        • 2015-11-16
        • 1970-01-01
        • 2012-11-19
        • 1970-01-01
        • 2018-11-03
        • 1970-01-01
        相关资源
        最近更新 更多