3

我正在寻找一种在访问我的网站时定位用户位置的方法,我尝试过 Maxmind,但它们在城市级别似乎不准确。我想要的信息是(国家、城市、经度、纬度)我希望国家和城市尽可能准确。

我也使用过 HTML5,但它要求我的用户共享位置信息的问题在我看来是不好的解决方案。(尽管我得到了非常准确的结果)

有什么解决办法吗?

注意:我发现谷歌搜索得到了准确的检测并且没有“要求分享我的位置”,但我没有找到任何使用谷歌服务的api

4

3 回答 3

3

这是我为使用谷歌地图的地理位置教程找到的代码。希望这很有用。

这适用于您连接到网络的方式。

  • 如果您使用宽带 ISP,它会为您提供固定 IP,您的位置会更准确。
  • 如果您使用移动设备连接到互联网,将显示您的网络从中获取信号的最近移动塔的位置

HTML5 地理位置示例

<!DOCTYPE html>
<html>
<head>
<title>GeoLocation Demo</title>
<meta charset="utf-8"/>
<script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>
<script>
  var startPos;
  var map;

  function init() {
      if (navigator.geolocation) {
          document.getElementById("support").innerHTML = "<p style='color:green'>Great! This browser supports HTML5 Geolocation</p>";
          navigator.geolocation.getCurrentPosition(updateLocation, handleLocationError, {
              timeout: 50000
          });
          //navigator.geolocation.watchPosition(updateCurrPosition,handleLocationError);

      } else {
          document.getElementById("support").innerHTML = "<p style='color:red'>Oops! This browser does not support HTML5 Geolocation</p>";
      }
  }

  function updateLocation(position) {
      startPos = position;
      var latitude = position.coords.latitude;
      var longitude = position.coords.longitude;

      //document.getElementById("startLat").innerHTML = latitude;
      //document.getElementById("startLon").innerHTML = longitude;

      var coords = new google.maps.LatLng(latitude, longitude);

      var mapOptions = {
          zoom: 10,
          center: coords,
          mapTypeControl: false,
          navigationControlOptions: {
              style: google.maps.NavigationControlStyle.SMALL
          },
          mapTypeId: google.maps.MapTypeId.ROADMAP
      };

      map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

      var marker = new google.maps.Marker({
          position: coords,
          map: map,
          title: "Your current location!"
      });

  }

  function handleLocationError(error) {
      switch (error.code) {
          case 0:
              updateStatus("There was an error while retrieving your location: " + error.message);
              break;

          case 1:
              updateStatus("The user prevented this page from retrieving the location.");
              break;

          case 2:
              updateStatus("The browser was unable to determine your location: " + error.message);

              break;

          case 3:

              updateStatus("The browser timed out before retrieving the location.");

              break;
      }
  }

  function updateStatus(msg) {
      document.getElementById("divStatus").innerHTML = msg;
  }
</script>
</head>

<body onload="init()">  
<div id="support"></div>
<div id="divStatus"></div>
<div id="map_canvas" style="width:600px;height:400px;"></div>
</body>
</html>
于 2012-11-02T06:23:20.410 回答
1
  1. www.ipfingerprints.com
  2. www.geoiptool.com/
  3. http://www.geobytes.com/TraceRouteLocator.htm
  4. http://whatismyipaddress.com/traceroute-tool
  5. http://www.dnsqueries.com/en/ip_geographical_informations.php

我希望这些链接可以帮助你。前两个链接有一个关于 IP 地址的地理位置信息数据库。而链接 3,4 和 5 在 traceroute 命令上工作。让我知道这些是否适合您。

于 2013-01-22T15:43:05.503 回答
0

IP 地址可能会更改取决于 ISP,我认为这就是地理位置数据库并不总是准确的原因。您可以比较这些地理位置站点之间的准确性,而不是 maxmind。 www.geobytes.com , www.hostip.info , www.ip2location.com

于 2012-11-02T10:03:31.347 回答