判断我是否在附近

发布于 2024-07-18 07:21:18 字数 165 浏览 7 评论 0原文

我已经得到了我当前的位置,并且我已经得到了地点列表。

我想做的是弄清楚我是否在其中一个地方附近,附近大约是+100m。 我不想显示地图,只想知道我是否在附近。

哪些类型的 php 库可用于比较位置/经纬度? 或者我可以用数学来解决这个问题吗?

谢谢

I've got my current location lat long and I've got a list of places and there lat long.

What I'd like to do is figure out if I'm nearby one of the places, nearby would be something like +100m. I don't want to display a map, just know if I'm near it.

What kind of php libraries are available for comparing location/lat long? Or can I solve it with math?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

望笑 2024-07-25 07:21:18

使用经度和纬度确定距离

这个问题可以通过使用球坐标来最容易地解决
地球。 你以前处理过那些吗? 这是从
球坐标到普通直角坐标,其中 a=纬度
b=经度,r 是地球半径:

x = r Cos[a] Cos[b]
y = r Cos[a] Sin[b]
z = r Sin[a]

然后我们将使用点积的以下属性(记为 [p,q]):

[p,q] = 长度[p] * 长度[q] * Cos[p 与 p 之间的角度] q]

(...)

至少,如果身高对你来说不重要的话。
如果您需要取决于道路或步行能力的高度和/或距离(这甚至是一个词吗?),我认为谷歌地图会更准确。

Using Longitude and Latitude to Determine Distance

This problem can be most easily solved by using spherical coordinates on the
earth. Have you dealt with those before? Here's the transformation from
spherical coordinates to normal rectangular coordinates, where a=latitude
and b=longitude, and r is the radius of the earth:

x = r Cos[a] Cos[b]
y = r Cos[a] Sin[b]
z = r Sin[a]

Then we'll use the following property of the dot product (notated [p,q]):

[p,q] = Length[p] * Length[q] * Cos[angle between p & q]

(...)

at least, if height isn't important to you.
if you need the height and/or distance dependend on roads or walkability (is this even a word?), i think google maps would be more exact.

抠脚大汉 2024-07-25 07:21:18

给定球面坐标(纬度/经度),计算两点之间的距离并不难。 在谷歌上快速搜索“纬度经度距离”就会发现这个方程。

显然是这样的:

acos(cos(a.lat) * cos(a.lon) * cos(b.lat) * cos(b.lon) +
     cos(a.lat) * sin(a.lon) * cos(b.lat) * sin(b.lon) +
     sin(a.lat) * sin(b.lat)) * r

其中 ab 是您的点,r 是地球的平均半径(6371 公里)。

一旦您能够计算给定坐标的两点之间的距离,您将需要循环遍历所有地标并查看您当前的位置是否靠近一个。

但是,如果您有很多地标,您可能需要使用空间搜索算法(可能使用 Quadtree 或类似的数据结构)。

It's not hard to calculate distance between two points, given their spherical coordinates (latitude/longitude). A quick search for "latitude longitude distance" on Google reveals the equation.

Apparently it's something like this:

acos(cos(a.lat) * cos(a.lon) * cos(b.lat) * cos(b.lon) +
     cos(a.lat) * sin(a.lon) * cos(b.lat) * sin(b.lon) +
     sin(a.lat) * sin(b.lat)) * r

where a and b are your points and r is the earth's mean radius (6371 km).

Once you're able to calculate the distance between two points given their coordinates, you'll want to loop through all the landmarks and see if your current location is near one.

However, if you have many landmarks, you might want to use a spatial search algorithm (maybe using a Quadtree or a similar data structure).

琉璃梦幻 2024-07-25 07:21:18

我不熟悉解决这个问题的软件库。 但如果你在 2D 空间中谈论,那么我想到了一些数学:

你可以使用以下计算找到 2D 空间中任意 2 个点的距离:

distance = sqrt( (X2 - X1)^2 + ( Y2 - Y1 )^2 )

