21

i would like to use Gmap API to calculate the best driving route for many destinations. I have read this page:

http://googlegeodevelopers.blogspot.de/2010/03/good-day-for-salesmen-that-travel-on.html

That is nearly the same thing i want. However, i only want to get a list of destinations back, that is sorted and tell which destination to go first.

For ex.: I send location of three Destinations A, B , C to Gmap. I need to receive an array like $bestroute[] = {B,C,A}.

I will code with PHP and javascript. If you can help me or know any tutorial, please help.

Thank you very much.

4

3 に答える 3

47

The link you refer to explains how to do this: admittedly you also get back some directions information you don't care about, but you can simply ignore it.

To be clear, you just make a directions request with some waypoints (say B,C and D) in addition to your origin and destination (A and E).

If using the web service, ensure you set optimize:true before listing your waypoints, and check the waypoint_order array (near the bottom of the result for this url) to get the info you want:

http://maps.googleapis.com/maps/api/directions/json?origin=Adelaide,SA&destination=Adelaide,SA&waypoints=optimize:true|Barossa+Valley,SA|Clare,SA|Connawarra,SA|McLaren+Vale,SA&sensor=false

If use the JS API, set optimizeWaypoints:true in your request.

As always, be sure you're abiding by the terms of service (e.g. results from your web service request must be displayed on a Google Map)

于 2012-08-03T23:01:12.607 に答える
6

You might want to look at the Distance Matrix service: https://developers.google.com/maps/documentation/javascript/distancematrix

It gives you distances between a set of origins and destinations, and might help you with narrowing down options.

于 2012-08-04T12:52:15.770 に答える
0

after hous of search i used this package https://github.com/alexpechkarev/google-maps with those parametres below :

$origin = ['lat' => 35.762288, 'lng' => -5.866525, 'ville' => 'tanger'];
    $destination = ['lat' => 33.565567, 'lng' => -7.552402, 'ville' => 'casablanca'];
    $points = [
        ['lat' => 33.951825, 'lng' => -6.839634, 'ville' => 'rabat'],
        ['lat' => 35.475028, 'lng' => -6.015167, 'ville' => 'assilah'],
        ['lat' => 35.178713, 'lng' => -6.132972, 'ville' => 'laarach'],
        ['lat' => 34.868921, 'lng' => -6.247939, 'ville' => 'moulay Bouslham'],
        ['lat' => 33.834129, 'lng' => -6.054144, 'ville' => 'khmissat'],
        ['lat' => 35.574788, 'lng' => -5.360490, 'ville' => 'Tetouan'],

    ];

    $wayPointsString = collect($points)->transform(function ($point) {
        return $point['lat'] . ',' . $point['lng'];
    })->implode('|');

    $data = \GoogleMaps::load('directions')
        ->setEndpoint('json')
        ->setParam([
            'origin' => collect($origin)->take(2)->implode(','),//tanger
            'destination' => collect($destination)->take(2)->implode(','), // casablanca
            'waypoints' => ['optimize:true|' . $wayPointsString
            ],
            'mode' => 'driving',
            'alternatives' => true,
            'departure_time' => 'now',
        ])
        ->get();

    $data = json_decode($data, true);
    $points_ = [];
    $points_[] = $origin;

    foreach ($data['routes'][0]['waypoint_order'] as $key => $value) {
        $points_[] = $points[$value];
    }
    $points_[] = $destination;
    dd($points_);

as a response :

array:8 [
  0 => array:3 [
    "lat" => 35.762288
    "lng" => -5.866525
    "ville" => "tanger"
  ]
  1 => array:3 [
    "lat" => 35.574788
    "lng" => -5.36049
    "ville" => "Tetouan"
  ]
  2 => array:3 [
    "lat" => 35.475028
    "lng" => -6.015167
    "ville" => "assilah"
  ]
  3 => array:3 [
    "lat" => 35.178713
    "lng" => -6.132972
    "ville" => "laarach"
  ]
  4 => array:3 [
    "lat" => 34.868921
    "lng" => -6.247939
    "ville" => "moulay Bouslham"
  ]
  5 => array:3 [
    "lat" => 33.834129
    "lng" => -6.054144
    "ville" => "khmissat"
  ]
  6 => array:3 [
    "lat" => 33.951825
    "lng" => -6.839634
    "ville" => "rabat"
  ]
  7 => array:3 [
    "lat" => 33.565567
    "lng" => -7.552402
    "ville" => "casablanca"
  ]
]
于 2019-09-12T16:07:53.850 に答える