views:

257

answers:

5

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

+7  A: 

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.

Schnalle
Nope height isn't important, neither is walkability :)Thanks for the link, think I need to read up a little bit more ...
Tom
+2  A: 

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).

Can Berk Güder
+1  A: 

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.

farzad
hm, the pythagorean theorem is not really valid if you're calculating distances on earth, because earth roughly resembles an oblate spheroid. think about a triangle with 3 90° angles - possible, on a curved surface. altought you may ignore this for very short distances.
Schnalle
that is correct. I thought that when the problem is finding nearby distances, the difference between curved surface, and a linear plate surface can be ignored. it carries some amount of approximation.
farzad
A: 

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.

Philip T.