Developing a custom store locator map: Create a PHP file to output the XML

Welcome to the fourth post in our series on creating your own store locator map. In this post we are going to create an XML file which will process the calls to the database in a browser.

Copy and paste this code into a blank text file:


// Get parameters from URL
$center_lat = $_GET&#91;"lat"&#93;;
$center_lng = $_GET&#91;"lng"&#93;;
$radius = $_GET&#91;"radius"&#93;;

// Start XML file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("Sheet1");
$parnode = $dom->appendChild($node);

// Opens a connection to a mySQL server
$connection=mysql_connect (localhost, $username, $password);
if (!$connection) {
  die("Not connected : " . mysql_error());

// Set the active mySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
  die ("Can\'t use db : " . mysql_error());

// Search the rows in the Sheet1 table
$query = sprintf("SELECT address, name, lat, lng, ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20",
$result = mysql_query($query);

$result = mysql_query($query);
if (!$result) {
  die("Invalid query: " . mysql_error());

header("Content-type: text/xml");

// Iterate through the rows, adding XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
  $node = $dom->createElement("Sheet1");
  $newnode = $parnode->appendChild($node);
  $newnode->setAttribute("name", $row['name']);
  $newnode->setAttribute("address", $row['address']);
  $newnode->setAttribute("lat", $row['lat']);
  $newnode->setAttribute("lng", $row['lng']);
  $newnode->setAttribute("distance", $row['distance']);

echo $dom->saveXML();

Save it as getoutput.php.  Upload the file to your server. To make sure that the XML is outputting correctly, go to You should see output like this:

<?xml version="1.0"?>
<Sheet1><marker name="Location #1" address="Address 1" lat="37.402653" lng="-122.079353" distance="0.38091455044131"/>
<marker name="Location #2" address="Address 2" lat="37.393887" lng="-122.078918" distance="0.5596115438175"/>
<marker name="Location #3" address="Address 3" lat="37.387138" lng="-122.083237" distance="1.0796074495809"/>

Once we have the XML working in the browser, we are ready to move on to creating the actual map.

Here are links to all of the posts in this series on how to create your own store locator map:


Create a spreadsheet of your store locations

Create and populate the MySQL table

Create a PHP file which will be used to connect to the database

Create a PHP file which will output the XML file results of a search

Create the HTML page which contains the store locator map


 And here are some hints and tips to help create and customize your store locator map:

Using your own custom markers for the locations in place of Google’s default markers

How to prevent the map from zooming in to close on a single location