How to get user Latitude, Longitude & current address using PHP?

Today, We want to share with you php get current location latitude longitude.In this post we will show you php latitude longitude, hear for How to Find Position with HTML5 Geolocation? we will give you demo and example for implement.In this post, we will learn about get current location address in php with an example.

how to get latitude and longitude from form in php?

 function showPosition(position) {
  var get_informations = position.coords.latitude + "," + position.coords.longitude;

  var img_url = "https://maps.googleapis.com/maps/api/staticmap?center=

   "+get_informations+"&zoom=14&size=400x300&sensor=false&key=YOUR_PUT_HERE_KEY";


   document.getElementById("mapholder").innerHTML = "<img src='"+img_url+"'>";

 }

index.php

<!DOCTYPE html>

<html>
<form method="post">
<input type="text" name="locations">
<input type="submit" name="submit" value="submit">
</form>
</html>

<?php

if(isset($_POST['submit']))
{
function getLatLong($locations){

    if(!empty($locations)){

        //Formatted locations

        $formattedAddr = str_replace(' ','+',$locations);

        //Send request and receive json data by locations

        $geocodeFromAddr = file_get_contents
('http://maps.googleapis.com/maps/api/geocode/json?locations='.$formattedAddr.'&sensor=false'); 

        $output = json_decode($geocodeFromAddr);

        //Get latitude and longitute from json data

        $data['lat_cordinate']  = $output->responses[0]->geometry->location->lat; 

        $data['lang_cordinate'] = $output->responses[0]->geometry->location->lng;


        if(!empty($data)){

            return $data;

        }else{

            return false;

        }

    }else{

        return false;  
 
    }

}

	$locations = $_POST['locations'];
	$latLong = getLatLong($locations);
	$lat_cordinate = $latLong['lat_cordinate']?$latLong['lat_cordinate']:' Sorry Not found';
	$lang_cordinate = $latLong['lang_cordinate']?$latLong['lang_cordinate']:'Sorry Not found';
	echo "Your Latitude:".$lat_cordinate."<br>";
	echo "Your lang_cordinate:".$lang_cordinate."";

}

?>

I hope you get an idea about Get location from latitude and longitude in PHP.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.