Adding property:value pairs from GooglemapsAPI to an existing json/javascript object

Posted by Rockinelle on Stack Overflow See other posts from Stack Overflow or by Rockinelle
Published on 2010-04-06T20:41:54Z Indexed on 2010/04/06 20:53 UTC
Read the original article Hit count: 170

Filed under:
|
|

I am trying to create a script that finds the closest store/location to a customer using googlemapsAPI. So I've got a json object which is a collection of store objects. Using Jquery's .each to iterate through that object, I am grabbing the driving time from the customer to each store. If it finds the directions, It copies the duration of the drive which is an object with the time in seconds and a human readable value. That appears to work, however when I try to sort through all of those store objects with the drivetime added, I cannot read the object copied from google. If I console.log the whole object, 'closest_store', It shows the values I'm looking for. When I try to read the values directly via closest_store.driveTime, Firebug is outputting undefined in the console. What am I missing here?

$.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);
            }
        )



        });

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about googlemaps