【问题标题】:Finding the nearest town/city寻找最近的城镇/城市
【发布时间】:2016-03-29 11:26:20
【问题描述】:

我正在寻找一种方法来查找最近的城镇/城市和浏览我网站的人所在的县。然后我希望它是这样的:

$town = "London, England";

或者类似的东西:

$town = "Bacup, Lancashire GB";

Google 已经成功地做到了这一点。当您搜索某些内容时,页面底部会显示“您的村庄 - 来自您的 Internet 地址”。

谢谢

【问题讨论】:

  • 为什么?你的理由是什么?
  • 你最有可能追求的是一个叫做“geolocation”的东西,Gogle 有一个 API 可以为你做这件事。如果这就是你的意思,那么已经为 php、html 和 jquery 准备好了 sn-ps 和库/跨度>
  • 是的,如果需要许可就可以了。如果用户在有 GPS 的移动/平板设备上浏览,它会自动使用它吗? @TheGeorgeL
  • 它也会在移动设备上请求许可。在大多数情况下,这会使用户对隐私感到恐惧并让他们关闭标签。
  • 你这样做到底是为了什么?

标签: php location


【解决方案1】:

由于您想知道查看您网站的人的位置,因此将从客户端计算机获取数据。 使用

navigator.geolocation.getCurrentPosition(showPosition);

用于获取客户端的位置。 showPosition 是要传递的函数名,所以用这个名字定义一个函数。如果您只想在客户端机器上显示位置,请执行以下操作:

function showPosition(position) {
    var x = document.getElementById("demo");
    x.innerHTML = "Latitude: " + position.coords.latitude + 
    "<br>Longitude: " + position.coords.longitude;  
}

或者您也可以将数据发送到您的服务器。

您可以在https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition 获得更多相关信息

现在您有了坐标(找出来,有多准确?),使用 google map location services api 获取位置名称。 Google Maps Geolocation API - https://developers.google.com/maps/documentation/geolocation/intro

【讨论】:

  • 对于 Chrome 5.0+、IE 9.0+、Firefox 3.5+、Safari 5.0+ 和 Opera 16.0+,因此请确保进行验证,例如:if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(success); } else { error('Geo Location is not supported'); }
【解决方案2】:

获取地理 IP 信息

请求地理 IP 服务器 (netip.de) 进行检查,返回 IP 所在的位置(主机、州、国家、城镇)。

<?php
   $ip='94.219.40.96';
   print_r(geoCheckIP($ip));
   //Array ( [domain] => dslb-094-219-040-096.pools.arcor-ip.net [country] => DE - Germany [state] => Hessen [town] => Erzhausen )

   //Get an array with geoip-infodata
   function geoCheckIP($ip)
   {
           //check, if the provided ip is valid
           if(!filter_var($ip, FILTER_VALIDATE_IP))
           {
                   throw new InvalidArgumentException("IP is not valid");
           }

           //contact ip-server
           $response=@file_get_contents('http://www.netip.de/search?query='.$ip);
           if (empty($response))
           {
                   throw new InvalidArgumentException("Error contacting Geo-IP-Server");
           }

           //Array containing all regex-patterns necessary to extract ip-geoinfo from page
           $patterns=array();
           $patterns["domain"] = '#Domain: (.*?)&nbsp;#i';
           $patterns["country"] = '#Country: (.*?)&nbsp;#i';
           $patterns["state"] = '#State/Region: (.*?)<br#i';
           $patterns["town"] = '#City: (.*?)<br#i';

           //Array where results will be stored
           $ipInfo=array();

           //check response from ipserver for above patterns
           foreach ($patterns as $key => $pattern)
           {
                   //store the result in array
                   $ipInfo[$key] = preg_match($pattern,$response,$value) && !empty($value[1]) ? $value[1] : 'not found';
           }

           return $ipInfo;
   }

?>

【讨论】:

  • 在 html5 浏览器中这是过时的,可以只使用 w3schools.com/html/html5_geolocation.asp 元素。
  • 我会说无论如何询问用户是个好主意,所以他们不会因为未经许可就这样做而生气。
  • 根据要求。好一个@TheGeorgeL
猜你喜欢
  • 2011-05-22
  • 1970-01-01
  • 1970-01-01
  • 2013-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多