0

I copied paste following code and I am a noob at this. So can you point out error please? I am sorry for asking such a stupid question.

<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>
10
  • Are you asking to convert that code in PHP equivalent? Commented Jun 12, 2015 at 5:51
  • IDK Just want to run this and get geolocation based on IP address and get address of visitor. Will it work? Commented Jun 12, 2015 at 5:54
  • I saved this file as location.html Commented Jun 12, 2015 at 5:54
  • Did you test it? if you view location.html on your browser what do you see? Commented Jun 12, 2015 at 5:56
  • Just the html text. I'll copy paste here: Client side IP geolocation using ipinfo.io Full response: Commented Jun 12, 2015 at 5:59

3 Answers 3

4

HtML5 using get user latitude, longitude and get current user address.

Sample code:

<!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>

Demo: http://jsfiddle.net/hmy7e0fs/

Sign up to request clarification or add additional context in comments.

Comments

0
<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>

Comments

0

Use this:

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

?>

I would like to prefer geoplugin better than http://ipinfo.io as its request limit per minute is max.

For more info please visit: http://www.geoplugin.com/

2 Comments

Yeah. BTW you should check HTML5 one. IDK which one of two I will use. But yours definitely useful man!
You need to keep two things in mind here . (1) using REMOTE_ADDR should be the last of several other things to check first. For example in my case it used the server and returned 404 for this call. (2) this is only going to be roughly accurate. HTML5 would be much better (but hard to implement) or a pay service etc. .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.