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.

<?php
$Address =”Street Address,City,Country”;
$Address = urlencode($Address);
$request_url = “http://maps.googleapis.com/maps/api/geocode/xml?address=”.$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=”http://maps.googleapis.com/maps/api/js”></script>
<script>
var myCenter=new google.maps.LatLng(<?php echo $Lat;?>,<?php echo $Lon;?>);
function initialize()
{
var mapProp = {
center:myCenter,
zoom:5,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
var map=new google.maps.Map(document.getElementById(“googleMap”),mapProp);
var marker=new google.maps.Marker({
position:myCenter,
});
marker.setMap(map);
}
google.maps.event.addDomListener(window, ‘load’, initialize);
</script>

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

Post visits: 16