有没有办法在用户不安装 Google Gears 的情况下获取用户位置?

发布于 2024-07-09 18:53:40 字数 250 浏览 14 评论 0原文

我不想让用户安装 Google Gears,这样我就可以向他展示他的猜测位置。 有没有一种方法无需使用 Google Gears 即可获取位置?

我找到了 http://www.wipmania.com/de/blog/google -geolocation-api/ 但它没有提供示例。

I don't want to have the user install Google Gears so I can show him his guessed location. Is there a way to get the location without having to use Google Gears?

I have found http://www.wipmania.com/de/blog/google-geolocation-api/ but it does not offer an example.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

缪败 2024-07-16 18:53:40

Google 提供了一种通过其 AJAX API 加载器 获取用户位置的方法。 即使没有安装 Gears,这也能正常工作。 只需包含一个指向 JsAPI 的脚本标记:

<script type="text/javascript" 
   src="http://www.google.com/jsapi?key=ABCDEFG">
</script>

当应用程序使用 AJAX API 加载程序时,加载程序会尝试根据客户端的 IP 地址对客户端进行地理定位。 如果此过程成功,则客户端的位置(范围为都市级别)将在 google.loader.ClientLocation 属性中提供。 如果进程未能找到匹配项,则该属性将设置为 null。

if (google.loader.ClientLocation){
  var center = new google.maps.LatLng(
    google.loader.ClientLocation.latitude,
    google.loader.ClientLocation.longitude
  );
  map.setCenter(center);
}

填充后,google.loader.ClientLocation 对象将填充以下都市级粒度属性:

  • ClientLocation.latitude — 提供与客户端 IP 地址关联的低分辨率纬度
  • ClientLocation.longitude< /code> — 提供与客户端 IP 地址关联的低分辨率经度
  • ClientLocation.address.city — 提供与客户端 IP 地址关联的城市名称
  • ClientLocation.address.country code> — 提供与客户端 IP 地址关联的国家/地区名称
  • ClientLocation.address.country_code — 提供与客户端 IP 地址 ClientLocation 关联的 ISO 3166-1 国家/地区代码名称
  • 。 address.region — 提供与客户端 IP 地址关联的国家/地区特定区域名称

Google provides a way to get the user location via it's AJAX API loader. This will even work without Gears installed. Simply include a script tag pointing to the JsAPI:

<script type="text/javascript" 
   src="http://www.google.com/jsapi?key=ABCDEFG">
</script>

When an application makes use of the AJAX API loader, the loader attempts to geo locate the client based on it's IP address. If this process succeeds, the client's location, scoped to the metro level, is made available in the google.loader.ClientLocation property. If the process fails to find a match, this property is set to null.

if (google.loader.ClientLocation){
  var center = new google.maps.LatLng(
    google.loader.ClientLocation.latitude,
    google.loader.ClientLocation.longitude
  );
  map.setCenter(center);
}

When populated, the google.loader.ClientLocation object is populated with the following metro-level granularity properties:

  • ClientLocation.latitude — supplies the low resolution latitude associated with the client's IP address
  • ClientLocation.longitude — supplies the low resolution longitude associated with the client's IP address
  • ClientLocation.address.city — supplies the name of the city associated with the client's IP address
  • ClientLocation.address.country — supplies the name of the country associated with the client's IP address
  • ClientLocation.address.country_code — supplies the name of the ISO 3166-1 country code associated with the client's IP address
  • ClientLocation.address.region — supplies the country specific region name associated with the client's IP address
溺渁∝ 2024-07-16 18:53:40

这通常称为 IP 地理定位。

此处就是一个示例。

问题是,大多数网站(如果您打算将其称为网络服务)都会向您收费。 否则,组合一个 Web 服务来获取地理位置页面,对其进行解析以获取地址,然后返回该信息。

在 PHP 中,这似乎工作得很好:

