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

PHP



Enter the location:






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

XML stands for Extensible Markup Language and it is a very popular type of data exchage used on the web for a variety of things, including as functioning as a data exchange format language for APIs.

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

We'll show how to obtain XML 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 Honolulu, Hawaii, and we will get the latitude and longitude associated with Honolulu, Hawaii from the google maps API.

So to access the google maps API, the URL is, https://maps.googleapis.com/maps/api/geocode/xml?address=honolulu,hawaii

So the hyperlink above brings us to the google maps API where the address is Honolulu, Hawaii.

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, loads in the XML 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 $xmlcontent. THis holds the all of the data produced in the goole maps api. Since this data is in XML format, I call this data $xmlcontent.

Next, we load the data into the memory of our file using the PHP simplexml_load_string() function. This method loads the content obtained in the $xmlcontent variable, which is the content retrieved by the PHP file_get_contents() function. Even though it's already retrieved in by the PHP file_get_contents() function, it isn't loaded in in simple XML format. Therefore, we have to then pass this data into the simplexml_load_string() function. We now have all of the XML data ready to parse through.

Being that we want the latitude and the longitude for the address the user enters in, we target these 2 values in the xML document.

The latitude and longitude values are both found under result, geometry, location section. The latitude is found in the tags lat. The longitude is found in the tags lng.

So therefore, going forward from the loading point, we have $xmlload->result->geometry->location->lat for the latitude. And we have $xmlload->result->geometry->location->lng for the longitude.

These give us the latitude and longitude values for the address entered in.

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

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 XML 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 XML data with PHP.

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


Related Resources

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




HTML Comment Box is loading comments...