Leaflet için yeni ve işaretleyicileri bir MySQL veritabanından bir broşür haritasına nasıl yükleyeceğimi merak ediyordum.MySQL'den PHP ve ajax kullanarak işaretleyiciler nasıl yüklenir?
İşaretçiler yalnızca harita başlatıldığında değil, bir işlevle yüklemek istemiyorum çünkü kullanıcının örneğin tarihler arasında seçim yapmasını ve ardından bu tarihlerdeki işaretçileri yüklemesini istiyorum. Şimdiye kadar aşağıdakileri yaptım, ancak bir hata alıyorum: Hatalı yaptığımdan emin değilim.
jQuery
$(document).ready(function() {
var markerpositions;
var map = L.map('map').setView([-55.7770641,55.6602758], 13);
L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
}).addTo(map);
getlocations();
var marker = L.marker(markerpositions).addTo(map);
function getlocations(){
var id = "1";
$.ajax({
type: "POST",
url: "getlocationstomap.php",
data: {id:id},
dataType: 'json',
cache: false,
})
.success(function(response) {
if(!response.errors && response.result) {
$.each(response.result, function(index, value) {
markerpositions = '['+value[0]+','+value[1]+']';
});
} else {
$.each(response.errors, function(index, value) {
$('input[name*='+index+']').addClass('error').after('<div class="errormessage">'+value+'</div>')
});
}
});
}
})
PHP (getlocationstomap.php) Eğer ajax yanıtı ayrıştırmak zaman
<?php
// assign your post value
$inputvalues = $_POST;
// assign result vars
$errors = false;
$result = false;
$mysqli = new mysqli('localhost', "root", "", "traxi");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
// escape your values
foreach ($inputvalues as $key => $value) {
if(isset($value) && !empty($value)) {
$inputvalues[$key] = $mysqli->real_escape_string($value);
} else {
$errors[$key] = 'The field '.$key.' is empty';
}
}
if(!$errors) {
// select your query
$addresult = "
SELECT `latitude`, `longitude`
FROM `positions`
WHERE `deviceid` = '" . $inputvalues['id'] . "'
";
//$returnResult = array();
if($result = $mysqli->query($addresult)) {
// collect results
while($row = $result->fetch_all())
{
// assign to new array
// make returnResult an array for multiple results
$returnResult = $row;
}
}
}
// close connection
mysqli_close($mysqli);
// print result for ajax request
echo json_encode(['result' => $returnResult, 'errors' => $errors]);
exit;
?>
vay benim için ideal bir yaklaşım gibi görünüyor! çok teşekkür ederim ! Kesinlikle onu okuyacağım. –