Colour Coded Mapping Widget

I have recently received a number of requests for the PHP code of the mapping widget that I wrote for my websites. Note however that this is the colour coded version used on most of my websites, but not the older monochrome version used on this website as it uses code written by another author. 

For this widget to work, the website needs to have a MySQL database, a 1440x720 resolution image file of a world map, and a copy of the geoplugin.class.php file. It also requires that the user have permission to run PHP code on the webpage (which unfortunately Synthasite hosted sites do not allow). The MySQL database in this example is on the server host, username is username, and password is password, and need to be changed to your own account. The database should also contain a table called private_counter with columns called (`index`, `longitude`, `latitude`, `x`, `y`, `count`,`last_visit`). In the same directory as the geoplugin.class.php file, create a new file with the *.php extension, and paste in the following code:

<?

require_once('./geoplugin.class.php');
$geoplugin = new geoPlugin();
$geoplugin->locate();

function Long2Map($long){

        $picx = $long*(1440/360) + 720;
        return $picx;
}

function Lat2Map($lat){

        $picy = (90-$lat)*(720/180);
        return $picy;
}

function HSVtoRGB(array $hsv) {

    list($H,$S,$V) = $hsv;
    //1
    $H *= 6;
    //2
    $I = floor($H);
    $F = $H - $I;
    //3
    $M = $V * (1 - $S);
    $N = $V * (1 - $S * $F);
    $K = $V * (1 - $S * (1 - $F));
    //4
    switch ($I) {
        case 0:
            list($R,$G,$B) = array($V,$K,$M);
            break;
        case 1:
            list($R,$G,$B) = array($N,$V,$M);
            break;
        case 2:
            list($R,$G,$B) = array($M,$V,$K);
            break;
        case 3:
            list($R,$G,$B) = array($M,$N,$V);
            break;
        case 4:
            list($R,$G,$B) = array($K,$M,$V);
            break;
        case 5:
        case 6: //for when $H=1 is given
            list($R,$G,$B) = array($V,$M,$N);
            break;
    }

    return array($R*255, $G*255, $B*255);
}

// create image
$image = imagecreatetruecolor(600, 400);
$image = ImageCreateFromJPEG('map.jpg');

$database = mysql_connect('server','username','password');

if (!$database) {

    die('Could not connect: ' . mysql_error());
}

mysql_select_db("username") or die(mysql_error());

$level = 15;

$sql = "SELECT * FROM `private_counter`";
while($level>0){
$data = mysql_query($sql,$database);

if (!$data)

    {
   die('Error: ' . mysql_error());
  }

while($info = mysql_fetch_array($data)){

  $curx = $info['x'];
  $cury = $info['y'];
  $size = 4*log($info['count']);
 $date = $info['last_visit'];
  $delay = time()-strtotime($date);
$delayd = $delay/(3600*24);

$RGBCol = HSVtoRGB(array(min($delayd/450,0.98),1,max(1-$delayd/1900,0)));

$current_color = imagecolorallocate($image,$RGBCol[0],$RGBCol[1],$RGBCol[2]);

imagefilledellipse($image,$curx,$cury,$size+1,$size+1,$black);

  imagefilledellipse($image,$curx,$cury,$size,$size,$current_color);
}

$level = $level - 1;

$sql = "SELECT * FROM `private_counter` WHERE (NOW() - last_visit)<(100000000*$level) AND (NOW() - last_visit)>(100000000*($level-1))";
}

imagejpeg($image,'./images/Map.jpg');

$cury = floor(Lat2Map($geoplugin->latitude));

$curx = floor(Long2Map($geoplugin->longitude));
imagefilledellipse($image,floor($curx),floor($cury),12,12,$white);
$curi = 1000*floor($curx) + floor($cury);

$data = mysql_query("INSERT INTO `private_counter`(`index`, `longitude`, `latitude`, `x`, `y`, `count`,`last_visit`) VALUES ($curi,$geoplugin->longitude,$geoplugin->latitude,$curx,$cury,1,NOW()) ON DUPLICATE KEY UPDATE count = count +1,`last_visit`=NOW();",$database);

if (!$data)

   {
   die('Error: ' . mysql_error());
  }
// flush image

header('Content-type: image/jpg');
imagejpeg($image);
imagedestroy($image);

mysql_close($database);

?>        

Then access the file as an ordinary image file in your HTML code, and it will generate a map that records and displays the location of visitors. It is currently setup to colour the hits based on the age of the last visit (red signifies a recent visit, down to purple for the oldest visits). It does have a minor bug in that some servers record the difference between December 31 and January 1 as one full year, which I haven't had time to debug yet. As always, users are welcome to modify my code to build their own widgets, but credit for the original PHP code is alway appreciated.


Make a Free Website with Yola.