Typeerror: Cannot Read Property "Legs" From Undefined

Using the DirectionFinder class of Google Apps script, you get directions betwixt locations from Google Maps. This form returns several details, including travel fourth dimension and altitude betwixt the given two locations. In this tutorial, I will show y'all how to get Google Map Travel Tim and Distance to Google Sheets automatically using Google Apps Script.

You can also develop this type of application with Microsoft Excel or some other programs to Go Google Map travel fourth dimension and distance. You can apply Google Maps APIs for that.

The benefit of using Google Sheets for this is, in one case you lot gear up Triggers for the Google Apps Script, the application runs on Google Servers. So, yous don't need to keep your computer turned on or to use any other servers with this method.

When y'all set time intervals and set Triggers to run this code, it will append the records to the Google Sheets.

How to get Google Map Travel Time and Distance to Google Sail

I volition explain this in ii parts equally below.

  • How to use this Google Canvass to get Travel Fourth dimension and Distance.
  • Explaining the Code

First, I will explicate to you lot how to apply the Google Sheet given beneath to collect Google Travel Time Information. For this part, you do non need any knowledge of coding.

Then in the second function, I will explain the code attached to this Google Sheet briefly. You can modify this lawmaking and go custom results.

How to apply this Google Sheet to get Travel Time and Distance.

Kickoff make a copy of the following Google sheet to your Google Drive.

This Google Sheet contains 3 sheets, namely "inputs", "Outputs" and "SetTriggers". Make a copy of this Google Sail to your Drive.

