我正在尝试编写一个脚本,将地理位置的 lat 和 lng 作为数组传递给 PHP,并使用 jQuery AJAX 向客户端返回一个值。我能找到的最好的解决方案是 JSON。我的以下脚本NULL
出于某种我无法分辨的原因返回了我的值。
索引.html
<div id="geoloc"></div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(showPosition);
} else {
$('#geoloc').html("<p>Geolocation is not supported by this browser</p>");
}
function showPosition(position) {
var latlng = [ {"lat":position.coords.latitude, "lng":position.coords.longitude}];
$.ajax({
type: "POST",
url: "libs/filterstores.php",
data: { json: JSON.stringify(latlng) },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data){
$('#geoloc').html("Latitude: " + data.lat + "<br />Longitude: " + data.lng);
}
});
}
});
</script>
过滤器存储.php
$currloc = json_decode($_POST['latlng'], true);
$stores = array('lat'=>$currloc['lat'],'lng'=>$currloc['lng']);
echo json_encode($stores);
以下将是我点击浏览器弹出的“共享位置”按钮后返回的结果
Latitude: sdf
Longitude: sdfsd