Looking for point within a circle via lat/lng

459 views
Skip to first unread message

TS

unread,
Sep 20, 2010, 5:23:25 AM9/20/10
to Google Maps JavaScript API v3
Hi,
in a database I have stored several addresses including their
geocoding data. Now I need to write a function where the user can set
a start point and a distance (radius) and the result should show every
address which is within this circle.

I need to do all the calculation server side and then sent the
relevant addresses to the client to finally ad the markers to the map.

By calling the Geocoder I get the lat/lng for the start point but
don't know how to do math for the circle. I would need to check every
address from the db whether it is in the area of the given circle or
not by looping.

Is this possible? Any help appreciated!

geoco...@gmail.com

unread,
Sep 20, 2010, 8:33:37 AM9/20/10
to Google Maps JavaScript API v3
See the "store locator" tutorial. It does that:
http://code.google.com/apis/maps/articles/phpsqlsearch_v3.html

-- Larry

cwb

unread,
Sep 20, 2010, 6:56:13 PM9/20/10
to Google Maps JavaScript API v3
The distance formula from any point on a flat plain is pretty
straightforward. If you get very far away, you now need to step into
spherical trigonometry (and that was no longer a required subject even
when I went to high school.)

For a flat plain, the distance between 2 objects is the simple
triangle hypotenuse theorem.

distance = square root ( (x2 - x1) squared + (y2-y1) squared ))

Note that you need to do this arithmetic for every point that's within
the square (lowest x, lowest y) to (highest x, highest y), but you can
do this query in SQL and then eliminate the ones outside the circle.

Rossko

unread,
Sep 20, 2010, 7:59:58 PM9/20/10
to Google Maps JavaScript API v3
> distance = square root ( (x2 - x1) squared + (y2-y1) squared ))

Sure, but that doesn't work well for lat/long coordinates. Haversine
Formula is generally used.

> Note that you need to do this arithmetic for every point that's within
> the square (lowest x, lowest y) to (highest x, highest y), but you can
> do this query in SQL and then eliminate the ones outside the circle.

Yes, if searching a large database a simple "bounding box" query can
be used to give candidates for more complicated further testing.

cwb

unread,
Sep 20, 2010, 10:58:10 PM9/20/10
to Google Maps JavaScript API v3
As I said, if you talk "long distances", the earth is not a "plain"
field. If you'd rather have him use Haversines formula, why not quote
it here. Otherwise, for distances under a few hundred miles (other
than at Latitude 90 or -90) my formula should work (assume latitude is
"y" and longitude is "x").

TS

unread,
Sep 21, 2010, 4:08:14 AM9/21/10
to Google Maps JavaScript API v3
Hi,
thanks for the answers so far! Meanwhile my problem is not to
calculate the radius of the given addresses in the database anymore. I
would do this by using Pythagoras: a2 + b2 =c2.
The problem I still have is that the client sets the radius in meter
and for any calc and the following comparison with MySQL I need to
convert either the distance in meters to lat/lng or lat/lng to meters.

Rossko

unread,
Sep 21, 2010, 10:06:36 AM9/21/10
to Google Maps JavaScript API v3
> If you'd rather have him use Haversines formula, why not quote
> it here.

He can use what he likes, when properly informed. It's not difficult
to find discussion of Haversine in the language and context of choice
for any interested person.

>  Otherwise, for distances under a few hundred miles (other
> than at Latitude 90 or -90) my formula should work (assume latitude is
> "y" and longitude is "x").

For moderate latitudes, error could be in the order of 30%, and will
apply to distances of miles or metres. That might be fine for the
orginal poster, so long as they understand the limitations of using
degrees as a form of distance measurement away from the equator.

For the original poster, have a look at some of these
http://www.google.com/search?q=lat+long+distance
just calculate the distance between two points, convert it to miles or
metres or whatever you want, and compare it with the search
requirement.

TS

unread,
Sep 24, 2010, 4:58:47 AM9/24/10
to Google Maps JavaScript API v3
I were using the store locator tutorial suggested by Larry which
seemed to be what I needed.
It is using the Haversine formula to calculate the addresses within a
specific distance.

To get out all the addresses within 5 km the SQL-command after the
tutorial looks like this (in PHP):
$radius=5;
SELECT id,lat,lng,
(6371*acos(cos(radians($lat))*cos(radians(lat))*cos(radians(lng)-
radians($lng))+sin(radians($lat))*sin(radians(lat)))) AS distance FROM
addresses HAVING distance<$radius ORDER BY distance LIMIT 50

$lat and $lng is the geocoded location from the origin address.

When I look at the map and using Google's scaleControl to compare it
with the results shown within 5 km, also addresses were shown which
are a lot farther away than 5km.

Does anybody know how I can fix that problem or what's wrong on the
formula?
Reply all
Reply to author
Forward
0 new messages