Google Sheets1
  1. Copy the above Google Sheet to your Google Bulldoze.
  2. Go to Inputs sheet.
  3. Add road description to cavalcade "A"
  4. Add route number in column "B". This volition employ to identify each route y'all divers here in the Outputs sheet.
  5. Add coordinates of Origin and Destination.
    • To go the coordinates, zoom in the Google Map completely and right-click in the specific location on the road, and copy the coordinates.
    • You need to provide latitude and longitude as comma-separated values (Ex: 6.899389906552246,79.92649007087084)
  6. Add together waypoints, if necessary (when y'all provide merely the origin and destination, Google volition provide the road with the least travel time, this may cull alternate routes depending on the traffic condition. If you want the travel fourth dimension beyond a specific route, you can define it using waypoints). Waypoints also should exist added as latitude and longitude as comma-separated values.
  7. To check whether everything is okay, become to the "Google Travel Time" custom menu and click "Run".
  8. Google will ask you to grant access to run the code in your Google Account. Allow Access. Over again click "Run" in the "Google Travel Fourth dimension" custom menu.
  9. Now, get to the "Outputs" sheet. If you have followed the above steps correctly, yous volition see one data is inserted in the Outputs sheet.
  10. To automate the data collection,
    • Go to the canvass SetTriggers, and enter the starting and end hours in 24-hour format in cells B1 and B2, respectively.
    • Enter the running intervals in minutes in cell B6. The running intervals must be one of 1, v, 10, fifteen, or 30.
    • And so go to the custom menu "Google Travel Fourth dimension" and select "Set Triggers".
  11. Cheque the "Output" sheet and you lot tin can see the travel time data is appended to the list in the interval you lot defined in the "SetTriggers" sheet.

Explaining the Code

When y'all click the items in the Custom Menu of the higher up Google Canvas, information technology will run some JavaScript functions attached to information technology. These codes are in a separate application called Google Apps Script.

To admission the Apps Script file, go to "Tools" and select "Script editor" in the to a higher place Google Sheet.

The Code

When you open the script edit as explained to a higher place, you will come across the following code in the Code.gs file.

Class DirectionFinder

You lot tin can go direction information from Google Maps using Google Maps APIs. In Google Apps Script you tin employ the DirectionFinder class to retrieve direction information between two locations.

The class methods, setOrigin, setDestination, setDepart, addWaypoint,getDirections, and setMode, are used in the following code. The getDirections method returns an object which includes the data we seek.

Input data

The "origin" and "destination" parameters are compulsory to remember management data. In addition to that hither, I am using departure fourth dimension, waypoints, and mode to get more specific results. And also I use beginning hr, end our, and running intervals to set the triggers to run the code automatically.

To become the direction details, you need to add these input data in the "Inputs" and "SetTriggers" sheets of the Google Canvas as explained higher up.

These inputs are assigned to variables in the code and you can see them from lines 15-25 of the to a higher place code.

Changing the time zone

In the script I have set up the fourth dimension zone to "GMT+five:30". Change it as appropriately.

Changing the departure time

The script is written to requite real-time data. If you want to become future travel time yous need to provide a hereafter date and time instead of the "now" variable in line 22 of the code. The divergence time cannot exist earlier than the current time. If you provide an earlier fourth dimension Google will return an error.

Adding waypoints

If yous want to define your route more precisely you lot can add together waypoints in between origin and destination. the sample Canvas is designed to provide up to four (4) waypoints. If no waypoint is added Lawmaking will render the road which has the best travel fourth dimension. In the "Input" canvass of the above Google Canvass, y'all can see it calculates the waypoint count. Do not delete the function in this column since it is used in the programme. When you add more rows, make certain to copy downwardly the formula.

Retrieving Typical Travel Fourth dimension

The higher up script returns the real-time data considering the electric current traffic condition. If you want to go typical travel time for your road, replace the duration_in_traffic method with the elapsing  method.

You tin create custom carte du jour items in Google Canvass. We utilize ane custom menu with three sub-menus in this Google Sheet. When y'all create a custom menu item, it will appear at the cease of the toolbar menu.

In the above code, line 2-10, the onOpen() function creates a custom menu in your Google Sheet. Y'all can add your functions in your lawmaking every bit card items. With them, you can run your functions and you do non need to go to the code every time.

The onOpen() function create a new menu item in your Google Sheet. I have named it as "Google Travel Time". Nether these menu items, it creates sub-menu items namely "Run", "Gear up Triggers", and "Delete Triggers" to run the relevant functions.

Automate the Script to think data at predefined intervals

When yous run the above getDistance() function one time, it will return the travel time for that specific fourth dimension you striking the run push. Y'all can automate this task and let the function run at predefined time intervals.

This is i of the reasons yous may apply Google Sheet instead of Microsoft Excel to do this task. You tin use "Time-Driven Triggers" to automate your script. When you lot gear up Triggers correctly, the lawmaking will run on the Google cloud and y'all don't demand to keep your pc ability on.

Installable triggers are subject to Apps Script trigger quota limits.

Creating Triggers

Yous can ready triggers either programmatically or by going to "G Suite Developer Hub". The function createEveryMinutesTrigger() in the line, 79-89 of the above lawmaking creates time-driven triggers to run the runGetDistance() part in defined intervals.

To brand it easy, I take created another input sheet (SetTriggers) in the Google Canvass to define time intervals to run the triggers. So that, anyone tin use this code to go Google Map travel time without touching the code.

The function runGetDistance() looks for the start hour and end hour values in the sheet SetTriggers. Inside this function we call the getDistance() office.

I take added this createEveryMinutesTrigger() office as a sub-menu item in the custom card mentioned in a higher place.

Appropriately, to set the triggers,

! Before setting triggers, yous must provide origin and destination details in the "Inputs" sheet as explained in a higher place.

  1. Become to the sheet SetTriggers, and enter the starting and stop hours in 24-hour format in cells B1 and B2, respectively.
  2. Enter the running intervals in minutes in cell B6. The running intervals must be one of 1, 5, 10, 15, or 30.
  3. And then get to the custom carte du jour "Google Travel Time" and select "Set up Triggers".

You tin also set triggers manually through the script editor by going to Edit>Current project's triggers.

Deleting Triggers

Information technology is important to remember that the time-driven triggers volition be running until you delete them.

The deleteTrigger() function in the line 93-101 of the to a higher place code delete all the triggers ready previously. I accept added this function also as a sub-menu in the custom card.

To delete all your triggers, get to the Google Travel Time custom menu and select "Delete Triggers"

Y'all can also delete triggers manually through the script editor by going toEdit>Current project's triggers.

Wrapping Up

In this mail service, I showed yous how to get Google Map to travel time and distance using Google Apps Script DirectionFinder form. You tin can use this to observe traffic atmospheric condition over a period of time in a specific road, junction, or in a specific area. You can fifty-fifty create a free online dashboard with Google Data Studio to monitor them visually.

References

  1. Build time-based triggers in Apps Script projects to automate programs
  2. Google Map – Developer Guide
  3. Google App Script – Course DirectionFinder

weismanlishat.blogspot.com

Source: https://www.bpwebs.com/get-google-map-travel-time-and-distance/

Belum ada Komentar untuk "Typeerror: Cannot Read Property "Legs" From Undefined"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel