Google map or sql to stop a marker being inserted within 3 miles of another
I have created a php page with a v3 google map and a few fields for long and lat. The concept is that a user adds a marker for their event which populates the long and lat fields to be stored in a M$-SQL 2008 R2 database.
Wha开发者_Go百科t I need now is for the web app to not allow a new event marker to be inserted within 3 miles of any other long/lat within the DB (The user doesn't see any of the other markers!). Ideally this should happen when they try to set the marker, but can happen when the submit button is pressed and it tries to populate the DB.
I assume that I will need a mix of sql and php code to perform this, but I am stumped as to where to start.
I have no experience with the Google Maps API, but you state in your post that you have a MySQL database table with longs and lats.
Read up on AJAX to automatically process the marker position (and verify it) instead of pressing Submit. That should get you pretty far.
Now for the long and lats. Say a person drops it on (x,y). We want to make a circle around this point with a radius of 3 miles. To select all the markers within a (square! mind you) 3 miles around this, we would want to make a similar query:
select stuff from markers where long < 'x + 3' and long > 'x - 3' and lat < 'y + 3' and lat > 'y - 3'
with the integer 3 in miles, ofcourse.
Now we take the difference in long and lat between the new (x,y)
marker and every of the queried markers and apply the pythagorean triangle to that.
Lets say one of the database markers lies on (db_x,db_y)
. We get the difference of these two with (db_x - x, db_y - y)
*footnote: it doesn't matter wether this is positive or negative, for Pythagoras takes it's square)
Now onto squaring this stuff (all in pseudo-code):
h^2 = (db_x - x)^2 + (db_y - y)^2
h = sqrt(h^2)
now for all of the longs and lats in the database (that we queried) we have the distance h
to our new marker.
Then onto looping through each of these h
variables to see if one < 3
, and if it is, you can't place the marker...
Good luck.
精彩评论