json Google maps API Routes替代排序位置

ruoxqz4g  于 2023-08-08  发布在  Go
关注(0)|答案(1)|浏览(112)

我正在测试Google Maps的Routes API,我得到了一个有很多点的折线。这条折线是伟大的画在Map上,但我需要排序的位置,我发送到最好的顺序太,没有所有的折线点。
这是我发送给API的位置代码。我希望收到中间位置排序。

$origin = $_POST['origin'];
$intermediates = $_POST['intermediates'];
$destination = $_POST['destination'];
$travelMode = $_POST['travel'];
$car_opt = ($travelMode=='DRIVE')?true:false;

    $field_or =  [
        "vehicleStopover"=> $car_opt,
        "location"=>[
            "latLng"=>[
                "latitude"=>$origin['lat'],
                "longitude"=>$origin['long']
            ]
        ]
    ];

    $field_des =  [
        "vehicleStopover"=> $car_opt,
        "location"=>[
            "latLng"=>[
                "latitude"=>$destination['lat'],
                "longitude"=>$destination['long']
            ]
        ]
    ];

    $field_mid = [];
    foreach($intermediates as $i){
        $field_mid[] = [
            "vehicleStopover"=> $car_opt,
            "location"=>[
                "latLng"=>[
                    "latitude"=>$i['lat'],
                    "longitude"=>$i['long']
                ]
            ]
        ];
    }

    $fields = array(
        "origin"=> $field_or,
        "destination"=> $field_des,
        "intermediates"=> $field_mid,
        "travelMode"=>$travelMode,
        
    );

字符串
如果我发送这个中间体数组(伪代码):

$field_mid = [
{location 1 data},
{location 2 data},
{location 3 data},
{location 4 data},
{location 5 data},
]


我想收到什么:

[
{location 3 data (closest to origin)},
{location 1 data},
{location 5 data},
{location 4 data},
{location 2 data (closest to destination)},
]


编辑:我只是要求一个API,实际上排序的位置阵列访问(最佳路线)。如果没有API,你说出来,我便结束问题。我找不到这个alt API(使用Routes API,因为我也需要折线),这就是为什么我问。

vyu0f0g1

vyu0f0g11#

不是一个API,但我发现了一个JavaScript库,可以帮助找到最近的位置:方法是“nearestPoint”。
https://turfjs.org/docs/#nearestPoint

var targetPoint = turf.point([28.965797, 41.010086], {"marker-color": "#0F0"});
  var points = turf.featureCollection([
            turf.point([28.973865, 41.011122]),
            turf.point([28.948459, 41.024204]),
            turf.point([28.938674, 41.013324])
  ]);
  var nearest = turf.nearestPoint(targetPoint, points);

字符串

相关问题