2

I have an MySQL database with thousands of entries with coordinates.

An entry looks like this: id, dt, img, long, lat

For now, I list the top 20 from mysql. But how do I get the 20 entries most near a given point? The given point is "center" in the code below.

Images

<script type="text/javascript">
  $(document).ready(function(){
    var myMarkers = {"markers": [   
    <?php 
      $query="SELECT id, dt, img, long, lat FROM places LIMIT 20";
      $result=mysql_query($query);
      $num=mysql_numrows($result);
      $i=0;
      while ($i < $num) {
        $row = mysql_fetch_array($result);
        $pl_id = $row[0];
        $pl_dt = $row[1];
        $pl_img = $row[2];
        $pl_longitud = $row[3];
        $pl_latitud = $row[4];
        $pres = "Text is coming.";
    ?>
    {"latitude": "<?php echo $pl_latitud ?>", "longitude":"<?php echo $pl_longitud ?>", "icon":"<?php echo $pl_img ?>", "baloon_text":"<?php echo $pres ?>"},
    <?php
        $i++;
      }
    ?>
    ]};
    $("#map").mapmarker({ zoom        : 10,
                          center      : '<?php echo $pl_latitud; ?>, <?php echo $pl_longitud; ?>',
                          markers : myMarkers
                        });
  });
</script>

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
  • (Pretty much exact) Duplicate: http://stackoverflow.com/questions/574691/mysql-great-circle-distance-haversine-formula – Sam Dufel May 13 '12 at 20:25
  • BTW, is there a way to tell the distance from given center? – andernicken May 13 '12 at 20:49
  • Sam Dufel: Dont understand that code. – andernicken May 13 '12 at 20:51
  • have you tried the google maps link in the question @SamDufel posted? I think you need the Haversine formula sql query from that page: https://developers.google.com/maps/articles/phpsqlsearch – martincarlin87 May 13 '12 at 21:12
  • @andernicken - the first answer to the question I linked has the exact sql query to sort locations by distance using lat/long. If you want an explanation of the math, just look at the wikipedia article on great circle distance. I don't see what else you could need. – Sam Dufel May 13 '12 at 22:57

0 Answers0