使用 PHP 进行地理定位

Geolocation with PHP

有没有办法在单个文件解决方案中将 lat/long 转换为 PHP?通常使用 JS 我会在纯 JS 解决方案中使用它,但现在我需要 PHP 变量中的值:

<script>
$(document).ready(function(){
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showLocation);
    } else {
        $('#location').html('Geolocation is not supported by this browser.');
    }
});
function showLocation(position) {
    var latitude = position.coords.latitude;
    var longitude = position.coords.longitude;
    return latitude;
}
</script>

我需要在不使用 AJAX post 的情况下将 lat/long 变量转换为 PHP 类型 url: xxx.php解决方案。仅限单个文件。有任何关于以干净的方式执行此操作的想法吗?

顺便说一句 - 如果我将它们写入 cookie,这只有在我重新加载文档时才有效。 :-(

谢谢!

您可以使用 geoplugin api Link

<?php
  echo var_export(unserialize(file_get_contents('http://www.geoplugin.net/php.gp? 
  ip='.$_SERVER['REMOTE_ADDR'])));
?>

我这样做了 在 JS (g1.php):

function showPosition(position) { var latlon = position.coords.latitude + "," + position.coords.longitude; var prog = "g2.php?latlon=" + latlon; location.href = prog;}

然后在 g2.php

中这样做

$field = explode(',', $_GET['latlon']);

我不知道这是否是好的编程,但它确实有效