Get latitude and longitude from an address with PHP and Google Maps API - Manoj Patial

Get latitude and longitude from an address with PHP and Google Maps API

Which CMS is Best? HubSpot CMS/COS or WordPress CMS
April 20, 2018
Codeigniter – Basic Introduction
April 21, 2018

Get latitude and longitude from an address with PHP and Google Maps API

Here I am going to show you how to get coordinates from address using google api based on the town, city or country location.

Let’s start with the following snippet:

<?php

	$address = '740 Story Rd San Jose CA 95122';
	$coordinates = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($address) . '&sensor=true');
	$coordinates = json_decode($coordinates);

	echo 'Latitude:' . $coordinates->results[0]->geometry->location->lat;
	echo 'Longitude:' . $coordinates->results[0]->geometry->location->lng;

	$latitude = $coordinates->results[0]->geometry->location->lat;
	$longitude = $coordinates->results[0]->geometry->location->lng;
?>

As you can see above simple piece of code. First, grab the address into a variable (in this case $address), then use file_get_contents and the Google Maps API.

The next step is to decode the JSON string into array using json_decode.

Now, let’s display the Latitude and Longitude results. You should see something like:

Latitude:45.5064121Longitude:-73.6397903

Finally, we have to use these variables in our script for show the location on the map:

<script>
		function initialize() {
		  var mapOptions = {
			zoom: 15,
			center: new google.maps.LatLng('<?php echo $latitude ?>', '<?php echo $longitude ?>')
		  };
		  var map = new google.maps.Map(document.getElementById('map'),
			  mapOptions);
		}

		google.maps.event.addDomListener(window, 'load', initialize);
 </script>

Checkout full code below:

<?php

	$address = '3500 Jean Talon Ouest, Montreal, QC H3R 2E8 Canada';
	$coordinates = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($address) . '&sensor=true');
	$coordinates = json_decode($coordinates);

	$latitude = $coordinates->results[0]->geometry->location->lat;
	$longitude = $coordinates->results[0]->geometry->location->lng;

?>

<!DOCTYPE html>
<html>
  <head>
    <title>Localizing the Map</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&language=en"></script>
    <script>
		function initialize() {
		  var mapOptions = {
			zoom: 15,
			center: new google.maps.LatLng('<?php echo $lat ?>', '<?php echo $lng ?>')
		  };
		  var map = new google.maps.Map(document.getElementById('map-canvas'),
			  mapOptions);
		}

		google.maps.event.addDomListener(window, 'load', initialize);
    </script>
  </head>

  <body>
    <div id="map"></div>
  </body>
</html>
5 1 vote
Article Rating
Manoj Patial
Manoj Patial
I am Manoj Patial, a website developer from India, I have more than 10 years of experience in website development. I developed more than 200+ website using Drupal, WordPress, PHP, Codeigniter, shopify, HubSpot and other CMS/PHP frameworks.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Pin It on Pinterest

0
Would love your thoughts, please comment.x
()
x