Linux, WebDev and other…

Google Maps – handling huge amount of markers


We have a huge amount of markers and we want to display it to user using google maps.


  • use ajax requests to fetch markers only for area visible by user
  • display message “please zoom in”, if we have too many markers (let’s say that limit is 500)
  • use MarkerClustererPlus to display fetched markers
  • do not fire ajax requests if user is browsing  subarea from last fetched area

Preparing test database

For testing purposes, I downloaded Free World Cities Databases. It’s a CSV file with 3 173 959 rows.

Then, I created database with one table:

Next, I imported test data to database:

Area query

This answer on stack overflow was very helpful, but it was much slower than simple query in same SO thread. Check out this screenshot:


So i decided to build proper query with PHP like this:

That gave me correct results and fast responses.

PHP Backend

This is full code placed in single file for this demo purpose:

The input is south west and north east bounds of map in JSON format.

The output is also in JSON format. It contains needZom (boolean flag set to true if there are too many results) and markers array (results from database).

HTML + JavaScript Frontend

 Live demo

, , ,

One thought on “Google Maps – handling huge amount of markers

Leave a Reply

Your email address will not be published. Required fields are marked *