【问题标题】:How to get a User's Current Location Using Google Geocode API and PHP如何使用 Google Geocode API 和 PHP 获取用户的当前位置
【发布时间】:2015-03-23 05:39:13
【问题描述】:

我已经创建了一个应用程序,作为数据收集的一部分,我想在用户使用 php 访问应用程序和网站时捕获他们的当前位置。

理想情况下,我想让它尽可能简单。目前,我有以下脚本,但其中有一个默认地址:

$fullurl = "http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true";
echo $json;
$string .= file_get_contents($fullurl); // get json content
$json_a = json_decode($string, true); //json decoder

echo $json_a['results'][0]['geometry']['location']['lat']; // get lat for json
echo $json_a['results'][0]['geometry']['location']['lng']; // get ing for json

我希望用户的当前位置代替 1600 Amphitheatre Parkway, Mountain View, CA。

非常感谢任何帮助。

【问题讨论】:

  • 您是否要获取当前用户位置?即:坐标?如果是这样,您还想保存有关该位置的哪些其他信息。即:城市、州、邮编等
  • Yoel,非常感谢您的详细检查。将所有这些数据存储在数据库中会很棒——坐标、城市、州和邮政编码。

标签: javascript php geolocation google-api reverse-geocoding


【解决方案1】:

由于 PHP 运行在服务器端,因此无法使用 PHP 获取用户位置。您可以通过浏览器使用javascript获取用户位置。

这是一个例子。在此示例中,我将代码分成两个文件。一种用于使用 PHP (geocoordinates.php) 处理和存储信息,另一种 (HTML) 用于收集地理编码信息 (index.html),即 index.html。

您可以将这两个文件合并到 index.php 中,但为了简单起见,我会将它们分开。

geocoordinates.php

<?php

if(isset($_POST['lat'], $_POST['lng'])) {
    $lat = $_POST['lat'];
    $lng = $_POST['lng'];

    $url = sprintf("https://maps.googleapis.com/maps/api/geocode/json?latlng=%s,%s", $lat, $lng);

    $content = file_get_contents($url); // get json content

    $metadata = json_decode($content, true); //json decoder

    if(count($metadata['results']) > 0) {
        // for format example look at url
        // https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452
        $result = $metadata['results'][0];

        // save it in db for further use
        echo $result['formatted_address'];

    }
    else {
        // no results returned
    }
}

?>

index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Geocoding Page</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  <script>
  function getLocation() {
      if (navigator.geolocation) {
          navigator.geolocation.getCurrentPosition(savePosition, positionError, {timeout:10000});
      } else {
          //Geolocation is not supported by this browser
      }
  }

  // handle the error here
  function positionError(error) {
      var errorCode = error.code;
      var message = error.message;

      alert(message);
  }

  function savePosition(position) {
            $.post("geocoordinates.php", {lat: position.coords.latitude, lng: position.coords.longitude});
  }
  </script>
</head>
<body>
    <button onclick="getLocation();">Get My Location</button>
</body>
</html>

请记住,在此示例中,一旦用户单击“获取我的位置”,浏览器将提示用户允许地理定位。您也可以在页面加载后调用 getLocation 函数,但浏览器总是会请求用户的许可

您可以通过http://www.w3schools.com/htmL/html5_geolocation.asp了解更多有关地理定位的信息

【讨论】:

  • Yoel,浏览器表明它正在跟踪我的位置,但我无法完全回显位置结果或分别回显 $lng 或 $lat 变量。你知道怎么回事吗?
  • @pol_guy 我刚刚尝试过并遇到了同样的问题。我用错误回调更新了答案,以防它无法获得您的位置。显然这是一个已知问题,请查看stackoverflow.com/a/3885172/4114033
  • 干得好+1。快速且相当可靠。在我的测试中被 2 所房子淘汰了。谢谢!
  • 它可以在本地主机上工作吗?我尝试这样做,但它不起作用
【解决方案2】:

如上所述,PHP 是服务器端,所以我使用一个文件来获取 Long 和 Lat 坐标,然后将它们传递到我需要它们的位置....(不需要 API 密钥)

<?
session_start();
?>
<script>
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(getpos);
  } else {
  }
function getpos(position) {
           latx=position.coords.latitude;
           lonx=position.coords.longitude;
         // Show Lat and Lon 
           document.write('<div>Lat: '+latx+'<br> Long: '+lonx+'</div>');
         // or send them to use elsewhere
         //  location.href = '(your file name).php?latx='+latx+'&lonx='+lonx+'&shead=<? echo $shead ?>';
}
</script>

【讨论】:

    猜你喜欢
    • 2016-12-15
    • 2016-04-18
    • 1970-01-01
    • 2018-07-30
    • 1970-01-01
    • 1970-01-01
    • 2014-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多