Not able to get optimized route from Google Direct

2020-07-22 10:08发布

I was trying to find the optimized route using google direction api by providing the waypoints.Below are the waypoints i have used statring point: LHR first waypoint: Hounslow second waypoint: harlington

When I tried to find the optimized track using http://findthebestroute.com/RouteFinder.html below is the response which is the obvious answer and correct.enter image description here

But now i would like to perform same in my own application using directionservice api provided by google. I have prepared the above request as following

Request Url: http://maps.googleapis.com/maps/api/directions/json
Request Method: GET
Status Code: 200
Params: {
    "origin": "LHR",
    "mode": "driving",
    "sensor": "false",
    "departure_time": "1375789483",
    "units": "metric",
    "waypoints": "optimize:true|hounslowl|harlington"
}

and i am getting the wrong response as following which suggest me LHR to Hounslow and to harlingston. I don't know what's wrong with the request i have made. Any help is strongly appreciable. enter image description here

5条回答
聊天终结者
2楼-- · 2020-07-22 10:34

Directions Routes

The legacy DirectionsTrip object has been renamed DirectionsRoute. Note that a route now refers to the entire start to end journey, rather than simply a leg of a parent trip.

A DirectionsRoute contains a single result from the specified origin and destination. This route may consist of one or more legs (of type DirectionsLeg) depending on whether any waypoints were specified. As well, the route also contains copyright and warning information which must be displayed to the user in addition to the routing information.

The DirectionsRoute is an object literal with the following fields:

legs[] contains an array of DirectionsLeg objects, each of which contains information about a leg of the route, from two locations within the given route. A separate leg will be present for each waypoint or destination specified. (A route with no waypoints will contain exactly one DirectionsLeg.) Each leg consists of a series of DirectionSteps.

waypoint_order contains an array indicating the order of any waypoints in the calculated route. This array may contain an altered order if the DirectionsRequest was passed optimizeWaypoints: true.

overview_path contains an array of LatLngs that represent an approximate (smoothed) path of the resulting directions. bounds contains a LatLngBounds indicating the bounds of the polyline along this given route. copyrights contains the copyrights text to be displayed for this route. If you do not use the provided DirectionsRenderer object, you must handle and display this information yourself. warnings[] contains an array of warnings to be displayed when showing these directions. If you do not use the provided DirectionsRenderer object, you must handle and display these warnings yourself.

Ref: https://developers.google.com/maps/documentation/javascript/directions#Legs

查看更多
乱世女痞
3楼-- · 2020-07-22 10:45

The Official documents can be found at the following url: https://developers.google.com/maps/documentation/javascript/directions#DirectionsRequests

Basically you just need to add optimizeWaypoints:true to your request. See the following Example:

var start_location = "LHR";
var finish_location = "LHR";
var way_points = [
    {
      location:"hounslowl",
      stopover:false
    },{
      location:"harlington",
      stopover:false
    }
  ]

// Let's create the Google Maps request object
var request = {
  origin: start_location, 
  destination: finish_location, 
  optimizeWaypoints:true,
  waypoints[]: way_points,
  travelMode: google.maps.TravelMode.DRIVING
};
查看更多
家丑人穷心不美
4楼-- · 2020-07-22 10:46

Sorry to answer to my own question. But since i was having great problem with the above scenario, I hope this has happened to other as well. This is for those who are having same problem and wasting time to find the desired answer.

I was searching for the answer to the question for almost 2 days. And finally found that there is no direct one step method to calculate the result and get the way point in sorted order. But there is way by sending multiple optimize route request to google.

Before I explain this simple logic, let's first figure out the required parameter that you need to set

  1. Origin - from where you want to begin the route
  2. Destination - last point to the route
  3. waypoints = points between the origin and destination with optimize set to true

Now the thing is you first need to set the origin and find the best final destination to obtain the optimized track.

For eg. if you are have number of way points say A, B, C, D. It is true that this order may not remain same after we find the optimized track. So first we need to decide that from where we want to begin the route let's assume B, then we need to calculate three optimized track assuming remining one of the point is final destination. Meaning

  1. origin: B destination:A waypoints: C,D => result [d1 km, t1 minute]
  2. origin: B destination:C waypoints: A,D => result [d2 km, t2 minute]
  3. origin: B destination:D waypoints: C,A => result [d3 km, t3 minute]

seeing at the result we have three possible track with different distance value and time. Now you simply can select your optimized track based on time optimization or distance optimization.

查看更多
Fickle 薄情
5楼-- · 2020-07-22 10:46

I was facing the same problem from last 2 hours.And i have found the main reason which can help any other programmer if they will have same problem.The reason why the google direction Api is not showing the optimized rout is because we are not providing them exact location.you are inserting only string of way-points which have only single city address .you have to provide complete address so google will know exact lat and long of that way-point as well as the origin and destination and will resolve this issue. example : origin = Gare de Thionville, Thionville, France.

Reference from google direction api documentation. "location specifies the location of the waypoint, as a LatLng, as a google.maps.Place object or as a String which will be geocoded."

查看更多
该账号已被封号
6楼-- · 2020-07-22 10:54

Using optimize:true allows the Directions service to optimize the provided route by rearranging the waypoints in a more efficient order. Documentation

EDIT

Looking at your code you do not supply a destination I tried (adding twickenham as a waypoint)

http://maps.googleapis.com/maps/api/directions/json?origin=LHR,uk&destination=hounslow,uk&waypoints=optimize:true|twickenham,uk|harlington,uk&sensor=false

And this provided the optimises route required.

To use optimize:true you require origin, destination and at least 2 waypoints.

查看更多
登录 后发表回答