Skip to Content
Google Maps Geocode Address with PHP
php code / April 20, 2018

     The present code is a Google Maps geocoding case with PHP and we likewise utilize some Google Maps JavaScript to demonstrate the geo-coded information on the guide

     You can utilize this code on the off chance that you need to make a store locator. Why this is helpful? Suppose you have a few locations on your database, you will never need to physically stick point the scope and longitude of every one of those addresses

    That is the reason we have the geocoding strategy. Simply input the addresses and Google will endeavor to distinguish the rough area of that address.

Code Here

Step by step Code

Step 1: Create container.php

<body class=””>
<div role=”navigation” class=”navbar navbar-default navbar-static-top”>
<div class=”container”>
<div class=”navbar-header”>
<button data-target=”.navbar-collapse” data-toggle=”collapse” class=”navbar-toggle” type=”button”>
<span class=”sr-only”>Toggle navigation</span>
<span class=”icon-bar”></span>
<span class=”icon-bar”></span>
<span class=”icon-bar”></span>
<a href=”#” class=”navbar-brand”>PHPCODE</a>
<div class=”navbar-collapse collapse”>
<ul class=”nav navbar-nav”>
<li class=”active”><a href=”#”>Home</a></li>
</div><!–/.nav-collapse –>
<div class=”container” style=”min-height:500px;”>
<div class=”>


Step 2: Create footer.php

<div class=”insert-post-ads1″ style=”margin-top:20px;”>

Step 3 : Create Function.php

// function to geocode address details
function getGeocodeData($address) {
$address = urlencode($address);
$googleMapUrl = “{$address}&key=YOUR_API_KEY”;
$geocodeResponseData = file_get_contents($googleMapUrl);
$responseData = json_decode($geocodeResponseData, true);
if($responseData[‘status’]==’OK’) {
$latitude = isset($responseData[‘results’][0][‘geometry’][‘location’][‘lat’]) ? $responseData[‘results’][0][‘geometry’][‘location’][‘lat’] : “”;
$longitude = isset($responseData[‘results’][0][‘geometry’][‘location’][‘lng’]) ? $responseData[‘results’][0][‘geometry’][‘location’][‘lng’] : “”;
$formattedAddress = isset($responseData[‘results’][0][‘formatted_address’]) ? $responseData[‘results’][0][‘formatted_address’] : “”;
if($latitude && $longitude && $formattedAddress) {
$geocodeData = array();
return $geocodeData;
} else {
return false;
} else {
echo “ERROR: {$responseData[‘status’]}”;
return false;

Step 4: Create index.php

<!DOCTYPE html>
<meta charset=”utf-8″>
<meta http-equiv=”X-UA-Compatible” content=”IE=edge”>
<meta name=”viewport” content=”width=device-width, initial-scale=1″>
<title>phpcode : Demo Google Maps Geocode Address with PHP</title>
<link rel=”stylesheet” href=”” />
<script src=””></script>
<link rel=”stylesheet” href=”style.css” />
<?php include(‘container.php’);?>
<div class=”container”>
<h2>Google Maps Geocode Address with PHP</h2>
if($_POST) {
// get geocode address details
$geocodeData = getGeocodeData($_POST[‘searchAddress’]);
if($geocodeData) {
$latitude = $geocodeData[0];
$longitude = $geocodeData[1];
$address = $geocodeData[2];
<div id=”gmap”>Loading map…</div>
<script type=”text/javascript” src=””></script>
<script type=”text/javascript”>
function init_map() {
var options = {
zoom: 14,
center: new google.maps.LatLng(<?php echo $latitude; ?>, <?php echo $longitude; ?>),
mapTypeId: google.maps.MapTypeId.ROADMAP
map = new google.maps.Map($(“#gmap”)[0], options);
marker = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(<?php echo $latitude; ?>, <?php echo $longitude; ?>)
infowindow = new google.maps.InfoWindow({
content: “<?php echo $address; ?>”
google.maps.event.addListener(marker, “click”, function () {, marker);
});, marker);
google.maps.event.addDomListener(window, ‘load’, init_map);
} else {
echo “Incorrect details to show map!”;
<div><strong>You can enter below example address or enter any other address to see map :</strong></div>
<div>1. Central Market Delhi</div>
<div>2. Fortis Hospital Noida</div>
<form action=”” method=”post”>
<div class=”row”>
<div class=”col-sm-4″>
<div class=”form-group”>
<input type=’text’ name=’searchAddress’ class=”form-control” placeholder=’Enter address here’ />
<div class=”form-group”>
<input type=’submit’ value=’Find’ class=”btn btn-success” />
<div style=”margin:50px 0px 0px 0px;”>
<a class=”btn btn-default read-more” style=”background:#3399ff;color:white” href=”#”>Back to Tutorial</a>
<?php include(‘footer.php’);?>

Step 5 Style.css 



Download  DEMO

PHPCODE © 2024