ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

javascript – 如何使用Google Geocode API和PHP获取用户的当前位置

2019-06-12 05:15:59  阅读:275  来源: 互联网

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


我创建了一个应用程序,并且作为数据收集的一部分,我想在用户使用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.

非常感谢任何帮助.

解决方法:

由于它在服务器端运行,因此无法使用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了解有关地理定位的更多信息

标签:php,javascript,google-api,geolocation,reverse-geocoding
来源: https://codeday.me/bug/20190612/1223592.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有