其中 ^2 表示由 2 提供动力。

所以假设您有一个 Point 对象数组(这里我为 Points 定义了一个简单的类),这样您就可以找出哪些点是相邻的:

class Point {
    protected $_x = 0;
    protected $_y = 0;

    public function __construct($x,$y) {
         $this->_x = $x;
         $this->_y = $y;
    }
    public function getX() {
         return $this->_x;
    }

    public function getY() {
    return $this->_y;
    }    

    public function getDistanceFrom($x,$y) {
        $distance = sqrt( pow($x - $this->_x , 2) + pow($y - $this->_y , 2) );
        return $distance;
    }

    public function isCloseTo($point=null,$threshold=10) {
        $distance = $this->getDistanceFrom($point->getX(), $point->getY() );
        if ( abs($distance) <= $threshold ) return true;
        return false;
    }

    public function addNeighbor($point) {
        array_push($this->_neighbors,$point);
        return count($this->_neighbors);
    }

    public function getNeighbors() {
        return $this->_neighors;
    }
}

$threshold = 100; // the threshold that if 2 points are closer than it, they are called "close" in our application
$pointList = array();
/*
 * here you populate your point objects into the $pointList array.
*/
// you have your coordinates, right?
$myPoint = new Point($myXCoordinate, $myYCoordinate);

foreach ($pointList as $point) {
   if ($myPoint->isCloseTo($point,$threshold) {
       $myPoint->addNeighbor($point);
   }
}

$nearbyPointsList = $myPoint->getNeighbors();

编辑:抱歉,我忘记了直线距离公式。 X 轴和 Y 轴距离值均应乘以 2,然后将其总和开平方即可得到结果。 该代码现已更正。

I'm not familiar with software libraries for this problem. but if you are talking in 2D space, then here is some math that comes to my mind:

you can find the distance of any 2 points in the 2D space using this calculation:

distance = sqrt( (X2 - X1)^2 + (Y2 - Y1 )^2 )

inwhich ^2 means powered by 2.

so le'ts say you have an array of Point objects (here I define a simple class for Points), this way you can find out which points are neighbored:

class Point {
    protected $_x = 0;
    protected $_y = 0;

    public function __construct($x,$y) {
         $this->_x = $x;
         $this->_y = $y;
    }
    public function getX() {
         return $this->_x;
    }

    public function getY() {
    return $this->_y;
    }    

    public function getDistanceFrom($x,$y) {
        $distance = sqrt( pow($x - $this->_x , 2) + pow($y - $this->_y , 2) );
        return $distance;
    }

    public function isCloseTo($point=null,$threshold=10) {
        $distance = $this->getDistanceFrom($point->getX(), $point->getY() );
        if ( abs($distance) <= $threshold ) return true;
        return false;
    }

    public function addNeighbor($point) {
        array_push($this->_neighbors,$point);
        return count($this->_neighbors);
    }

    public function getNeighbors() {
        return $this->_neighors;
    }
}

$threshold = 100; // the threshold that if 2 points are closer than it, they are called "close" in our application
$pointList = array();
/*
 * here you populate your point objects into the $pointList array.
*/
// you have your coordinates, right?
$myPoint = new Point($myXCoordinate, $myYCoordinate);

foreach ($pointList as $point) {
   if ($myPoint->isCloseTo($point,$threshold) {
       $myPoint->addNeighbor($point);
   }
}

$nearbyPointsList = $myPoint->getNeighbors();

edit: I'm sorry, I had forgotten the linear distance formula. both X and Y axis distance values should be powered by 2 and then the sqrt of their sum is the result. the code is now corrected.

余生共白头 2024-07-25 07:21:18

我对这个包不熟悉,但是
GeoClass 可能有用。

我在 FreeGIS 网站上找到了它。 如果这不是您想要的,软件部分列出了许多其他 php 软件包。

I'm not familiar with the package, but
GeoClass may be useful.

I found it on the FreeGIS website. If that isn't quite what you were looking for, there were many other php packages listed in the software section.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文