PHP/MySQL: Select locations close to a given location from DB

情到浓时终转凉″ 提交于 2019-12-02 19:44:19
cletus

MySQL Great Circle Distance (Haversine formula) does exactly what you need.

With only 200 records however you may as well just load them all and check them with code. The data set is really way too small to be worrying too much about database vs code or any other such optimizations.

Calculating distance between zip codes in PHP has a couple of PHP implementations of this algorithm.

Geo Proximity Search is pretty much the exact same problem you have.

Maybe something like

SELECT field1, field2, ...,
    ACOS(SIN(latitude / 180 * PI()) * SIN(:1) + COS(latitude / 180 * PI()) * COS(:2) * COS(:2 - longtidude)) * 6371 AS distance
    ORDER BY distance ASC;

or

SELECT field1, field2, ...,
    ACOS(SIN(RADIANS(latitude)) * SIN(:1) + COS(RADIANS(latitude)) * COS(:2) * COS(:2 - longtidude)) * 6371 AS distance
    ORDER BY distance ASC;

(directly translated from the PHP code)

:1 and :2 is $lat2/180*pi() and $long2/180*pi() respectively.

That's the Haversine formula. You can translate the PHP directly into SQL so you can query the database spatially (the alternative being to pull every record out of the DB and run the data through PHP). MySQL provides all the maths functions that you need.

I did this for a commercial website which provided post/zipcode based distance lookups, so its certainly possible without specific GIS functions.

There are MySQL functions available for doing exactly this.

http://dev.mysql.com/doc/refman/5.0/en/gis-introduction.html

MySQL has the ability to index rows geospatially. You might not need to do this math by yourself (you can just ask MySQL to compute the distance between two geo objects and sort by that..).

See: http://forums.mysql.com/read.php?23,159205,159205

Irina S.

Just came a cross this topic. Maybe someone will be interested in this function, tested in MySql 5.7:

    create function GetDistance(lat1 real, lng1 real, lat2 real, lng2 real) returns real no sql
    return ATAN2(SQRT(POW(COS(RADIANS(lat2)) * SIN(RADIANS(lng1 - lng2)), 2) + 
                POW(COS(RADIANS(lat1)) * SIN(RADIANS(lat2)) - SIN(RADIANS(lat1)) *
                COS(RADIANS(lat2)) *  COS(RADIANS(lng1 - lng2)), 2)), 
                (SIN(RADIANS(lat1)) * SIN(RADIANS(lat2)) + COS(RADIANS(lat1)) *
                COS(RADIANS(lat2)) * COS(RADIANS(lng1 - lng2)))) * 6372.795;

Calling:

select GetDistance(your_latitude, your_longitude, table_field_lat,
                   table_field_lng) as dist from [your_table] limit 5;

Why don't you use MySQL's geospatial features...? No, just kidding.

If the 200 records are actual places like towns, etc then as an alternative could you use GeoNames' API?

The following webservice will provide the 10 closest locations to the lat and lng provided:

http://ws.geonames.org/findNearby?lat=47.3&lng=9

Source: http://www.geonames.org/export/web-services.html#findNearbyPlaceName

Full list: http://www.geonames.org/export/ws-overview.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!