How to Access the Google Maps API in JSON format with PHP 


               


How to Access the Google Maps API in JSON Format with PHP

PHP



Enter An Address to Find Latitude and Longitude Values:







In this article, we go over how to access the google maps API encoded in JSON format with PHP.

JSON is Javascript Object Notation.

PHP has built-in functions that allows it to work easily with JSON data.

We'll show how to obtain JSON data from the google maps API and work with it in PHP.

So in this example code that we'll make, we will extract the latitude and longitude of a city using the google maps API. So we put in a location, such as Detroit Michigan, and we will get the latitude and longitude values associated with Detroit Michigan from the google maps API.

So to access the google maps API, the URL is, https://maps.googleapis.com/maps/api/geocode/json?address=detroit,michigan

So the hyperlink above brings us to the google maps API where the address is detroit, michigan.

The generic link to access the google maps API page of any location is,

https://maps.googleapis.com/maps/api/geocode/json?address=

After the word address, you simply put in the location that you want to enter. This can be any type of location such as a state or a city in a state or a full street address. You can really put anything after address.

Using this base URL, we can have a user enter in an address, concatenate it to the above URL, putting in the address in the URL, retrieve the page with PHP, decode the JSON data with PHP, and then extract the data that we want from the API. As said before, we will extract the latitude and longitude associated with the address.

So let's go on to the code necessary to do this. This is shown below. We'll separate it into blocks.

First Block of PHP Code

So the first block of PHP code does the majority of the work as far as PHP code is concerned.

It takes the address from the form that a user has entered and puts into the URL representing the google maps API. It then retrieves the page, decodes the JSON data, and extracts the latitude and longitude value from the google maps API.

So we'll go through the code above.

So we create a PHP variable named $location. We use the PHP superglobal array, $_POST to extract the data from the text box with the address that the user has entered in. So now we have the address that the user has entered in.

We then create an if statement so that we can know whether the data has been entered into the text box or not. We do not want the following code to run if the text box is empty.

We then create the a $url variable. This holds the URL of the google maps api for the address entered in. We have the base of the URL and then we have in the address ($location variable) by the concatenator. We put it in a urlencode() function so that if the user enters spaces in the address, this is accounted for.

We then create a variable called $jsoncontent. THis holds the all of the data produced in the goole maps api. Since this data is in JSON format, I call this data $jsoncontent.

Next, we decode the json data with the PHP json_decode() function. This converts the JSON data into a PHP array.

Lastly, we extract the latitude and longitude values from this google maps api data.

Since the latitudes and longitudes are found under the first element of results, this is located in ['results'][0]. Under results, it is found in geometry. Under geometry, it is found in location. In location, the attribute is lat for latitude and lng for longitude.

We create the PHP variable $latitude and $longitude and set them equal to these points.

HTML Code

Next we have the HTML code that we need so that we can create a form where a user can enter the address into.

It's a very simple form that just has a text box for entering the address and a submit button for submitting the data through.



So here we have a form. Since we want the form to submit the data input into to this current page, we leave the action attribute empty. Therefore, the data entered into the form stays on this page. The method is POST, since we don't need it appended to the current page's URL. Instead we want it appended to the URL we have in the code. Using POST, therefore, is better than using GET.

We then create the text box and assign it the name location_entered. We set its value equal to $location, which is the PHP variable that stores the address that the user has entered. This is so that we can see the address that the user has entered into the text box even after the submit button is clicked.

We then have a submit button so that the form can be submitted to the server.

This concludes the HTML code.

Second Block of PHP Code

We then have our last block of PHP code, which is very simple, put underneath the text box.



The last block of PHP simply outputs the latitude and longitude for the address that has been entered in.

And this concludes how we can use the google maps API to get data in JSON format such as the latitude and longitude of an address.

All it is is a matter of knowing the URL of the google maps API and how you can enter an address and append it to the URL to get the data on the address you want. You then work with the data as you would with any JSON data with PHP.

So this is how it is done when the API is presented in JSON format. You can also do the same exact thing if the data is in XML format. To see how to use the google maps API with the data in XML format to access information, see How to Access the Google Maps API in XML Format with PHP.


Related Resources

How to Obtain the Zip Code from the Google Maps API Using PHP




HTML Comment Box is loading comments...