How to get the total driving distance with Google Distance Matrix API? - javascript

I have an array of lat/lng. Example:
[
"14.60592,103.12081",
"38.21146,114.86461",
"13.73028,100.65138",
"34.08863,108.39783",
"57.186,14.04"
]
How can I calculate the overall distance from A to B and from B to C.
I trying with the Google Distance Matrix API.

If you need driving distance then you will not be able to calculate it with any formula not knowing the terrain, roads etc. For example if there is a river between those two points then the driving distance will depend on where is the closest bridge.
You can use some API that has that knowledge and can give you a real driving distance. For example you can use the Google Maps API. See e.g. Distance Matrix API:
https://developers.google.com/maps/documentation/distance-matrix/
https://developers.google.com/maps/documentation/distance-matrix/intro
You can use the Google Maps API in Node with:
https://www.npmjs.com/package/google-maps
https://www.npmjs.com/package/#google/maps
https://www.npmjs.com/package/googlemaps
and some more - see:
https://www.npmjs.com/browse/keyword/google-maps

Related

Can Vehicle Tracking be done without using Google Maps API?

My web application is intended to track a college shuttle on a predefined fixed route. There are certain pickup points on the route which are fixed. Through the web-app, the user should be able to get the estimated time of arrival of a college shuttle. For that, I would require the actual distance - along the route and the speed of the vehicle. The haversine formula for the shortest distance would not apply over here.
However, My friend thought of a solution that is to plot points along the route at a fixed distance say 20 meters, and calculate the distance in relation to the points. For example-If Shuttle is at point 5 and the user is at point 10, then the distance between both of them would be computed as (10-5)*20 i.e 100 meters. This solution isn't highly accurate but it would work.
How would I determine the shuttle location with respect to the points? I have the live coordinates of the shuttle, the coordinates of all the points on the route. What is the best way to get the result such as
Shuttle location is point 5. I am using Javascript and NodeJS. For Database MongoDB. Current location is obtained using Geolocation API
Leaflet is a great platform for maps and there is a plugin for k-next-nearest-neighbor searches here https://github.com/mapbox/leaflet-knn
The nearest stop along the route to the bus would then give you what you need I think

how to find latlong from distance?

I want to calculate the estimated location. Suppose i have source (A) and destination (B). Let say vehicle take 18 hours to reach from source to destination. After one hour it departs, the vehicle is at point C. At point C i have speed, Lat Long and distance cover from source (A). let say it cover 100km in 2 hours at point C. At this point i want to calculate the estimated location i.e where the vehicle will be after 2 hours or what will be the location after 300 km from point C with respect to current time,speed ,location and distance at point C. Vehicle is moving along the road. Please help me in this regard. Thanks
Not actual code but some possible hints to the answer. That's how I would do it.
I assume that you use the Google Direction API and that you know how to calculate a distance between two points from their coordinates.
From this, my idea would be call the API to get your route and to the use the polyline part of the answer. The polyline is encoded with this algorithm. You can use this javascript code to get your list of points.
You then calculate the distance between each per pair of points from the start. When you reach the distance from point A to point C, you know on which segment of the polyline your vehicle should be. To get the exact coordinates on the segment, I suggest you use the interpolate function of the Google Maps Geometry API.
Of course, if your route contains many segments, you might want to use a heuristic such as approximating the search of the middle segment by filtering your list by using a box of coordinates.

Find point of minimum total travel distance from a set of locations using Google Maps API

I have a list of addresses and I would like to find a point that has the minimum total travel distance (got by using Google Distance Matrix Service) to all addresses in the list.
For example: I have a list of all employees' addresses and I would like to find a place for the new office. The total driving distance from all employees' home to the new office should be minimum.
I think about try every road intersection point then compare the result, but I don't know how to get list of road intersections nearby.
How can I do that with Google Maps API? Thank you very much!

How can get the distances between 2 points via Google Maps Javascript API3?

I am developing an application via the Google Maps API (Version 3).
I have 2 google.maps.LatLng objects named loc1 and loc2.
How do I calculate the distance between them in say meters?
I have tried using various tricks from this question to convert latitude/longitude measures into meters. However, I question the reliability of my implementation.
I have also tried implementing a distance matrix, but I think that a distance matrix is a lot of work just to calculate the distance between 2 points.
Thank you!
Sounds simple, I recommend the geometry library,
https://developers.google.com/maps/documentation/javascript/reference#spherical
computeDistanceBetween(from:LatLng, to:LatLng, radius?:number)
From my experience the result is in meters. Remember to add the optional geometry library in your tag
<script type="text/javascript"
src="http://maps.googleapis.com/maps/api/js?libraries=geometry&sensor=false">
</script>
Calculating distances between two points basically boils down to pythagoras (sum of the squares etc). However, this doesn't really work with Lat & Lon (which are coordinates on a sphere). What I do is convert the Lat & Lon values to the local map grid which is flat. Then I can use pythagoras to calculate the distance between those points. I've already posted on this in Stack Overflow here. This will have all the code you would need.
Your question is a bit ambiguous.
I mean - when you say the distance, do mean
distance in a straight line
or driving distance,
or bicycle distance,
or walking distance.
The answer to this question will determine your implementation.
If it is a straight line distance you need, then it is best to use the haversine implementation.
Otherwise, you will need to request directions from the google directions service, specifying the mode of "transport".
When you've finished you can test the distance between two points on a straight line using the provided map. It will give you a visual as well as the distance.

Help with coordinates to plot on google map using api

i have got these two coordinates x and y from an external database.
X = 30490, y = 31430
was trying to figure out how to plot these coordinates on to google map? As google map only accept lat and lon value...
Any help is most appreciated!! Thanks...
This may help.
How do I convert coordinates to a Latitude & Longitude?
You will need to know the coordinate system that was used for those coordinates. There are hundreds if not thousands of 'official' coordinate systems!
once you have the coordinate system, you can transform the coordinates to those used by Google (Longitude, Latitude degrees WGS84 with a spherical Earth).
You may want to do the transformation offline, but if you are doing it online with JavaScript, then take a look at the Proj4JS library.
For offline use, the standard open source library is Proj.4 but that is probably a bit daunting if this is your first experience with geographic coordinate systems and map projections.

Categories

Resources