【问题标题】:How to print curret user's address如何打印当前用户的地址
【发布时间】:2017-05-10 04:23:28
【问题描述】:

首先,请原谅我的英语。

嗨!我需要用 PHP 打印当前用户的地址,我有这个小脚本:

<?
  function getaddress($lat,$lng)
  {
     $url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.trim($lat).','.trim($lng).'&sensor=false';
     $json = @file_get_contents($url);
     $data=json_decode($json);
     $status = $data->status;
     if($status=="OK")
     {
       return $data->results[0]->formatted_address;
     }
     else
     {
       return false;
     }
  }
?>

<?php
  $lat= 21.884766199999998; //latitude
  $lng= -102.2996459; //longitude
  $address= getaddress($lat,$lng);
  if($address)
  {
    echo $address;
  }
  else
  {
    echo "Not found";
  }
?>

当然可以,但我不知道如何将 $lat 和 $long 变量更改为当前用户位置。

简而言之;如何将当前用户的经纬度位置传递给 PHP 变量以让该脚本正常工作?

【问题讨论】:

  • PHP 默认不提供此功能,您需要使用某种外部 API 来获取信息。
  • 感谢您的回答,我开始在 Google Maps Api 中搜索。
  • 如果用户有一些代理设置或防火墙来欺骗 IP 地址,没有人可以保证正确的地址或纬度语言

标签: javascript php location maps


【解决方案1】:
<html>
<body>
<p id="demo">Click the button to get your coordinates:</p>
<button onclick="getLocation()">Try It</button>
<script>
var x=document.getElementById("demo");
function getLocation()
  {
  if (navigator.geolocation)
    {
    navigator.geolocation.getCurrentPosition(showPosition);
    }
  else{x.innerHTML="Geolocation is not supported by this browser.";}
  }
function showPosition(position)
  {
  x.innerHTML="Latitude: " + position.coords.latitude + 
  "<br>Longitude: " + position.coords.longitude;    
  }
</script>
</body>
</html>

这是一个基于 javascript 的搜索。您可以在 html 浏览器中尝试它并将 lat long 传递给 php 脚本。

如果它可以帮助你,或者你可以告诉,我也有其他方法。

【讨论】:

    【解决方案2】:

    您需要使用 JavaScript 或其他 google api 来获取此信息。您可以将 lat & lang 放在单独的隐藏字段中,然后从该隐藏字段分配给您的 php 变量。

    这里是一个使用 html 5 和 java-script 的示例脚本

    <script>
    var x = document.getElementById("demo");
    function getLocation() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(showPosition);
        } else {
            x.innerHTML = "Geolocation is not supported by this browser.";
        }
    }
    function showPosition(position) {
        x.innerHTML = "Latitude: " + position.coords.latitude + 
        "<br>Longitude: " + position.coords.longitude; 
    }
    </script>
    

    这是另一个使用 google Geo-location API 的脚本

        <!DOCTYPE html>
    <html>
      <head>
        <title>Geolocation</title>
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
        <meta charset="utf-8">
        <style>
          /* Always set the map height explicitly to define the size of the div
           * element that contains the map. */
          #map {
            height: 100%;
          }
          /* Optional: Makes the sample page fill the window. */
          html, body {
            height: 100%;
            margin: 0;
            padding: 0;
          }
        </style>
      </head>
      <body>
        <div id="map"></div>
        <script>
          // Note: This example requires that you consent to location sharing when
          // prompted by your browser. If you see the error "The Geolocation service
          // failed.", it means you probably did not give permission for the browser to
          // locate you.
          var map, infoWindow;
          function initMap() {
            map = new google.maps.Map(document.getElementById('map'), {
              center: {lat: -34.397, lng: 150.644},
              zoom: 6
            });
            infoWindow = new google.maps.InfoWindow;
    
            // Try HTML5 geolocation.
            if (navigator.geolocation) {
              navigator.geolocation.getCurrentPosition(function(position) {
                var pos = {
                  lat: position.coords.latitude,
                  lng: position.coords.longitude
                };
    
                infoWindow.setPosition(pos);
                infoWindow.setContent('Location found.');
                infoWindow.open(map);
                map.setCenter(pos);
              }, function() {
                handleLocationError(true, infoWindow, map.getCenter());
              });
            } else {
              // Browser doesn't support Geolocation
              handleLocationError(false, infoWindow, map.getCenter());
            }
          }
    
          function handleLocationError(browserHasGeolocation, infoWindow, pos) {
            infoWindow.setPosition(pos);
            infoWindow.setContent(browserHasGeolocation ?
                                  'Error: The Geolocation service failed.' :
                                  'Error: Your browser doesn\'t support geolocation.');
            infoWindow.open(map);
          }
        </script>
        <script async defer
        src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
        </script>
      </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-10
      • 2012-11-04
      • 1970-01-01
      • 1970-01-01
      • 2012-06-11
      • 2017-02-21
      • 1970-01-01
      相关资源
      最近更新 更多