title: php - Server side marker clustering
body: <p>I currently have a map on my website that is displaying 50,000 markers. These were un readable to the user due to cluttering so I implemented a clustering solution using google's markerclusterer. This works ok but due to the amount of markers, it is incrediably slow to load.</p>
  
  <p>I would like to use a server side solution in the form of php but can not find out the best way to do this. This is my current setup</p>
  
  <p>External php file markers-xml.php</p>
  
  <pre><code>&lt;?php
  $SQL = "SELECT MarkerName, Lat, Lng FROM TableName WHERE MarkerName !=''";
  $Query = mysql_query($SQL);
  $NumRows = mysql_num_rows($Query);
  ?&gt;
  &lt;markers&gt;
  &lt;?php       
  for($i = 0; $i &lt; $NumRows; $i++)
  {
      $row = mysql_fetch_assoc($Query);
      $Lat = $row['Lat'];
      $Lng = $row['Lng'];
      $MarkerName = $row['MarkerName'];
  
      echo "&lt;marker Lat='$Lat' Lng='$Lng' MarkerName='$MarkerName'&gt; &lt;/marker&gt;\n";
  }
  ?&gt;
  &lt;/markers&gt;
  </code></pre>
  
  <p>Main file conataining map</p>
  
  <pre><code>function initialize(mapvars) {
          var xmldata = "markers-xml.php";
  
          downloadUrl(xmldata, function(doc) {
              var xml = xmlParse(doc);
              var markersInfo = xml.documentElement.getElementsByTagName("marker");
              var markers = []; 
              var bounds = new google.maps.LatLngBounds();
              for (var i = 0; i &lt; markersInfo.length; i++) {
                  var Lat = parseFloat(markersInfo[i].getAttribute("Lat"));
                  var Lng = parseFloat(markersInfo[i].getAttribute("Lng"));
                  var point = new google.maps.LatLng(Lat,Lng);
                  var MarkerName = markersInfo[i].getAttribute("MarkerName");  
  
                  var marker = new google.maps.Marker({
                      position: point,
                      map: map,
                      title: MarkerName,
                      MarkerName: MarkerName
                  });
  
              markers.push(marker);
              bounds.extend(point);
          }
  
          var markerCluster = new MarkerClusterer(map, markers);
      }
  </code></pre>