开发者

Google maps - Near positions

开发者 https://www.devze.com 2023-01-13 22:03 出处:网络
On my website members are开发者_开发技巧 tagging photo position on Google maps API. Longitude and latitude are saved in database (SQL).

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!


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消