Geo-location Identifier

Now it easy to identify where user accessing your website. You can store the location, state and country. Here i’m giving the code to just show all there parameters. How you need customize.

Step1: Create the geonames.php

<?php
	session_start();

if ($_GET['type'] == 'JSON') {
     CURL_file_get_contents('http://ws.geonames.org/findNearbyPlaceNameJSON?lat='.$_GET['lat'].'&lng='.$_GET['lon']);
}

if ($_GET['type'] == 'XML') {
   CURL_file_get_contents('http://ws.geonames.org/findNearbyPlaceName?lat='.$_GET['lat'].'&lng='.$_GET['lon']);
}

function CURL_file_get_contents( $url ){
          $open_file = curl_init();
            // Tell CURL the url to open.
          curl_setopt($open_file, CURLOPT_URL, $url);
            // Tell CURL to return the file in a string.
          curl_setopt($open_file, CURLOPT_RETURNTRANSFER, 1);
            // Execute the CURL command and retrun the page in a string.
          $return_var = curl_exec($open_file);
          curl_close($open_file);         
  		 $abc = json_decode( $return_var);	  
		 $_SESSION['cityname'] 		= $abc->geonames[0]->name;
	     $_SESSION['state'] 		= $abc->geonames[0]->adminName1;
		 $_SESSION['countryName'] 	= $abc->geonames[0]->countryName;
		 echo $return_var;	 

}
?>

Step2: Create index.php (or) where you need to show the user location details place the code there.

Place this code before </head>

 <script type="text/javascript">

        function main() {

            // Check for geoLocation Support
            if (navigator.geolocation) {
                // YES                                                      
                // snapshot of position: 
                //navigator.geolocation.getCurrentPosition(renderPosition, renderError);
                // updating position:
                navigator.geolocation.watchPosition(renderPosition, renderError);
            } else {
                // NO
                $('#geoResults').html('<p>Your browser does not support geolocation.</p>');
            }
        }

        //jQuery to render the output
        function renderPosition(position) {

            if (!window.count) window.count = 0;
            var urlJSON ='';

            count ++;
            // var urlJSON = 'http://ws.geonames.org/findNearbyPlaceNameJSON?lat=-36.9104718&lng=174.9133483';
            var urlJSON = 'http://websitename.com/geonames.php?type=JSON&lat='+position.coords.latitude+'&lon='+position.coords.longitude;
            var urlXML  = 'http://websitename.com/geonames.php?type=XML&lat=' +position.coords.latitude+'&lon='+position.coords.longitude;

            // now get the XML reverse geo data
            $.getJSON(urlJSON, function(json) {

                /* Parse JSON objects */
                $.each(json.geonames,function(i,item) {
                    // get the name - which is the suburb - and update the page
                    $('#jsonResults').html('<p>You live in: ' + item.name + ' '+ item.adminName1 +' '+ item.countryName+'</p>');
                });

            });
        }

        function renderError() {
            $('#georesults').html('<p>The page could not get your location.</p>');
        }

    </script>

Place this div where you want to show the details

 <div id=”jsonResults”>your location</div>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s