techfolks php tutorials logo
PHP Uncategorized

How to use Google map Address in PHP

Let’s Start, First we Getting latitude and longitude from Address in Google Maps Using PHP. The script is given below.

Google map Address in PHP

$Address =”Street Address,City,Country”;
$Address = urlencode($Address);
$request_url = “”.$Address.”&sensor=true”;
$xml = simplexml_load_file($request_url) or die(“url not loading”);
$status = $xml->status;
if ($status==”OK”) {
$Lat = $xml->result->geometry->location->lat;
$Lon = $xml->result->geometry->location->lng;
$LatLng = “$Lat,$Lon”;
The idea is simple. Make a request to Google Maps server, and it will return an XML (or JSON). Then, parse the XML to get the latitude and longitude.

Now we have latitude and longitude of the given address.Our next step is to display the Google Map.This can be done using  Java Script.Code for this is:

<script src=””></script>
var myCenter=new google.maps.LatLng(<?php echo $Lat;?>,<?php echo $Lon;?>);
function initialize()
var mapProp = {
var map=new google.maps.Map(document.getElementById(“googleMap”),mapProp);
var marker=new google.maps.Marker({
google.maps.event.addDomListener(window, ‘load’, initialize);

In Above code, we just pass the latitude and longitude point (,).

See $Lat for latitude and $Lon for longitude .

In script code iset the zoom level to 5 and lat lon location to center.

After this we add a HTML container to display the map and code for this is

I used googleMap id of this container for reference to java script code .See this line of code “var map=new google.maps.Map(document.getElementById(“googleMap“),mapProp);”

After implementing this script you seen the Google map of assigned location .

Google map using address in php