Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 704255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:55:18+00:00 2026-05-14T03:55:18+00:00

I am trying to create a script that finds the closest store/location to a

  • 0

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



        });
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:55:19+00:00Added an answer on May 14, 2026 at 3:55 am

    In your second $.each, I’m not sure what value is that the code is referencing. Try this approach instead:

    Do this to set the value:

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

    Use this for your bottom loop:

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

    Update to fix async issue:

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

    This counts up before sending a request, and as each request comes back, counts down…when we get back to 0 (last request coming back) it then runs the function that calculates the closest store.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create system users with a php script securely, In that, I'd
I'm trying to create a build script for my current project, which includes an
What I'm trying to do is create a PowerShell script which gets a list
I'm trying to learn bash string handling. How do I create a bash script
I am trying create a WCF service that leverages the WPF MediaPlayer on the
Trying to create a user account in a test. But getting a Object reference
Trying to create my first iPhone app that would play back audio. When I
Trying to create several layers of folders at once C:\pie\applepie\recipies\ without using several different
Trying to create a small monitor application that displays current internet usage as percentage
I am trying to create a jquery accordion that fades the header of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.