php 无序经纬度排序
时间: 2023-08-31 16:32:52 浏览: 74
### 回答1:
可以使用 PHP 的 usort 函数来对经纬度进行排序。首先需要定义一个比较函数,用来确定哪些经纬度需要排在前面。然后将经纬度数组传递给 usort 函数,并使用定义的比较函数进行排序。
例如,假设有一个含有经纬度的数组 $coords,如下:
```
$coords = array(
array("lat" => 40.748817, "lng" => -73.985428),
array("lat" => 40.692943, "lng" => -73.990963),
array("lat" => 40.712764, "lng" => -74.011148),
);
```
可以定义一个比较函数,如下:
```
function compare($a, $b) {
if ($a["lat"] == $b["lat"]) {
return 0;
}
return ($a["lat"] < $b["lat"]) ? -1 : 1;
}
```
最后,使用 usort 函数对数组进行排序:
```
usort($coords, "compare");
```
这样就可以按照纬度进行无序排序了。
### 回答2:
PHP中可以使用usort()函数对无序经纬度进行排序。
首先,我们需要定义一个排序函数,用来比较两个经纬度的大小。可以使用两个经纬度的距离来进行比较,距离越小,排序越靠前。可以使用Haversine公式来计算两个经纬度之间的距离。
接下来,我们可以使用usort()函数来对经纬度数组进行排序。usort()函数接受一个数组和一个排序函数作为参数,在排序函数中,我们可以使用之前定义的比较函数来对经纬度进行比较。
下面是一个示例代码:
```php
// 定义排序函数,比较两个经纬度的距离
function compareDistance($location1, $location2) {
// 使用Haversine公式计算两个经纬度之间的距离
$lat1 = deg2rad($location1['lat']);
$lon1 = deg2rad($location1['lon']);
$lat2 = deg2rad($location2['lat']);
$lon2 = deg2rad($location2['lon']);
$earthRadius = 6371; // 地球半径,单位为千米
$deltaLat = $lat2 - $lat1;
$deltaLon = $lon2 - $lon1;
$a = sin($deltaLat / 2) * sin($deltaLat / 2) + cos($lat1) * cos($lat2) * sin($deltaLon / 2) * sin($deltaLon / 2);
$c = 2 * atan2(sqrt($a), sqrt(1 - $a));
$distance = $earthRadius * $c;
return $distance;
}
// 定义经纬度数组
$locations = array(
array('lat' => 39.913818, 'lon' => 116.363625),
array('lat' => 31.224361, 'lon' => 121.469170),
array('lat' => 22.279029, 'lon' => 114.162813)
);
// 使用usort()函数对经纬度数组进行排序
usort($locations, 'compareDistance');
// 输出排序结果
foreach ($locations as $location) {
echo '经度:' . $location['lat'] . ', 纬度:' . $location['lon'] . PHP_EOL;
}
```
以上代码会按照距离由小到大的顺序输出排序结果。你可以根据自己的实际需求修改比较函数和经纬度数组。
### 回答3:
在PHP中,如果想要对无序的经纬度进行排序,可以按照经度或纬度的大小进行排序。以下是一种可以实现的方法:
1. 定义一个包含无序经纬度的数组。
2. 使用 PHP 的内置函数 `usort()` 来对数组进行排序。在回调函数中,比较每个经纬度的经度或纬度,并根据需要返回负值、零或正值。例如,如果想按照经度从小到大的顺序进行排序,可以比较两个经纬度数组元素的第一个值。
3. 排序后的数组就是经过排序后的无序经纬度数组。
以下是代码示例:
```php
$coordinates = array(
array(45.678, -123.456),
array(41.232, -78.912),
array(36.789, -90.123),
// more coordinates...
);
usort($coordinates, function($a, $b) {
// Sort by longitude (first element)
if ($a[0] == $b[0]) {
return 0;
}
return ($a[0] < $b[0]) ? -1 : 1;
// To sort by latitude, replace $a[0] with $a[1]
});
// 打印经过排序后的经纬度数组
foreach ($coordinates as $coordinate) {
echo $coordinate[0] . ", " . $coordinate[1] . "<br>";
}
```
这样,你就能够获得按照经度从小到大排序的经纬度数组了。如果你想按照纬度进行排序,只需使用 `$a[1]` 和 `$b[1]` 来比较即可。
相关推荐
![php](https://img-home.csdnimg.cn/images/20210720083646.png)
![jpg](https://img-home.csdnimg.cn/images/20210720090814.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)