这段代码非常适用于它通过javascript获取我的经度和纬度,并输出到屏幕。但我想将这些值传递给PHP变量,这是我目前所拥有的。我不是一个javascript专家,虽然在我问Stackoverflow之前我花了5到6个小时研究和尝试不同的东西。谢谢你的帮助。
<!DOCTYPE html>
<html>
<body>
<p id="long"></p>
<p id="lat"></p>
<script>
var x = document.getElementById("long");
var y = document.getElementById("lat");
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
function showPosition(position) {
x.innerHTML = position.coords.longitude; //displays longitude on screen
y.innerHTML = position.coords.latitude; //displays latitude on screen
}
</script>
<?php
echo $plong = '<script>x.innerHTML = position.coords.longitude;</script>';
echo $plat = '<script>y.innerHTML = position.coords.latitude;</script>';
echo "long " . $plong;
echo "lat " . $plat;
?>
</body>
</html>