Reputation: 8225
I have asp.net web forms page which has repeater to show multiple markers at the page. I want to cluster the markers, I followed some examples found online but no success so far.
This is what I have:
HTML markup:
var markers = [
<asp:Repeater ID="rptMarkers" runat="server">
<ItemTemplate>
{
"title": '<%# Eval("Name") %>',
"lat": '<%# Eval("Latitude") %>',
"lng": '<%# Eval("Longitude") %>',
"description": '<%# Eval("Name") %>'
}
</ItemTemplate>
<SeparatorTemplate>
,
</SeparatorTemplate>
</asp:Repeater>
];
var mapOptions = {
center: new google.maps.LatLng(43.648399, -79.485703),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
var markerCluster = new MarkerClusterer(map, markers, mapOptions );
</script>
<script type="text/javascript">
window.onload = function () {
var mapOptions = {
center: new google.maps.LatLng(43.648399, -79.485703),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var infoWindow = new google.maps.InfoWindow();
var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
for (i = 0; i < markers.length; i++) {
var data = markers[i]
var myLatlng = new google.maps.LatLng(data.lat, data.lng);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: data.title
});
(function (marker, data) {
google.maps.event.addListener(marker, "click", function (e) {
infoWindow.setContent(data.description);
infoWindow.open(map, marker);
});
})(marker, data);
}
}
</script>
<div id="dvMap" style="width: 500px; height: 277px">
</div>
</div>
Then, in the backend file I am getting all the data from database and sending to the repeater which properly renders multiple markers, as you can see at this screenshot as well: http://gyazo.com/049821984b889eefa551df663af0d2eb.png
This is the section I was trying to use to get the clustering working (visible in the code above as well):
var mapOptions = {
center: new google.maps.LatLng(43.648399, -79.485703),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
var markerCluster = new MarkerClusterer(map, markers, mapOptions );
When I view the source of the page this is the content I am getting regarding the markers data:
<div class="divdisplay">
<script type="text/javascript" src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/src/markerclusterer.js"></script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var markers = [
{
"title": 'Product Specialist',
"lat": '43.6877731',
"lng": '-79.2709108',
"description": 'Product Specialist'
}
,
{
"title": 'Finance Manager',
"lat": '43.7345524',
"lng": '-79.2561590',
"description": 'Finance Manager'
}
,
{
"title": 'New Job',
"lat": '43.7598255',
"lng": '-79.2252689',
"description": 'New Job'
}
,
{
"title": 'AD, Algorithmic Trading Development Team ',
"lat": '43.7240344',
"lng": '-79.3006776',
"description": 'AD, Algorithmic Trading Development Team '
}
,
{
"title": 'Financial Analyst - Investment Finance Systems Ana',
"lat": '43.7870613',
"lng": '-79.2761657',
"description": 'Financial Analyst - Investment Finance Systems Ana'
}
,
{
"title": 'New Business Coordinator CSR - Group Benefits',
"lat": '43.7891500',
"lng": '-79.1404511',
"description": 'New Business Coordinator CSR - Group Benefits'
}
,
{
"title": 'TD Job 1',
"lat": '43.7092267',
"lng": '-79.2962398',
"description": 'TD Job 1'
}
,
{
"title": 'Web Designer',
"lat": '43.7841207',
"lng": '-79.2917106',
"description": 'Web Designer'
}
,
{
"title": 'ceo',
"lat": '43.6488293',
"lng": '-79.3965691',
"description": 'ceo'
}
,
{
"title": 'IT Analyst',
"lat": '43.6546566',
"lng": '-79.3805941',
"description": 'IT Analyst'
}
,
{
"title": 'Part-Time MARKET RESEARCH INTERVIEWERS',
"lat": '43.6683729',
"lng": '-79.4392835',
"description": 'Part-Time MARKET RESEARCH INTERVIEWERS'
}
];
var mapOptions = {
center: new google.maps.LatLng(43.648399, -79.485703),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
var mcOptions = {gridSize: 50, maxZoom: 15};
var markerCluster = new MarkerClusterer(map, markers, mcOptions);
</script>
<script type="text/javascript">
window.onload = function () {
var mapOptions = {
center: new google.maps.LatLng(43.648399, -79.485703),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var infoWindow = new google.maps.InfoWindow();
var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
for (i = 0; i < markers.length; i++) {
var data = markers[i]
var myLatlng = new google.maps.LatLng(data.lat, data.lng);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: data.title
});
(function (marker, data) {
google.maps.event.addListener(marker, "click", function (e) {
infoWindow.setContent(data.description);
infoWindow.open(map, marker);
});
})(marker, data);
}
}
</script>
<div id="dvMap" style="width: 500px; height: 277px">
</div>
</div>
Upvotes: 1
Views: 2225
Reputation: 6004
You need to pass an array of google.maps.Marker objects to the marker clusterer - you're passing your array of initial marker data.
You need to create a new MarkerClusterer after all markers have been created, not before. So, remove these lines from the first script block in your example:
var mcOptions = {gridSize: 50, maxZoom: 15};
var markerCluster = new MarkerClusterer(map, markers, mcOptions);
We'll move these to after all the markers have been created. So change your window.onload function to this:
<script type="text/javascript">
window.onload = function () {
var mapOptions = {
center: new google.maps.LatLng(43.648399, -79.485703),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
// declare an array to keep all google.maps.Marker instances in:
var googleMarkers = [];
var infoWindow = new google.maps.InfoWindow();
var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
for (i = 0; i < markers.length; i++) {
var data = markers[i]
var myLatlng = new google.maps.LatLng(data.lat, data.lng);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: data.title
});
// add the new marker to the googleMarkers array
googleMarkers.push(marker);
(function (marker, data) {
google.maps.event.addListener(marker, "click", function (e) {
infoWindow.setContent(data.description);
infoWindow.open(map, marker);
});
})(marker, data);
}
// now all the markers have been created, make a new MarkerClusterer:
var mcOptions = {gridSize: 50, maxZoom: 15};
var markerCluster = new MarkerClusterer(map, googleMarkers, mcOptions);
}
</script>
Upvotes: 2