添加属性:从GooglemapsAPI到现有json / javascript对象的值对

时间:2010-04-06 20:41:55

标签: jquery json google-maps

我正在尝试创建一个脚本,使用googlemapsAPI查找到客户最近的商店/位置。所以我有一个json对象,它是商店对象的集合。使用Jquery的.each迭代该对象,我抓住了从客户到每个商店的驾驶时间。如果找到方向,它会复制作为对象的驱动器的持续时间,以秒为单位的时间和人类可读的值。这似乎有效,但是当我尝试使用添加的驱动时间对所有这些商店对象进行排序时,我无法读取从谷歌复制的对象。如果我console.log整个对象'nearest_store',它显示我正在寻找的值。当我尝试通过nearest_store.driveTime直接读取值时,Firebug在控制台中输出undefined。我在这里缺少什么?

$.getJSON('<?php echo Url::base() ?>oldservices/get_locations', {}, function(data){
            $.each(data, function(index, value)
            {
                var end = data[index].address;

                var request = {
                    origin: start,
                    destination: end,
                    travelMode: google.maps.DirectionsTravelMode.DRIVING
                };

                directionsService.route(request, function(response, status)
                {
                    //console.log(response);
                    if (status == google.maps.DirectionsStatus.OK)
                    {
                        value.driveTime = response.trips[0].routes[0].duration.value;
                        //console.log(response.trips[0].routes[0].duration.value);
                    };
                });


            });


            var closest_store;
            $.each(data, function(index, current_store)
            {
                if (index == 0)
                {
                    closest_store = current_store;
                }
                else if (current_store.driveTime.value < closest_store.driveTime.value)
                {
                    closest_store = value
                };
                console.log(current_store.driveTime);
            }
        )



        });

2 个答案:

答案 0 :(得分:0)

为什么不使用value.address而不是data[index].address

答案 1 :(得分:0)

在您的第二个$.each中,我不确定代码引用的value是什么。请尝试这种方法:

执行此操作以设置值:

data[index].driveTime = response.trips[0].routes[0].duration.value;

将此用作底部循环:

var closest_store;
$.each(data, function(index, current_store) {
    if (index == 0) {
        closest_store = current_store;
    } else if (current_store.driveTime.value < closest_store.driveTime.value) {
        closest_store = current_store;
    };
    console.log(current_store.driveTime);
});

更新以解决异步问题:

$.getJSON('<?php echo Url::base() ?>oldservices/get_locations', {}, function(data){
  var count = 0;
  $.each(data, function(index, value) {
    var end = data[index].address;
    var request = { 
          origin: start,
          destination: end,
          travelMode: google.maps.DirectionsTravelMode.DRIVING
        };
    count++;
    directionsService.route(request, function(response, status) {
      count--;
      if (status == google.maps.DirectionsStatus.OK)
        value.driveTime = response.trips[0].routes[0].duration.value;
      if (count == 0)
        getClosestStore(data);
    });
  });
});


function getClosestStore(data) {
  var closest_store = data[0];
  $.each(data, function(index, current_store) {
    if (current_store.driveTime.value < closest_store.driveTime.value)
      closest_store = current_store;
  });
  //do something with closest_store
}

在发送请求之前计数,并且当每个请求返回时,倒计时......当我们回到0(最后一个请求返回)时,它会运行计算最近存储的函数。