Skip to main content

Get Current Location & Address in PHP





index.php
____________________________________________________________________

<!DOCTYPE html>
<head>
<title>GEt Current Location</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript">
jQuery(function($){
var lat = $('#lat');
var lng = $('#lng');
$('#submit').click(function(){
jQuery.ajax({
url: 'get_address.php',
type: 'post',
data: 'lat=' + lat.val() + '&lng='+lng.val(),
success: function(results){
$("#ack").val(results)
}
});
});
});
</script>

<script>
if(!navigator.geolocation){
alert('Your Browser does not support HTML5 Geo Location. Please Use Newer Version Browsers');
}
navigator.geolocation.getCurrentPosition(success, error);
function success(position){
var latitude  = position.coords.latitude;
var longitude = position.coords.longitude;
var accuracy  = position.coords.accuracy;
document.getElementById("lat").value  = latitude;
document.getElementById("lng").value  = longitude;
document.getElementById("acc").value  = accuracy;

}
function error(err){
alert('ERROR(' + err.code + '): ' + err.message);
}
</script>
<body>
<textarea id="ack" rows="4" cols="60"></textarea>
<input type="hidden" name="lat" id="lat" /><br />
<input type="hidden" name="lng" id="lng" /><br />
<input type="hidden" id="acc" />
<button id="submit">Get Current address</button>
</body>
</html>


get_address.php
____________________________________________________________________________
<?php
$latitude  = $_POST["lat"];
$longitude = $_POST["lng"];
$geolocation = $latitude.','.$longitude;
$request = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.$geolocation.'&sensor=false';
$file_contents = file_get_contents($request);
$json_decode = json_decode($file_contents);
for($i=0; $i<1; $i++) { echo $formatted_address = $json_decode->results[$i]->formatted_address; }
?>

Comments

Popular posts from this blog

transparent css

body {   font-family: 'Open Sans', sans-serif;   font-size: 14px;   line-height: 45px;   /*color: #666;*/   color: black;   background-color: #fff; } :hover {   text-decoration: none;   outline: none } .form-control {   background-color: transparent;   border-color: black;   height: 50px;   border-radius: 0;   box-shadow: none; } textarea.form-control {   min-height: 180px;   resize:none; } .form-group {   margin-bottom: 30px; } .contact-info {   padding-left:70px;   font-weight: 300; } ul.address {   margin-top: 30px;   list-style: none;   padding: 0;   margin: 0; } .btn-submit {   display: block;   padding: 12px;   width: 100%;   background:transparent;   color: #4985C6;   border:1px solid black;   margin-top: 40px; } .btn-submit:hover {   display: block;   padding: 12px; ...

HTML 5 pattern validation

<!DOCTYPE html> <html> <head> <title>html5 patterns val</title> </head> <body> <form action=""> Email :   <input type="email" name="email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$" required> Min Char:  <input type="password" name="pw" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}" title="Must contain at least one number and one uppercase and lowercase letter, and at least 8 or more characters" required> Search: <input type="search" name="search" pattern="[^'\x22]+" title="Invalid input" required> Password:   <input type="password" name="pw" pattern=".{6,}" title="Six or more characters" required> Country code: <input type="text" name="country_code" pattern="[A-Za-z]{3}" title="Three let...