【问题标题】:Ways to get location of the client from the browser?从浏览器获取客户端位置的方法?
【发布时间】:2011-09-24 09:45:20
【问题描述】:

我需要的是客户端的纬度/经度(通过浏览器)

在网上找到一些文章,在堆栈溢出本身找到一些(旧文章)Get GPS location from the web browser。差不多 18 个月前就已经回答了这个问题——想知道是否有其他(更有效的)方法可以从浏览器中获取用户位置信息。

到目前为止,找到了 2 个

使用 Maxmind

其他,使用google的api

w3c的geo api会有向下兼容性问题:http://dev.w3.org/geo/api/spec-source.html,所以不要选择。

找到了一个站点,www.drumaroo.com -- 在我们第一次进入该站点时请求共享该位置。需要类似的东西

【问题讨论】:

  • W3C GeoLocaiton API 是不依赖基于 IP 的位置数据库(如 Google 的)的唯一方法。您提供的示例网站同时使用了这两种 API。

标签: geolocation


【解决方案1】:

用户可以通过以下方式定位:-

  1. 使用 IP 地址。 由于网络地址的不确定性,最容易实现但最不可靠 / 拓扑。
  2. 使用纬度、经度对 最准确的解决方案,但大多数最终用户不知道他们的纬度, 经度值。
  3. 使用邮政编码 很好想,但很难实现,虽然 ZipCodes 被用于
    很长一段时间,但这还不是标准。 (见链接
    http://en.wikipedia.org/wiki/Postal_code)。仅邮政编码是不够的 计算距离我们需要将其转换为纬度、经度对,这是一个
    开销。
  4. 使用用户地址 最引人注目的想法,因为每个用户都至少有一个地理地址。 仅地址不足以计算距离,我们需要将其转换为 纬度、经度对,这是一个开销。

最好的选择是同时使用选项 2,3.4,您可以分两步完成:-

  1. 首先确定地址的经纬度。
  2. 然后使用这个 lat、long 值进行进一步处理,即存储在 数据库等

有关可用的各种选项,请参阅this question的答案

【讨论】:

【解决方案2】:

试试 HTML5 地理定位

 function init() {


 var mapOptions = {
    zoom: 8,
    mapTypeId: google.maps.MapTypeId.ROADMAP
 };


 map = new google.maps.Map(document.getElementById('map-canvas'),
  mapOptions);



if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
  var pos = new google.maps.LatLng(position.coords.latitude,
                                   position.coords.longitude);

  var infowindow = new google.maps.InfoWindow({
    map: map,
    position: pos,
    content: 'Location found using HTML5.'
  });

  map.setCenter(pos);
}

 } else {
alert('Geolocation not detected');   
}
}

【讨论】:

    【解决方案3】:

    这里是地理位置描述:

    http://www.w3schools.com/htmL/html5_geolocation.asp

    然后可以对 lat/lng 进行反向地理编码以查找国家/地区地址。等等

    https://developers.google.com/maps/documentation/javascript/examples/geocoding-reverse

    【讨论】:

      【解决方案4】:

      https://developers.google.com/maps/documentation/javascript/examples/map-geolocation 。只需点击 javascript+html 并将代码复制到 html 文件中。

      【讨论】:

        【解决方案5】:

        <!DOCTYPE html>
        <html>
        <head>
        
        <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
        <title>Reverse Geocoding</title>
        <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
        <script type="text/javascript">
        var geocoder;
        
        if (navigator.geolocation) {
            var location_timeout = setTimeout("geolocFail()", 10000);
        
            navigator.geolocation.getCurrentPosition(function(position) {
                clearTimeout(location_timeout);
        
                var lat = position.coords.latitude;
                var lng = position.coords.longitude;
        
                geocodeLatLng(lat, lng);
            }, function(error) {
                  alert("inside error ");
                clearTimeout(location_timeout);
                geolocFail();
            });
        } else {
              alert("Turn on the location service to make the deposit");
            // Fallback for no geolocation
            geolocFail();
        }
        
        function geolocFail(){
         alert("Turn on the location service to make the deposit");
         document.write("Turn on the location service to make the deposit");
        }
        
        /*if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
        }
        //Get the latitude and the longitude;
        function successFunction(position) {
        var lat = position.coords.latitude;
        var lng = position.coords.longitude;
        codeLatLng(lat, lng)
        
        }
        function errorFunction(){
        alert("Geocoder failed");
        } */
        
        function initialize() {
        geocoder = new google.maps.Geocoder();
        
        }
        function geocodeLatLng(lat, lng) {
        var latlng = new google.maps.LatLng(lat, lng);
        geocoder.geocode({'latLng': latlng}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
        console.log(results)
        if (results[1]) {
        //formatted address
        var add= results[0].formatted_address
        alert(add);
        
        //city data
        //alert(city.short_name + " " + city.long_name)
        
        } else {
        alert("No results found");
        }
        } else {
        alert("Geocoder failed due to: " + status);
        }
        });
        }
        </script>
        </head>
        <body onload="initialize()">
        </body>
        </html>

        【讨论】:

        • 您能否为 OP 添加一些关于此代码的作用的解释?谢谢
        【解决方案6】:

        这是跟踪设备(浏览器/移动)位置的 HTML 代码。只需将上述文件保存为 .html 扩展名并从系统或移动设备上的任何浏览器浏览它,它将显示用户当前位置。

        【讨论】:

          猜你喜欢
          • 2011-11-19
          • 1970-01-01
          • 2011-10-19
          • 2012-09-11
          • 2020-02-12
          • 1970-01-01
          • 2012-08-23
          • 2012-05-03
          相关资源
          最近更新 更多