Howto setup a Nepenthes powerd google Map

This doc is on TODO list

Requirements

  • running and working xmlrpc server with mysql database
  • php
  • google map license

Set it up

get the google map key

hit the google docs

good, detailed docs howto use the api. these ppl want you to use their product.

http://www.google.com/apis/maps/documentation/

xmlrpc server with mysql database

get the data from the database on the map

the data.xml.php script

the script will cache the query results for 15 minutes.

<?php
 
header('Content-Type: text/xml');
 
$query = false;
 
if ( ($st = @stat("data.cache.xml")) != false )
{
 
        if ( $st['mtime'] < ( time(0) - 900 ) )
        {
                $query = true;
        }
}else
{
        $query = true;
}
 
 
if ( $query == true )
{
        $f = fopen("data.cache.xml","w+");  // change this path
 
 
        mysql_connect('<SERVER>', '<USER>', '<PASS>'); // make sure to changes this
        mysql_select_db('<DB>');                       // and this
 
        $mytime = time(0) - 24 * 3600 * 9;
 
 
        $query = "SELECT DISTINCT AttackerCity, AttackerCountry, AttackerLat, AttackerLng, COUNT(*) AS Attackers FROM Hit WHERE AttackerLat <> 0.0 AND AttackerLng <> 0.0 GROUP BY AttackerLat,AttackerLng";
 
        $r_hit = mysql_query($query);
        print mysql_error();
        if( mysql_num_rows($r_hit) )
        {
//              print("<markers>\n");
                fwrite($f,"<markers>\n");
                while( $hit = mysql_fetch_assoc($r_hit) )
                {
//                      print('  <marker lat="'.$hit['AttackerLat'].'" lng="'.$hit['AttackerLng'].'" count="'.$hit['Attackers'].'" country="'.$hit['AttackerCountry'].'" city="'.$hit['AttackerCity'].'" />'."\n");
                        $line ='   <marker lat="'.$hit['AttackerLat'].'" lng="'.$hit['AttackerLng'].'" count="'.$hit['Attackers'].'" country="'.$hit['AttackerCountry'].'" city="'.$hit['AttackerCity'].'" />'."\n";
                        fwrite($f,$line);
                }
//              print("</markers>\n");
                fwrite($f,"</markers>\n");
        }
        fclose($f);
}
 
$f = fopen("data.cache.xml","r");
$contents = '';
while (!feof($f)) {
  echo fread($f, 8192);
}
fclose($f);
?>

the ajax google java script

the script will open data.xml.php and use the google maps api to render the page. make sure to insert your google maps key

 
<script src="http://maps.google.com/maps?file=api&v=1&key=___YOUR_GOOGLE_MAPS_API_KEY___" type="text/javascript">
</script>
</head>
<body>
<div id="map" style="width: 800px; height: 400px">
</div>
<script type="text/javascript">
//<![CDATA[
 
var map = new GMap(document.getElementById("map"));
//    map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
map.addControl(new GLargeMapControl());
map.centerAndZoom(new GPoint(0.0, 18.0), 15);
//]]>
 
 
var yellowicon = new GIcon();
yellowicon.image = "http://labs.google.com/ridefinder/images/mm_20_yellow.png";
yellowicon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
yellowicon.iconSize = new GSize(12, 20);
yellowicon.shadowSize = new GSize(22, 20);
yellowicon.iconAnchor = new GPoint(6, 20);
yellowicon.infoWindowAnchor = new GPoint(5, 1);
 
 
var orangeicon = new GIcon();
orangeicon.image = "http://labs.google.com/ridefinder/images/mm_20_orange.png";
orangeicon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
orangeicon.iconSize = new GSize(12, 20);
orangeicon.shadowSize = new GSize(22, 20);
orangeicon.iconAnchor = new GPoint(6, 20);
orangeicon.infoWindowAnchor = new GPoint(5, 1);
 
 
 
var redicon = new GIcon();
redicon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
redicon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
redicon.iconSize = new GSize(12, 20);
redicon.shadowSize = new GSize(22, 20);
redicon.iconAnchor = new GPoint(6, 20);
redicon.infoWindowAnchor = new GPoint(5, 1);
 
var blackicon = new GIcon();
blackicon.image = "http://labs.google.com/ridefinder/images/mm_20_black.png";
blackicon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
blackicon.iconSize = new GSize(12, 20);
blackicon.shadowSize = new GSize(22, 20);
blackicon.iconAnchor = new GPoint(6, 20);
blackicon.infoWindowAnchor = new GPoint(5, 1);
 
 
function createMarker(point,country,city,count)
{
 
    var marker;
    if ( count <= 5){
     marker = new GMarker(point,yellowicon);
    }else
    if ( count <= 25){
     marker = new GMarker(point,orangeicon);
    }else
    if ( count <= 250){
     marker = new GMarker(point,redicon);
    }else
    {
     marker = new GMarker(point,blackicon);
    }
 
    var msg = "<small><b>Country:</b> " + country +"<br/>";
    msg = msg+"<b>City:</b> " + city +"<br/>";
    msg = msg+"<b>Count:</b> " + count +"<br/>";
    msg = msg+"</small>";
    GEvent.addListener(marker, "click", function(){marker.openInfoWindowHtml(msg);});
    return marker;
}
 
 
var request = GXmlHttp.create();
request.open("GET", "data.xml.php", true);
request.onreadystatechange = function() {
    if (request.readyState == 4) {
        var xmlDoc = request.responseXML;
        var markers = xmlDoc.documentElement.getElementsByTagName("marker");
 
        for (var i = 0; i < markers.length; i++) {
            var point = new GPoint(parseFloat(markers[i].getAttribute("lng")),
                                   parseFloat(markers[i].getAttribute("lat")));
            var city = markers[i].getAttribute("city");
            var country = markers[i].getAttribute("country");
            var count = markers[i].getAttribute("count");
            var marker = new createMarker(point,country,city,count);
            map.addOverlay(marker);
        }
    }
}
request.send(null);
</script>
 

Bugs

data.xml.php works unreliable sometimes ...

 
howto/setup_nepenthes_googlemap.txt · Last modified: 2006/02/17 14:01
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki