{"id":3073,"date":"2015-01-05T15:51:21","date_gmt":"2015-01-05T14:51:21","guid":{"rendered":"https:\/\/www.devco.net\/?p=3073"},"modified":"2015-01-05T15:54:28","modified_gmt":"2015-01-05T14:54:28","slug":"marker-clustering-using-gmaps-js","status":"publish","type":"post","link":"https:\/\/www.devco.net\/archives\/2015\/01\/05\/marker-clustering-using-gmaps-js.php","title":{"rendered":"Marker clustering using GMaps.js"},"content":{"rendered":"

In a previous post<\/a> I showed that I am using a KML file as input into GMaps.js<\/a> to put a bunch of points on a map for my travels<\/a> site. This worked great, but I really want to do some marker clustering since too many points is pretty bad looking as can be seen below.<\/p>\n

<\/center><\/p>\n

I’d much rather do some clustering and expand out to multiple points when you zoom in like here:<\/p>\n

<\/center><\/p>\n

Turns out there are a few libraries for this already, I started out with one called Marker Clusterer<\/a> but ended up with a improved version of this called Marker Clusterer Plus<\/a>. And GMaps.js supports cluster libraries natively so should be easy, right?<\/p>\n

Turns out the way Google maps loads KML files is done using a layer over the map and the points just are not accessible to any libraries, so the cluster libraries does nothing. Ok, so back to drawing points using my code.<\/p>\n

I added a endpoint to the app that emits my points as JSON:<\/p>\n

\r\n[\r\n  {\"type\":\"visit\",\r\n   \"country\":\"Finland\",\r\n   \"title\":\"Helsinki\",\r\n   \"lat\":60.1333,\r\n   \"popup_html\":\"

\\nHelsinki<\/font>\\n


\\nBusiness trip in 2005

\\n\\n<\/p>\\n\",\r\n \"comment\":\"Business trip in 2005\",\r\n \"lon\":25,\r\n \"icon\":\"\/markers\/marker-RED-REGULAR.png\"}\r\n]\r\n<\/pre>\n

Now adding all the points and getting them clustered is pretty easy:<\/p>\n

\r\n