x This domain is for sale. If you intrested, Please contact : webspeckle@gmail.com

How to use marker clusters on a Google map?

This tutorial shows you how to use marker clusters to display a large number of markers on a map. You can use the MarkerClusterer library in combination with the Google Maps JavaScript API to combine markers of close proximity into clusters, and simplify the display of markers on the map.

The MarkerClusterer library uses the grid-based clustering technique that divides the map into squares of a certain size (the size changes at each zoom level), and groups the markers into each square grid. It creates a cluster at a particular marker, and adds markers that are in its bounds to the cluster. It repeats this process until all markers are allocated to the closest grid-based marker clusters based on the map's zoom level. If markers are in the bounds of more than one existing cluster, the Maps JavaScript API determines the marker's distance from each cluster, and adds it to the closest cluster.

You can set a MarkerClusterer option to adjust the cluster position to reflect the average distance between all the markers that are contained within it. You can also customize the MarkerClusterer to modify other parameters like the grid size, the cluster icon, cluster text, among others.



As a simple illustration, this tutorial adds a set of markers to the map using the locations array. You can use other sources to get markers for your map.

Adding a marker clusterer
Follow the steps below to add a marker clusterer:

1) Get the marker clustering library and images from GitHub, and store them on a server accessible to your app.
The JavaScript library and image files for the MarkerClusterer are available in the Google Maps repo on GitHub. Download or copy the following files from GitHub to a location accessible to your app:
markerclusterer.js
m1.png
m2.png
m3.png
m4.png
m5.png

2) Add the marker clustering library to your page.
In the code for this tutorial, the script below specifies the location of the markerclusterer.js library file on https://developers.google.com.

<script src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js">
Change the path to specify the location where you have saved the same file.

3) Add a marker clusterer in your app.
The code below adds a marker clusterer to the map.

var markerCluster = new MarkerClusterer(map, markers,
            {imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
      }
This sample passes the markers array to the MarkerClusterer. It also specifies the location of all five image files in the imagePath parameter. Replace this with the path to the location where you have saved the same image files.

Demo

No comments:

Post a Comment