How to obtain latitude and longitude of a user by IP address or pinpoint location

后端 未结 2 1099
醉话见心
醉话见心 2021-02-10 13:46

I want to get the geolocation (latitude and longitude) of a computer user by a php script. I was using this one



        
相关标签:
2条回答
  • 2021-02-10 14:11

    download geoip.inc - http://www.maxmind.com/download/geoip/api/php-20120410/geoip.inc, geoipcity.inc - http://www.maxmind.com/download/geoip/api/php-20120410/geoipcity.inc, geoipregionvars.php - http://www.maxmind.com/download/geoip/api/php-20120410/geoipregionvars.php,

    GeoLiteCity.dat - http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz please convert GeoLiteCity.dat.gz to GeoLiteCity.dat and put in geoip named folder

        include("application/libraries/geoip/geoipcity.inc");
        include("application/libraries/geoip/geoipregionvars.php");
    
        $giCity = geoip_open("application/libraries/geoip/GeoLiteCity.dat", GEOIP_STANDARD);
    
        $ip =$_SERVER['REMOTE_ADDR'];
        $record = geoip_record_by_addr($giCity, $ip);
    
        echo "Getting Country and City detail by IP Address <br /><br />";
        echo "IP: " . $ip . "<br /><br />";
    
        echo "Country Code: " . $record->country_code .  "<br />" .
        "Country Code3: " . $record->country_code . "<br />" .
        "Country Name: " . $record->country_name . "<br />" .
        "Region Code: " . $record->region . "<br />" .
        "Region Name: " . $GEOIP_REGION_NAME[$record->country_code][$record->region] . "<br />" .
        "City: " . $record->city . "<br />" .
        "Postal Code: " . $record->postal_code . "<br />" .
        "Latitude: " . $record->latitude . "<`enter code here`br />" .
        "Longitude: " . $record->longitude . "<br />" .
        "Metro Code: " . $record->metro_code . "<br />" .
        "Area Code: " . $record->area_code . "<br />" ; 
    
    0 讨论(0)
  • 2021-02-10 14:15

    I think this is what you want.simple and easy to use.Thanks to HTML5 see source here

    <html>
    <body>
    <p id="demo">Click the button to get your coordinates:</p>
    <button onclick="getLocation()">Try It</button>
    <script>
    var x=document.getElementById("demo");
    function getLocation()
      {
      if (navigator.geolocation)
        {
        navigator.geolocation.getCurrentPosition(showPosition);
        }
      else{x.innerHTML="Geolocation is not supported by this browser.";}
      }
    function showPosition(position)
      {
      x.innerHTML="Latitude: " + position.coords.latitude + 
      "<br>Longitude: " + position.coords.longitude;    
      }
    </script>
    </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题