<?php
 if(isset($_GET["ip"])){
   echo geolocate($_GET["ip"]);
 }

 function geolocate($ip){
   $raw_html = file_get_contents("http://www.geody.com/geoip.php?ip=$ip");
   if(preg_match('/Location:(.*)/',$raw_html,$matches)){
     $location_raw = $matches[1];

     //Get rid of pesky HTML tags
     $location = preg_replace("/<[^>]*>/","",$location_raw);
     return $location;
   }else{
     return "ERROR";
   }
 }

This is typically called IP Geolocation.

An example is here.

The thing is, most sites (if you plan on calling this as a web service) will charge you for it. Otherwise, throw together a web service that grabs a geolocation page, parses it for the address, and returns that piece of information.

In PHP, this seems to work pretty well:

<?php
 if(isset($_GET["ip"])){
   echo geolocate($_GET["ip"]);
 }

 function geolocate($ip){
   $raw_html = file_get_contents("http://www.geody.com/geoip.php?ip=$ip");
   if(preg_match('/Location:(.*)/',$raw_html,$matches)){
     $location_raw = $matches[1];

     //Get rid of pesky HTML tags
     $location = preg_replace("/<[^>]*>/","",$location_raw);
     return $location;
   }else{
     return "ERROR";
   }
 }
本王不退位尔等都是臣 2024-07-16 18:53:40

使用地理定位服务或图书馆需要注意的一件事是它们永远不会 100% 准确。 我们使用一个 IP2Location,它实际上一直是我们客户服务部门投诉的一个稳定来源。 我们正在寻找改变该网站行为的方法,并权衡将其完全删除的因素。

问题在于,将 IP 与用户关联的负担由用户 ISP 承担。 它们拥有大块的 IP 范围,并注册哪些块属于特定位置。 问题是 ISP 在移动块或块覆盖太大区域时通常不会更新此信息。

例如,我住在曼哈顿以北约六十英里处,但我的 IP 经常显示我在芝加哥,并且拥有大部分查找工具。 我有 FIOS,并且与其他几个人交谈过,他们也证实了同样的行为。

One thing to watch out for with an geolocation service or library is that they will never be 100% accurate. We use one, IP2Location, and it actually has been a steady source of complaints to our customer service department. We are looking at ways to alter the behavior of the site and weighing the factors of removing it completely.

The problem lies in the fact that the burden of associating the IP with a user is with the users ISP. They have large blocks of IP ranges, and they register which blocks belong to a specific location. The problem is that ISPs often don't update this information when they shift blocks or a block covers too large of an area.

For example, I live about sixty miles north of Manhattan, but my IP often shows that I am in Chicago with most of the lookup tools. I have FIOS, and have talked to a couple of other people who have confirmed the same behavior.

前事休说 2024-07-16 18:53:40

您可以使用 HTML5 来实现此目的。 这很简单。 这里是示例。

You can use HTML5 for this purpose. That is pretty simple. Here is example.

泪冰清 2024-07-16 18:53:40

如果您的用户使用移动设备,Xitfy 也是一个免费选项。 他们有一个小客户端,在用户同意后可以无缝安装在他们的手机上。 然后,您可以调用基于 REST 的 api,从 GPS/wifi 或手机信号塔获取准确的经纬度。

它们支持Android、Windows Mobile、BlackBerry 和Symbian,几乎涵盖了所有智能手机。

If your users are on mobile, Xitfy is also a free option. They have a small client that after the user agrees can be seamlessly installed on their handset. You can then call a REST based api to get their exact lat/lon sourced from gps / wifi or cell tower.

They support Android, Windows Mobile, BlackBerry and Symbian which covers almost all smartphones.

冷血 2024-07-16 18:53:40

您可以使用这个简单的代码对 IP 地址进行地理编码。 效果很好。 尝试这个:

function functionname(Your IP Here)
{

    $ip = $ClientIP;

    $json = file_get_contents("http://api.easyjquery.com/ips/?ip=".$ip."&full=true");

    $json = json_decode($json,true);

    return $json;
}

You can geocode the IP address using this simple code. It works great. Try this:

function functionname(Your IP Here)
{

    $ip = $ClientIP;

    $json = file_get_contents("http://api.easyjquery.com/ips/?ip=".$ip."&full=true");

    $json = json_decode($json,true);

    return $json;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文