从给定的阵列lat长约25公里处获得最近的lat。

时间:2022-09-15 11:53:21

i have array of lat long like :

我有一系列的lat,比如:

var locationList = new Array( '23.2531803, 72.4774396', '22.808782, 70.823863', '24.3310019, 72.8516531', '22.3073095, 73.1810976', '22.3038945, 70.8021599', '23.850809, 72.114838' );

i want get nearest around 25 km 's lat long from first given array which is 23.2531803, 72.4774396

我想从第一个给定的数组中得到最近的大约25千米的lat,它是23.2531803,72.4774396。

are there any calculation for nearest 25 km 's lat long from given array.

从给定的阵列到最近的25公里的lat是否有任何计算。

NOTE: for some reason i can not use sql query, because i get lat long from given address 

3 个解决方案

#1


3  

Step 1: Calculate the distance between your start coordinate and every subcoordinate Step 2: Pick the smallest distance Step 3: Is it < 25 km? Success!

步骤1:计算开始坐标和每个子坐标步骤2之间的距离:选择最小距离步骤3:它< 25 km?成功!

How to calculate distance between two coordinates:

如何计算两个坐标之间的距离:

function distance($lat1, $lon1, $lat2, $lon2) {

$D = 6371; // Earth Radius
$dLat = $lat2-$lat1;
$dLon = $lon2-$lon1;

$a = sin($dLat/2) * sin($dLat/2) +
     sin($dLon/2) * sin($dLon/2) * cos($lat1) * cos($lat2); 

$b = 2 * atan2(sqrt($a), sqrt(1-$a)); 
$c = 2 * atan2(sqrt($a), sqrt(1-$a)); 


return $D * $c;

}

This function thinks of the Earth as a perfect ball, which it is not - slight variations do apply, but are neglible at the 25km diameter you want.

这个功能认为地球是一个完美的球体,它不是——轻微的变化确实适用,但是在你想要的25公里直径上是不容易的。

#2


1  

First of all your data array is awfull. You need to make your data more computer readable. Then you can use Pythagorean theorem to calculate the distance from each location. You can save the first distance and index in an variable then replace it with new distance and index if it's shorter.

首先,您的数据数组是awfull。你需要让你的数据更多的计算机可读。然后你可以用勾股定理来计算每个位置的距离。您可以将第一个距离和索引保存在一个变量中,然后用新的距离和索引替换它,如果它更短的话。

var closest = {id:0,dist:-1};
var myloc = [23.2531303, 72.4774398]
for(var i = 0; i < locationList.length; i++)
{
  var long_lat = locationList[i].match(/([0-9.]+)+/)
  long_lat[0] = parseFloat(long_lat[0]);
  long_lat[1] = parseFloat(long_lat[1]);
  var dist = Math.sqrt(Math.pow(long_lat[0] - myloc[0], 2)+Math.pow(long_lat[1] - myloc[1], 2)));
  if((closest.dist == -1) || (closest.dist > dist))
  {
    closest.dist = dist;
    closest.id = i;
  }

}

#3


0  

When you like to calculate in JS you may use google.maps.geometry.spherical.computeDistanceBetween() to calculate the distance between the single points.

当你喜欢用JS来计算时,你可以用google.maps.几何学,spheric.computedistancebetween()来计算单点之间的距离。

Store the results in an array, sort the array and when the smallest entry is <25km you got what you want.

将结果存储在一个数组中,对数组进行排序,当最小的条目<25km时,您得到了您想要的。

#1


3  

Step 1: Calculate the distance between your start coordinate and every subcoordinate Step 2: Pick the smallest distance Step 3: Is it < 25 km? Success!

步骤1:计算开始坐标和每个子坐标步骤2之间的距离:选择最小距离步骤3:它< 25 km?成功!

How to calculate distance between two coordinates:

如何计算两个坐标之间的距离:

function distance($lat1, $lon1, $lat2, $lon2) {

$D = 6371; // Earth Radius
$dLat = $lat2-$lat1;
$dLon = $lon2-$lon1;

$a = sin($dLat/2) * sin($dLat/2) +
     sin($dLon/2) * sin($dLon/2) * cos($lat1) * cos($lat2); 

$b = 2 * atan2(sqrt($a), sqrt(1-$a)); 
$c = 2 * atan2(sqrt($a), sqrt(1-$a)); 


return $D * $c;

}

This function thinks of the Earth as a perfect ball, which it is not - slight variations do apply, but are neglible at the 25km diameter you want.

这个功能认为地球是一个完美的球体,它不是——轻微的变化确实适用,但是在你想要的25公里直径上是不容易的。

#2


1  

First of all your data array is awfull. You need to make your data more computer readable. Then you can use Pythagorean theorem to calculate the distance from each location. You can save the first distance and index in an variable then replace it with new distance and index if it's shorter.

首先,您的数据数组是awfull。你需要让你的数据更多的计算机可读。然后你可以用勾股定理来计算每个位置的距离。您可以将第一个距离和索引保存在一个变量中,然后用新的距离和索引替换它,如果它更短的话。

var closest = {id:0,dist:-1};
var myloc = [23.2531303, 72.4774398]
for(var i = 0; i < locationList.length; i++)
{
  var long_lat = locationList[i].match(/([0-9.]+)+/)
  long_lat[0] = parseFloat(long_lat[0]);
  long_lat[1] = parseFloat(long_lat[1]);
  var dist = Math.sqrt(Math.pow(long_lat[0] - myloc[0], 2)+Math.pow(long_lat[1] - myloc[1], 2)));
  if((closest.dist == -1) || (closest.dist > dist))
  {
    closest.dist = dist;
    closest.id = i;
  }

}

#3


0  

When you like to calculate in JS you may use google.maps.geometry.spherical.computeDistanceBetween() to calculate the distance between the single points.

当你喜欢用JS来计算时,你可以用google.maps.几何学,spheric.computedistancebetween()来计算单点之间的距离。

Store the results in an array, sort the array and when the smallest entry is <25km you got what you want.

将结果存储在一个数组中,对数组进行排序,当最小的条目<25km时,您得到了您想要的。