how to get current latitude and longitude in php?

Today, We want to share with you how to get current latitude and longitude in php.In this post we will show you get current location address in php, hear for get current location latitude and longitude jquery we will give you demo and example for implement.In this post, we will learn about Laravel 6 Get City Country By IP Address with an example.

php get current location latitude longitude

Get latitude and longitute from json data

<!DOCTYPE html>

<html>

<form method="post">

<input type="text" name="address">

<input type="submit" name="submit" value="submit">

</form>

</html>

<?php

if(isset($_POST['submit']))

{


function getLatLong($address){

    if(!empty($address)){

        //Formatted address

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

        //Send request and receive json data by address

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

        $output = json_decode($geocodeFromAddr);

        //Get latitude and longitute from json data

        $data['latitude']  = $output->results[0]->geometry->location->lat; 

        $data['longitude'] = $output->results[0]->geometry->location->lng;

        //Return latitude and longitude of the given address

        if(!empty($data)){

            return $data;

        }else{

            return false;

        }

    }else{

        return false;  
 
    }

}

	$address = $_POST['address'];
	$latLong = getLatLong($address);
	$latitude = $latLong['latitude']?$latLong['latitude']:'Not found';
	$longitude = $latLong['longitude']?$latLong['longitude']:'Not found';
	echo "Your Latitude:".$latitude."<br>";
	echo "Your longitude:".$longitude."";

}

?>

add geolocation to website

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

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

   "+latlon+"&zoom=14&size=400x300&sensor=false&key=fhfhSd87sdsdD89825dfDFDDD454d578ds88s8";


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

 }

I hope you get an idea about get longitude and latitude javascript.
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.