I'm using Google Maps javascript v3. I draw a circle on the map and I permit the user modify the radius. Then, I send coordinates of the center and the radius in km to a server script. I extract all the markers with distance less than the radius from the center. I use the Great Circle formula and MySql GEOPOINT objects to calculate the distance:

(6371 * acos (cos(radians(".$coord_X_center."))* cos(radians(X(GEOPOINT)))
            * cos(radians(Y(GEOPOINT)) - radians(".$coord_Y_center."))
            + sin(radians(".$coord_X_center."))
            * sin( radians(X(GEOPOINT) )))) AS Distance FROM table_markers c
            HAVING Distance <= ".$radius." ORDER BY RATING desc,Distance;" 

Now consider the example I prepared for you: Example If you modify the radius of the circle, just to exclude one marker, much more markers will disappear. This is because the distance calculated, between center and disappeared markers, is higher than the radius of the circle. Why? What is the difference? Maybe Google Maps do not consider the Earth a sphere as I do in the formula above?

Update:

POST Request sent (double-checked with firebug): centerx: 8.222424000000046

centery: 45.295839

radius: 60.84800957919576

They are casted:

$coord_X_center = number_format((float) $post_filtered['centerx'], 4, '.', '');
$coord_Y_center = number_format((float) $post_filtered['centery'], 4, '.', '');
$radius = (int) $post_filtered['radius'];

Answer, via formula above:

{"names":["PONT-SAINT-MARTIN","FONTAINEMORE","LILLIANES","PERLOZ","GABY"],
"positions":[{"Lat":"7.81191320621736","Lon":"45.5991574533545"},
{"Lat":"7.88378299041174","Lon":"45.6488324511642"},
{"Lat":"7.85828195583629","Lon":"45.6242900876236"},
{"Lat":"7.81139246615188","Lon":"45.6285105114389"},
{"Lat":"7.88397564319245","Lon":"45.7144850123829"}]}
1

There are 1 best solutions below

1
On

The radius of the earth is 6378.1 kilometers (not 6371 km, at least according to Google).