Saturday, September 27, 2008

PHP: Using Google Maps to find a location

You can use Google Maps service to find a location, for example a street address. In this example we create a web page with a text field and a button to find a location based on a street address. We create a small PHP program to use Google Maps to show the entered location on the map. See the example running here.

Create a new HTML/PHP file using your text editor. Place a form on the page as in listing 1. On the form there will be a text field and a button.

Listing 1. The form code. Click on the picture to see it in it's original size.



Next, add a PHP script block in the very top of the document. Nothing - even an empty row or a space - cannot be before the block. See listing 2.

Listing 2. The PHP script block and code. Click on the picture to see it in it's original size.



Save the file under your web server directory with .php extension by the name find.php (or equivalent to the PHP file name in your form's action attribute). Open the page in your browser using http. Enter a location in the address field, for example Mannerheimintie 1, 00100 Helsinki, and hit the find button. The location should now be opened on the Google Maps.

No comments: