我想使用Gmap API来计算许多目的地的最佳行车路线。 我看过这个页面:
http://googlegeodevelopers.blogspot.de/2010/03/good-day-for-salesmen-that-travel-on.html
这几乎和我想要的一样。但是,我只想获得一个目的地列表,这些目的地已经排序并告知首先要去哪个目的地。
例如:我将三个目的地A,B,C的位置发送给Gmap。我需要收到一个像$ bestroute [] = {B,C,A}这样的数组。
我将用PHP和javascript编写代码。如果您可以帮助我或了解任何教程,请提供帮助。
非常感谢。
答案 0 :(得分:42)
您所指的链接说明了如何执行此操作:诚然,您也会收到一些您不关心的路线信息,但您可以忽略它。
要明确的是,除了您的出发地和目的地(A和E)之外,您只需要提供一些路标请求(比如B,C和D)。
如果使用web service,请确保在列出航点之前设置optimize:true,并检查waypoint_order数组(靠近此网址结果的底部)以获取所需信息:
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
如果使用JS API,请在请求中设置optimizeWaypoints:true。
与往常一样,请确保您遵守服务条款(例如,您的网络服务请求的结果必须显示在Google地图上)
答案 1 :(得分:6)
您可能需要查看距离矩阵服务:https://developers.google.com/maps/documentation/javascript/distancematrix
它为您提供了一组来源和目的地之间的距离,可能会帮助您缩小选项范围。
答案 2 :(得分:0)
经过大量搜索,我使用了此软件包https://github.com/alexpechkarev/google-maps 以及以下参数:
$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_);
作为回应:
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"
]
]