2
votes

I have a MySQL database with stored lat-lngs using a PHP file. Markers are displayed based on haversine formula by radius and coordinate parameters (2km, myLat, myLng - works fine). I've seen post on passing user geocoded lat-lngs to the formula to display markers. Is there any way to have the lat-lng position of a draggable marker passed to the haversine formula to display the markers? The lat-lng could be sent by search button click after dragging the marker.

This is an example, the selection made is based on choosen lat-lng, but the marker is draggable, I'd like the connect the draggable marker to the php code.

MySQL selection & draggable marker

Thank you in advance

<?php
 header("Content-type: text/xml");
 require("mypwfile.php");

 function parseToXML($htmlStr) 
 { 
 $xmlStr=str_replace('<','&lt;',$htmlStr); 
 $xmlStr=str_replace('>','&gt;',$xmlStr); 
 $xmlStr=str_replace('"','&quot;',$xmlStr); 
 $xmlStr=str_replace("'",'&#39;',$xmlStr); 
 $xmlStr=str_replace("&",'&amp;',$xmlStr); 
 return $xmlStr; 
 } 

 // Get parameters from URL
 $center_lat =$radius = (isset($_GET['lat']) ? $_GET['lat'] : null);
 $center_lng = (isset($_GET['lng']) ? $_GET['lng'] : null);
 $radius = (isset($_GET['radius']) ? $_GET['radius'] : null);

 // Opens a connection to a MySQL server
 $connection=mysql_connect ('localhost', $username, $password);
 if (!$connection) {
 die('Not connected : ' . mysql_error());
 }

 // Set the active MySQL database
 $db_selected = mysql_select_db($database, $connection);
 if (!$db_selected) {
 die ('Can\'t use db : ' . mysql_error());
 }

 // Select all the rows in the markers table
 $query = sprintf("SELECT app_id, lat, lng, ( 6371 * acos( cos( radians('myLat') )
* cos( radians( lat ) ) * cos( radians( lng ) - radians('myLng') ) + 
sin( radians('myLat') ) * sin( radians( lat ) ) ) ) 
AS distance FROM markers HAVING distance < '2' ORDER BY distance LIMIT 0 , 30",

mysql_real_escape_string($center_lat),
mysql_real_escape_string($center_lng),
mysql_real_escape_string($center_lat),
mysql_real_escape_string($radius));

$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}

// Start XML file, echo parent node
echo "<markers>\n";

// Iterate through the rows, printing XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
echo '<marker ';
echo 'app_id="' . parseToXML($row['app_id']) . '" ';
echo 'lat="' . $row['lat'] . '" ';
echo 'lng="' . $row['lng'] . '" ';
echo 'distance="' . $row['distance'] . '" ';
echo "/>\n";
}

// End XML file
echo "</markers>\n";
?>
1

1 Answers

0
votes

In the Map page assign a click listener to the marker to save lat,lng & radius to database.(myfile.php is your php/mysql file)

google.maps.event.addListener(marker, "click", function() {
  var radius = document.getElementById('radiusSelect').value;
  var latlng = marker.getPosition();
  var url = "myfile.php?lat="+latlng.lat()+"&lng="+latlng.lng()+'&radius='+ radius;
  });

You also have an error in formating your query using sprintf

Should be

$query = sprintf("SELECT app_id, lat, lng, ( 6371 * acos( cos( radians('%s') )* cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) 
AS distance FROM markers HAVING distance < '%s' ORDER BY distance LIMIT 0 , 30",
mysql_real_escape_string($center_lat),
mysql_real_escape_string($center_lng),
mysql_real_escape_string($center_lat),
mysql_real_escape_string($radius));