views:

73

answers:

3

On my website members are tagging photo position on Google maps API. Longitude and latitude are saved in database (SQL).

Does anyone know how to find tagged photos that are in radius 100km of tagged photo?

Let say that latitude and longitude are 46.03765154061627 | 14.5404052734375. Is there any kind of math formula that would check 100km radius position or any other way?

Thank you!

+1  A: 

You could calculate the great-circle distance between two points. Luckily this is relatively easy with the haversine formula, assuming a spherical representation of the earth. You may want to read further and check out the JavaScript implementation at Calculate distance, bearing and more between Latitude/Longitude points by Chris Veness.

If you will only have a handful of photos, you can simply calculate the great-circle distance from the user submitted point to each photo point. Then simply sort the result list by the distance, and filter only the photos with a distance below the 100km threshold.

However, if you will be having many photos, you should probably consider filtering these from the database. You could use a database with geo-spatial indexing capabilities. For example MySQL, PostgreSQL and SQL Server 2008 all have geo-spatial features (either natively or via extensions), which include spatial indexing and implementations of the haversine formula.

Daniel Vassallo
A: 

Google Maps API includes methods for handling Latitude and Longitude coordinates. If you're dealing with a small number of coordinates it would be easy to use its GLatLng class and call the distanceFrom method.

You can also use the GLatLngBounds, which is designed to see if certain coordinates are within a defined rectangular boundary.

Brendan Enrick
@Brendan: Note that the `distanceFrom()` method is only available in the v2 API, which is now deprecated. The v3 API does not implement an equivalent method, and would have to implement it ourselves: http://code.google.com/apis/maps/documentation/javascript/reference.html#LatLng. Luckily it's not a difficult implementation.
Daniel Vassallo
A: 

to define a circle of 100km radius may be a bit complex (great circle calculation, distance between two points, etc.) ... it's easier to define a "square shape" of 100km (or 200km) length with your point (M) in the middle:

without being too scientific, asuming

  • the earth is a sphere (I know it isn't, but ...)
  • the circumference at aequator is ca. 40.000km - so a 100km portion (of longitude) is aequivalent to 0,9 angle degrees
  • neglecting the fact that for latitude this is varying the closer you come to the poles
  • rounding the 0.9 to 1 degree

we can say that you want to search for pictures in an area where its picture coords (P) meet the criteria

lon(M)-1 <= lon(P) <= lon(M)+1

lat(M)-1 <= lat(P) <= lat(M)+1

(all in degrees). I would think that - for a WEB service - this is accurate enough and very easy to implement.

MikeD
As @Brendan suggested, the Google Maps API also provides the `LatLngBounds. contains()` method to test if a coordinate lies in a rectangular bounds: http://code.google.com/apis/maps/documentation/javascript/reference.html#LatLngBounds
Daniel Vassallo
MikeD
Thank you all for helping me, I think I have found solution for my problems:http://spinczyk.net/blog/2009/10/04/radius-search-with-google-maps-and-mysql/And:http://sa-action.blogspot.com/2009/09/google-map-advance-search-by-radius.html
Uroš Uršej
@Uroš: Yes, the articles you mentioned are implementing the [Haversine formula](http://en.wikipedia.org/wiki/Haversine_formula) in SQL. The only problem with that method is that it will be very slow if you'll be having thousands of photos, because the database will have to do a full table scan to get you the results. That is why I suggested spatial indexing in my answer. However, if you will be having few photos, that could be good enough :)
Daniel Vassallo
Well my database has now over 60.000 photos and only 35 are geotagged yet. But in future I think many of my members will start using geo tagging and maybe spaital indexing will be by help ;) Thanks! ;)
Uroš Uršej
Uroš Uršej