Get your current geo location(php)

Get your current geo location(php) :

<!DOCTYPE html>

<html>
<body>

<p>Click the button to get your coordinates.</p>

<label><input type=”checkbox” onclick=”if(this.checked){getLocation()}“>get current location</label>

<p id=”demo”></p>

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 +

Longitude: ” + position.coords.longitude;
var lat = position.coords.latitude;
var lon = position.coords.longitude;
SetCookie(‘lat’,lat,1);
SetCookie(‘lon’,lon,1);

}
function SetCookie(cookieName,cookieValue,nDays) {
var today = new Date();
var expire = new Date();
if (nDays==null || nDays==0) nDays=1;
expire.setTime(today.getTime() + 3600000*24*nDays);
document.cookie = cookieName+”=”+escape(cookieValue)
+ “;expires=”+expire.toGMTString();
}

<?php
header(“Refresh:2; url=save.php”);
function getaddress($lat,$lng)
{
$url = ‘http://maps.googleapis.com/maps/api/geocode/json?latlng=&#8217;.trim($lat).’,’.trim($lng).’&sensor=false’;
$json = @file_get_contents($url);
$data=json_decode($json);
$status = $data->status;
if($status==”OK”)
return $data->results[0]->formatted_address;
else
return false;
}
if(!isset($_COOKIE[“lat”])||!isset($_COOKIE[“lon”])) {
echo “click checkbox”;
}
else
{
$lat= $_COOKIE[“lat”]; //latitude

$lng= $_COOKIE[“lon”];; //longitude

$address= getaddress($lat,$lng);
if($address)
{
echo $address;
}
else
{
echo “Not found”;
}

}
?>
</body>
</html>

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