Для моего веб-приложения я пытаюсь опубликовать переменные широты и долготы из javascript (googleMap.js) во внешний php-файл (closestLocations.php).Я использую метод post AJAX для публикации двух переменных, однако я все еще получаю, что переменные не определены.
Файл closestLocations.php создает XML-файл, содержащий список местоположений относительно определеннойместоположение (в настоящее время широта и долгота жестко заданы, но я хочу изменить их на эти переменные).
Любая помощь будет принята с благодарностью
googleMap.js
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
latitude = position.coords.latitude;
longitude = position.coords.longitude;
myPosition = {lat: latitude, lng: longitude};
$.ajax({
type: "POST",
data: {lat:latitude,lng:longitude},
url: "php/closestLocations.php",
success: function(data){
console.log(latitude);
}
});
//personalise infowindow
infoWindow.setPosition(myPosition);
infoWindow.setContent('You Are Here!');
infoWindow.open(map);
map.setCenter(myPosition);
//display marker on map where user is located
var marker = new google.maps.Marker({
map: map,
position: myPosition,
});
}, function() {
handleLocationError(true, infoWindow, map.getCenter());
});
} else {
handleLocationError(false, infoWindow, map.getCenter());
}
closestLocations.php
require_once("closestLocations.php");
function parseToXML($htmlStr)
{
$xmlStr=str_replace('<','<',$htmlStr);
$xmlStr=str_replace('>','>',$xmlStr);
$xmlStr=str_replace('"','"',$xmlStr);
$xmlStr=str_replace("'",''',$xmlStr);
$xmlStr=str_replace("&",'&',$xmlStr);
return $xmlStr;
}
$connection=mysqli_connect ('localhost', $username, $password);
if (!$connection) {
die('Not connected : ' . mysqli_error());
}
// Set the active MySQL database
$db_selected = mysqli_select_db($connection, $database);
if (!$db_selected) {
die ('Can\'t use db : ' . mysqli_error());
}
$query = "SELECT locationName, address, town, postcode, telephone, ( 3959 * acos ( cos ( radians(50.6346) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-6.6169) ) + sin ( radians(50.6346) ) * sin( radians( lat ) ) ) ) AS distance FROM defibrillators HAVING distance < 5 ORDER BY distance LIMIT 0 , 10 ";
$result=mysqli_query($connection, $query);
if(!$result){
die('Invalid query: ' . mysqli_error());
}
header("Content-type: text/xml");
// Start XML file, echo parent node
echo "<?xml version='1.0' ?>";
echo '<markers>';
$ind=0;
// Iterate through the rows, printing XML nodes for each
while ($row = @mysqli_fetch_assoc($result)){
// Add to XML document node
echo '<marker ';
echo 'name="' . parseToXML($row['locationName']) . '" ';
echo 'address="' . parseToXML($row['address']) . '" ';
echo 'town="' . parseToXML($row['town']) . '" ';
echo 'postcode="' . parseToXML($row['postcode']) . '" ';
echo 'telephone="' . parseToXML($row['telephone']) . '" ';
echo '/>';
$ind = $ind + 1;
}
// End XML file
echo '</markers>';