-2

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"}]}
Fabio
  • 1
  • 1
  • 4
  • 1
    ...or they're using a different precision for their radius – Rowland Shaw Dec 29 '14 at 12:10
  • Perhaps you could knock up an example that illustrates the issue? – Rowland Shaw Dec 29 '14 at 12:11
  • Please, Rowland, click on the link "Example", on my question, and follow the instructions. It will help to understand – Fabio Dec 30 '14 at 10:55
  • As I understand it, when you resize the circle of interest on your map, it shows all the results sent back, but this list is incomplete. As such, you'll need to share with us the script that the AJAX callback goes to, as that is where the fault appears to be. – Rowland Shaw Dec 30 '14 at 12:21
  • Yes, you have got it. I've updated with more code. I'm sure the problem is not in Ajax managing of the answer. – Fabio Dec 31 '14 at 12:45

1 Answers1

0

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

geocodezip
  • 158,664
  • 13
  • 220
  • 245
  • No, it isn't the solution. I've just inserted 6378.1 on the formula, but the issue is still there. Anyway, I've updated the example. Thanks for advice – Fabio Dec 30 '14 at 10:51