【问题标题】:GeoLocation using PHP使用 PHP 的地理位置
【发布时间】:2015-06-12 05:46:42
【问题描述】:

我复制粘贴以下代码,我对此很陌生。那你能指出错误吗?我很抱歉问了这么愚蠢的问题。

<HTML>

<HEAD>
</HEAD>

<BODY>
    <h3>Client side IP geolocation using <a href="http://ipinfo.io">ipinfo.io</a></h3>
    <hr/>
    <div id="ip"></div>
    <div id="address"></div>
    <hr/>Full response: <pre id="details"></pre>
</BODY>

</HTML>
<script type="text/javascript" charset="utf-8">
$.get("http://ipinfo.io", function(response) {
    $("#ip").html("IP: " + response.ip);
    $("#address").html("Location: " + response.city + ", " + response.region);
    $("#details").html(JSON.stringify(response, null, 4));
}, "jsonp");
</script>

【问题讨论】:

  • 您是否要求将该代码转换为等效的 PHP 代码?
  • IDK 只想运行它并根据 IP 地址获取地理位置并获取访问者的地址。它会起作用吗?
  • 我将此文件保存为 location.html
  • 你测试了吗?如果您在浏览器上查看 location.html,您会看到什么?
  • 只是 html 文本。我将在此处复制粘贴:使用 ipinfo.io 的客户端 IP 地理定位完整响应:

标签: php jquery geolocation


【解决方案1】:

HtML5 使用获取用户纬度、经度和获取当前用户地址。

示例代码:

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
        <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
    </head>
    <body>
        <p>Address: <div id="address"></div></p>
    </body>
    <script type="text/javascript" charset="utf-8">
    $(document).ready(function() {

        var currgeocoder;

        //Set geo location lat and long
        navigator.geolocation.getCurrentPosition(function(position, html5Error) {
            geo_loc = processGeolocationResult(position);
            currLatLong = geo_loc.split(",");
            initializeCurrent(currLatLong[0], currLatLong[1]);
        });

        //Get geo location result
        function processGeolocationResult(position) {
            html5Lat = position.coords.latitude; //Get latitude
            html5Lon = position.coords.longitude; //Get longitude
            html5TimeStamp = position.timestamp; //Get timestamp
            html5Accuracy = position.coords.accuracy; //Get accuracy in meters
            return (html5Lat).toFixed(8) + ", " + (html5Lon).toFixed(8);
        }

        //Check value is present or
        function initializeCurrent(latcurr, longcurr) {
            currgeocoder = new google.maps.Geocoder();

            console.log(latcurr + "-- ######## --" + longcurr);

            if (latcurr != '' && longcurr != '') {
                //call google api function
                var myLatlng = new google.maps.LatLng(latcurr, longcurr);
                return getCurrentAddress(myLatlng);
            }
        }

        //Get current address
        function getCurrentAddress(location) {
            currgeocoder.geocode({
                'location': location
            }, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    console.log(results[0]);
                    $("#address").html(results[0].formatted_address);
                } else {
                    alert('Geocode was not successful for the following reason: ' + status);
                }
            });
        }
    });
    </script>
</html>

演示:http://jsfiddle.net/hmy7e0fs/

【讨论】:

    【解决方案2】:
    <HTML>
    <HEAD>
    
    </HEAD>
    <script src="http://code.jquery.com/jquery-2.1.4.min.js" type="text/javascript" ></script>
    <script src="http://js.maxmind.com/js/geoip.js" type="text/javascript" ></script>
    <script type="text/javascript" charset="utf-8">
    
    
    $.get("http://ipinfo.io", function (response) {
    alert('hello');
        $("#ip").html("IP: " + response.ip);
        $("#address").html("Location: " + response.city + ", " + response.region);
        $("#details").html(JSON.stringify(response, null, 4));
    }, "jsonp");
    </script>
    <BODY>
    <h3>Client side IP geolocation using <a href="http://ipinfo.io">ipinfo.io</a></h3>
    
     <hr/>
    <div id="ip"></div>
    <div id="address"></div>
    <hr/>Full response: <pre id="details"></pre>
    </BODY>
    </HTML>
    

    【讨论】:

      【解决方案3】:

      使用这个:

      <?php 
      $ip = $_SERVER['REMOTE_ADDR'];
      $locationArray = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $ip));
      echo '<pre>';
      print_r($locationArray);
      
      ?>
      

      我希望 geoplugin 比http://ipinfo.io 更好,因为它每分钟的请求限制是最大值。

      欲了解更多信息,请访问:http://www.geoplugin.com/

      【讨论】:

      • 是的。顺便说一句,您应该检查 HTML5 之一。 IDK 我将使用两个中的一个。但你绝对是有用的人!
      • 您需要记住两件事。 (1) 使用 REMOTE_ADDR 应该是其他几个首先要检查的事情中的最后一个。例如,在我的情况下,它使用服务器并为此调用返回 404。 (2) 这只是大致准确的。 HTML5 会更好(但很难实现)或付费服务等。
      猜你喜欢
      • 2019-04-11
      • 2011-04-06
      • 2016-10-14
      • 1970-01-01
      • 2011-02-23
      • 2012-11-16
      • 2013-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多