Thursday, 20 March 2014

Filled Under:
,

how to get latitude and longitude from address in php

              you can easily find latitude and longitude using address.here i am using Google Maps API to get latitude & longitude using php.just copy below code and run this code to you can easily find latitude or longitude of any address in the world.



HTML Code :

<html>
<head><title>Simple</title></head>
<body>
<br />
    <form name="frm" id="frm" method="post" enctype="multipart/form-data" action="get_lati_longi.php">
        Type Your Location : <input type="text" name="myaddress" />
        <input type="submit" value="get lati longi">
    </form>
<br />

PHP Code :

<?php

if(isset($_POST['myaddress'])){
$myaddress = urlencode($_POST['myaddress']);
$url = "http://maps.googleapis.com/maps/api/geocode/json?address=$myaddress&sensor=false";
$getmap = file_get_contents($url); 
$googlemap = json_decode($getmap);

foreach($googlemap->results as $res){
     $address = $res->geometry;
     $latlng = $address->location;
     $formattedaddress = $res->formatted_address;


}
echo "Latitude: ". $latlng->lat ."<br />". "Longitude:". $latlng->lng; 
}
?>

</body>
</html>








2 comments: