Company Name
Company News

Uresk Links Pro
Keep track of your links and count the clicks they get.

Uresk Email Tracker
Save email hosting costs by only sending email to those who click.

Uresk Quick Gallery
Easily & Quickly create a picture gallery.

Uresk TAF
Fast & Easy Tell-A-Friend software.

Track Platform
Track clicks, ROI, email opens, and do A/B Testing.

More Coming Soon!

Customer Login

Forum
Discuss our products & get web development help.

Google Maps Tutorial
Learn how to graphically show visitors to your website, using Google Maps.

Services We Offer

Web Development
Professional web development and programming.

Graphic & Website Design
We can create great-looking websites and other collateral.

 

Using Google Maps to show the geographical location of your website's visitors

Part IV – Creating the XML

Now that we have all that data, we need to return it in a usable manner, so that we can parse it all and display it in the map. There are several ways to do that, but the best way is to use XML.

We could just echo out XML strings – this is a fairly simple XML document. We’ll go ahead and use the DOM-XML object in PHP 5 though, just to be clean.

So, first we need to create the document & root element:

$dom = new DOMDocument( '1.0', 'UTF-8' );
$rootElement = $dom->createElement("locations");

Then, attach the root element to the document:

$dom->appendChild($rootElement);

We want to show them on the map as a green pin, so we'll use the code from the previous page in here to add that one first:

mysql_connect( 'localhost', 'username', 'password' );
$db = mysql_select_db( 'db' );

$license_key = 'xxx';
$ipaddress = $_SERVER['REMOTE_ADDR'];

$sql = "SELECT *
            FROM visitor_log
            WHERE ip = '{$ipaddress}'";

$c = mysql_num_rows(mysql_query($sql));

if( $c > 0 ){
 $info = mysql_fetch_array(mysql_query($sql));
 $sql = "INSERT
             INTO visitor_log
            (ip,lat,lng,city,state,country)
            VALUES('{$ipaddress}','{$info['lat']}','{$info['lng']}', '{$info['city']}', '{$info['state']}','{$info['country']}')";
} else{
 $query = "http://maxmind.com:8010/f?l=" . $license_key . "&i=" . $ipaddress;
 $url = parse_url($query);
 $host = $url["host"];
 $path = $url["path"] . "?" . $url["query"];
 $timeout = 1;
 $fp = fsockopen ($host, 8010, $errno, $errstr, $timeout)
 or die('Can not open connection to server.');
 if ($fp) {
  fputs ($fp, "GET $path HTTP/1.0\nHost: " . $host . "\n\n");
  while (!feof($fp)) {
   $buf .= fgets($fp, 128);
   }
  $lines = split("\n", $buf);
  $data = $lines[count($lines)-1];
  fclose($fp);
 } else {
 # enter error handing code here
 }
 $dataArray = split( ",", $data );
 $lat = $dataArray[4];
 $lng = $dataArray[5];
 $city = $dataArray[2];
 $state = $dataArray[1];
 $country = $dataArray[0];
 $sql = "INSERT
            INTO visitor_log
            (ip,lat,lng,city,state,country)
            VALUES ('{$ipaddress}','{$lat}','{$lng}','{$city}','{$state}','{$country}')";
 }
mysql_query($sql);

$element = $dom->createElement( 'location' );
$element->setAttribute( 'lat', $lat );
$element->setAttribute( 'lng', $lng );
$text = $dom->createCDATASection( $city. ', ' . $state . ', ' . $country );
$element->appendChild( $text );
$rootElement->appendChild($element);


Now, query for the last 100 unique visits or so, except for the person viewing the page:


$sql = "SELECT *
            FROM visitor_log
            WHERE lat != {$lat} and lng != {$lng}
            GROUP BY lat, lng
            ORDER BY visit_date DESC
            LIMIT 100";
$res = mysql_query($sql);

For each of them, we are going to create a new element with latitude, longitude, and the name of the city/state/country:

while($array = mysql_fetch_array($res)){
 $lat = $array['lat'];
 $lng = $array['lng'];
 $location = $array['city'].', '.$array['state'].', '.$array['country'];

$element = $dom->createElement( 'location' );
$element->setAttribute( 'lat', $lat );
$element->setAttribute( 'lng', $lng );
$text = $dom->createCDATASection( $location );
$element->appendChild( $text );
$rootElement->appendChild($element);
}

And finally, return the XML:


return $dom->saveXML();

And, that’s it!

It should return XML that looks something like this:


<locations>
<location lat="40.960701" lng="-111.928001">This is you!</location>
</locations>

We'll then use this XML to create our points on the map.

Part V – Setting up the map >>


 
Copyright © 2002-2005 Uresk Networks