【问题标题】:location detection in web browser [closed]Web浏览器中的位置检测[关闭]
【发布时间】:2012-10-22 19:28:27
【问题描述】:

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

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

有什么解决办法吗?

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

【问题讨论】:

    标签: html geolocation geoip ip-geolocation


    【解决方案1】:

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

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

    • 如果您使用的是宽带 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>
    

    【讨论】:

    • 因为我看到您的代码正在使用 HTML5,因此它会在推进之前向我的用户弹出“共享位置”请求。这对我来说是有问题的 ..而且我知道 HTML5 会强制弹出 ..但同时我看到谷歌在搜索中没有任何弹出窗口..有什么想法吗?
    • 我已阅读浏览器默认设置为禁用共享位置,因为这违反了用户的隐私,如果我们没有用户事先授权访问位置
    • 所以我认为您的代码将无法正常工作。我的问题是谷歌如何在没有它的情况下使其准确?我认为还有另一种解决方案
    • 我相信这是对您问题的一个非常准确的答案。我做过类似的事情。你可以在这里看到一个实际的例子saffie.ca/geo.html 随意检查源代码以查看代码的样子
    【解决方案2】:
    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 命令上工作。 让我知道这些是否适合您。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2013-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-13
        • 2020-09-25
        • 1970-01-01
        相关资源
        最近更新 更多