7

I am creating a mobile app using Phonegap and JQuery.

The app collects the longitude and latitude of users and stores them in a database (using a PHP script - web service), and with some algorithm it detects whether there is traffic or not. What I need is to grab all the longitude and latitude values from the database which are close to each other, say in the same street/200m range.

Lets say you have a list in the database with 5 lon/lat which are near each other (in the same street A), 3 lon/lat which are near each other in some Street B, and some other representing users randomly in other streets. Can anyone tell me how I can detect the lon/lat values that are near each other? The values I am storing them as separate values in the MySQL database, ie one field for longitude and another for latitude.

Once I obtain the lon/lat which are near each other, then I would need to find the center point between the users in street A, and users in street B to mark as a traffic congestion (based on some other detections).

2
  • Depending on the scale of your application and if its an option you might want to consider using PostGIS instead. As it has much better SPATIAL support than MySQL. Doing this in PostGIS would be very simple see gis.stackexchange.com/q/27878/3591 Dec 19, 2012 at 11:19
  • Hi Mark, the scale of the application is pretty basic. We were considering PostGIS but resorted to MySQL as we found hosting which was cheaper... Otherwise it's true PostGIS has better spatial features :/ Dec 19, 2012 at 11:35

1 Answer 1

6

You should look into the Haversine formula. Please see below:

SELECT id, (3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance 
FROM markers 
HAVING distance < 25 
ORDER BY distance;

This will return all records that are within 25 miles of the input long / lat pair.

5
  • HAVING distance < 25 the distance keyword here - is it something recognized by MySQL? Some function or something of the sort? Dec 19, 2012 at 11:33
  • Please check the ans it making an alias 'distance' Dec 19, 2012 at 11:42
  • @m4k I guess the above snippet would get me all the long/lat that are both in Street A and Street B. Is there some way to differentiate between the two though? Cos what I would need to do is get the points that are close to each other (as above), and then get all the points on Street A or B and find their mid-point. Dec 19, 2012 at 15:10
  • @m4k I tried this out but unfortunately it is not returning anything :( just FYI, my long/lat values are in the following format: 51.58731, -0.319427 I have replaced the lat/lng in the Select statement with my fields Longitude and Latitude and also replaced the ID with VehicleID. For some reason I am not getting anything back even though I have in the database vehicles that are close together Dec 19, 2012 at 15:11
  • @m4k ok I found out that I need to replace 37 and -122 with my coordinates. Dec 19, 2012 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.