简体   繁体   中英

calculate nearest place from my geolocated position

I have a series of places into a mysql database where every places has as field: lat,lon and address. I would calculate the nearest place from my actual position obtained through geolocation. I would use the function as here: Google Map - find markers but how can I take from db all value and put in javascript array?

Option 1: Do the calculation on the database by switching to a database that supports GeoIP.

Option 2: Do the calculation on the database: if you're using MySQL, so the following stored procedure should help

CREATE FUNCTION distance (latA double, lonA double, latB double, LonB double)
    RETURNS double DETERMINISTIC
BEGIN
    SET @RlatA = radians(latA);
    SET @RlonA = radians(lonA);
    SET @RlatB = radians(latB);
    SET @RlonB = radians(LonB);
    SET @deltaLat = @RlatA - @RlatB;
    SET @deltaLon = @RlonA - @RlonB;
    SET @d = SIN(@deltaLat/2) * SIN(@deltaLat/2) +
    COS(@RlatA) * COS(@RlatB) * SIN(@deltaLon/2)*SIN(@deltaLon/2);
    RETURN 2 * ASIN(SQRT(@d)) * 6371.01;
END//

Option 3: If you have an index on latitude and longitude in your database, you can reduce the number of calculations that need to be calculated by working out an initial bounding box in your scripting language of choice (minLat, maxLat, minLong and maxLong), and limiting the rows to a subset of your entries based on that (WHERE latitude BETWEEN minLat AND maxLat AND longitude BETWEEN minLong AND maxLong). Then MySQL only needs to execute the distance calculation for that subset of rows.

If you're using a SQL statement or a stored procedure to calculate the distance, then SQL still has to look through every record in your database, and to calculate the distance for every record in your database before it can decide whether to return that row or discard it. Because the calculation is relatively slow to execute, it would be better if you could reduce the set of rows that need to be calculated, eliminating rows that will clearly fall outside of the required distance, so that we're only executing the expensive calculation for a smaller number of rows.

Using a bounding box is like drawing a square on the map first with the left, right, top and bottom edges at the appropriate distance from our centre point. Our circle will then be drawn within that box, with the Northmost, Eastmost, Southmost and Westmost points on the circle touching the borders of the box. Some rows will fall outside that box, so SQL doesn't even bother trying to calculate the distance for those rows. It only calculates the distance for those rows that fall within the bounding box to see if they fall within the circle as well.

You can use cos and sin and other mathematical functions in mysql, so you wouldn't need to get the whole db into javascript, you can run the query in mysql.

I found the answer here: http://www.movable-type.co.uk/scripts/latlong-db.html (too long to include in an answer)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM