views:

238

answers:

3

hi i am creating a mashup in django and google maps and i am wondering if there is a way of clustering markers on the server side using django/python.

A: 

There is a great tutorial here with a server-side clustering function. It's written in PHP, but you could easily port it to Python.

Chris B
A: 

I came up with the code below to figure out if one marker is close enough to another for clustering - close if two cluster icons start overlapping. Works for the whole world map for all zoom levels.

The problem is that map projection is non-linear and you can't just set some delta_lang delta_lat tolerance - both will depend on the lattitude. For local maps it is not a problem though.

If you want to do all on the server side you will have to calculate clustered markers for each zoomlelvel either per AJAX call or print them all at once.

function isCloseTo($other,$z){//$z is zoomlevel
    $delta_lat = abs($this->lattitude - $other->lattitude);
    $delta_lng = abs($this->longitude - $other->longitude);

    $l = abs($this->lattitude);
    $l2 = $l*$l;
    $l3 = $l2*$l;
    $l4 = $l3*$l;

    $factor =   1
            +0.0000312*$l
            +0.0003604*$l2
            -0.000009858*$l3
            +0.0000001506*$l4;

    $tol_lat = (45.42*exp(-0.6894339*$z)/3)/$factor;
    $tol_lng = 21.845*exp(-0.67686*$z)/2;
    if ($delta_lat < $tol_lat and $delta_lng < $tol_lng){
        return true;
    }
    else{
        return false;
    }
}
Evgeny
doesn't look very pythonic...
lhahne
that's php :) but hopefully you can figure out the translation :)
Evgeny
+1  A: 

I have implemented server side clustering in Django on my real estate/rentals site; I explain it here.

Tristen