Marker Clustering on Google Maps with JSON multi markers
Marker clustering is a technique used in Google Maps to group multiple markers into a single cluster when they are close to each other. This helps in improving the performance of the map when dealing with a large number of markers.
To implement marker clustering with JSON multi markers on Google Maps, we can follow the steps below:
- Include Google Maps JavaScript API in your HTML file. You can do this by adding the following script tag in the head section of your HTML file:
- Create a div element in your HTML file to hold the map. You can give it an id for easier access in JavaScript:
- In your JavaScript code, initialize the map by accessing the div element and creating a new instance of the google.maps.Map class:
- Load the JSON data containing the markers onto the map using AJAX. You can use the XMLHttpRequest or fetch API to retrieve the JSON data from a server:
- Create a function to add markers to the map. Inside this function, you can loop through the markersData array and create a new google.maps.Marker object for each marker:
- Implement marker clustering by including the MarkerClusterer library provided by Google. You can add the library script tag in your HTML file:
- In the addMarkers function, create a new instance of the MarkerClusterer class and pass in the map object and the array of markers:
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY" async defer></script>
<div id="map"></div>
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 37.7749, lng: -122.4194},
zoom: 10
});
var request = new XMLHttpRequest();
request.open('GET', 'markers.json', true);
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
var markersData = JSON.parse(request.responseText);
// Call a function to add markers to the map
addMarkers(markersData);
} else {
console.error('Error loading markers JSON');
}
};
request.send();
function addMarkers(markersData) {
var markers = [];
for (var i = 0; i < markersData.length; i++) {
var marker = new google.maps.Marker({
position: {lat: markersData[i].lat, lng: markersData[i].lng},
map: map,
title: markersData[i].title
});
markers.push(marker);
}
}
<script src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js"></script>
var markerCluster = new MarkerClusterer(map, markers, {
imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'
});
By following the above steps, you should be able to implement marker clustering with JSON multi markers on Google Maps. The markers will be clustered when they are close to each other, improving the performance and readability of the map.
Here is an example of how the above code can be combined in an HTML file:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY" async defer></script>
<script src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js"></script>
</head>
<body>
<div id="map" style="height: 400px; width: 100%;"></div>
<script>
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 37.7749, lng: -122.4194},
zoom: 10
});
var request = new XMLHttpRequest();
request.open('GET', 'markers.json', true);
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
var markersData = JSON.parse(request.responseText);
addMarkers(markersData);
} else {
console.error('Error loading markers JSON');
}
};
request.send();
function addMarkers(markersData) {
var markers = [];
for (var i = 0; i < markersData.length; i++) {
var marker = new google.maps.Marker({
position: {lat: markersData[i].lat, lng: markersData[i].lng},
map: map,
title: markersData[i].title
});
markers.push(marker);
}
var markerCluster = new MarkerClusterer(map, markers, {
imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'
});
}
</script>
</body>
</html>
- Get Latitude And Longitude From Google Maps Onclick In Android
- Find Location By Latitude And Longitude Google Maps Javascript
- Google Maps Plot Multiple Using Clustering In Javascript Example
- Html table javascript add column dynamically?
- How Do I Change Marker Cluster Options & How Do I Customize The Cluster Icon Using Google Maps Api With Markerclusterer