Ik probeer de extensie "OverlappingMarkerSpiderfier" te integreren in mijn google maps code. Ik zit namelijk met het probleem dat op mijn kaart een aantal markers precies dezelfde coordinaten hebben. Daarbij gebruik ik de markerclusterer van Google om dichtbij elkaar staande markers te grouperen. Wanneer twee markers dus op elkaar staan zal die gegroupeerde marker nooit splitten in de twee markers die eronder zitten, hoe ver je ook inzoomt.
Vandaar dat ik spiderfier wil gebruiken, dit splitst alle op elkaar staande markers en trekt ze uit elkaar wanneer je erop klikt.
Ik heb een poging gewaagd dit te integreren in mijn google maps code maar ik kom er niet uit.
Mijn oude zonder de integratie:
(function($) {
"use strict";
var map;
var bedrijvenlijst = []; // nieuwe array
$.each( bedrijven, function( key, value ) {
// console.log( key + ": " + value.plaats );
bedrijvenlijst.push({
title : value.title,
alias : value.alias,
image : value.image,
address : value.straat + ' ' + value.plaats,
position : {
lat : parseFloat( value.lat ),
lng : parseFloat( value.lng )
},
markerIcon : 'marker-green.png'
});
});
var props = bedrijvenlijst; // Voeg de array toe
// console.log('niet mobiel');
setTimeout(function() {
$('body').removeClass('notransition');
if ($('#home-map').length > 0) {
map = new google.maps.Map(document.getElementById('home-map'), options);
var styledMapType = new google.maps.StyledMapType(styles, {
name : 'Styled'
});
map.mapTypes.set('Styled', styledMapType);
map.setCenter(new google.maps.LatLng(51.8410662,4.3000354));
map.setZoom(13);
var markers = props.map(function(prop, i) {
var marker = new google.maps.Marker({
position: prop.position,
icon: new google.maps.MarkerImage(
'images/' + prop.markerIcon,
new google.maps.Size(36, 36),
new google.maps.Point(0, 0),
new google.maps.Point(18, 36),
new google.maps.Size(36, 36)
),
});
var infoboxContent = '<div class="infoW">' +
'<div class="propImg">' +
'<img style="object-fit:contain;" src="images/' + prop.image + '">' +
'<div class="propBg">'+
// '<div class="propPrice">' + prop.price + '</div>'+
'</div>' +
'</div>' +
'<div class="paWrapper">' +
'<div class="propTitle">' + prop.title + '</div>' +
'<div class="propAddress">' + prop.address + '</div>' +
'</div>' +
'<div class="clearfix"></div>' +
'<div class="infoButtons">' +
'<a class="btn btn-sm btn-round btn-gray btn-o closeInfo">Sluit</a>' +
'<a href="'+ prop.alias +'.html" class="btn btn-sm btn-round btn-green viewInfo">Bekijk</a>' +
'</div>' +
'</div>';
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infobox.setContent(infoboxContent);
infobox.open(map, marker);
// map.setCenter(new google.maps.LatLng(marker.position.lat,marker.position.long));
// map.event.trigger(map, 'resize');
}
})(marker, i));
$(document).on('click', '.closeInfo', function() {
infobox.open(null,null);
});
return marker;
});
var markerCluster = new MarkerClusterer(
map,
markers,
{
imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'
}
);
var addMarkers = function(props, map) {
}
addMarkers(props, map);
}
}, 300);
var options = {
zoom : 13,
mapTypeId : 'Styled',
disableDefaultUI: false,
mapTypeControlOptions : {
mapTypeIds : [ 'Styled' ]
},
zoomControl: true,
zoomControlOptions: {
position: google.maps.ControlPosition.RIGHT_CENTER
},
streetViewControl: true,
streetViewControlOptions: {
position: google.maps.ControlPosition.RIGHT_CENTER
},
scrollwheel: false
};
var styles = [{
stylers : [ {
hue : "#cccccc"
}, {
saturation : -100
}]
}, {
featureType : "road",
elementType : "geometry",
stylers : [ {
lightness : 100
}, {
visibility : "simplified"
}]
}, {
featureType : "road",
elementType : "labels",
stylers : [ {
visibility : "on"
}]
}, {
featureType: "poi",
stylers: [ {
visibility: "off"
}]
}];
var infobox = new InfoBox({
disableAutoPan: false,
maxWidth: 202,
pixelOffset: new google.maps.Size(-101, -285),
zIndex: null,
boxStyle: {
background: "url('images/infobox-bg.png') no-repeat",
opacity: 1,
width: "202px",
height: "245px"
},
closeBoxMargin: "28px 26px 0px 0px",
closeBoxURL: "",
infoBoxClearance: new google.maps.Size(1, 1),
pane: "floatPane",
enableEventPropagation: false
});
})(jQuery);
En mijn poging tot integratie:
(function($) {
"use strict";
var map;
var bedrijvenlijst = []; // nieuwe array
$.each( bedrijven, function( key, value ) {
// console.log( key + ": " + value.plaats );
bedrijvenlijst.push({
title : value.title,
alias : value.alias,
image : value.image,
address : value.straat + ' ' + value.plaats,
position : {
lat : parseFloat( value.lat ),
lng : parseFloat( value.lng )
},
markerIcon : 'marker-green.png'
});
});
var props = bedrijvenlijst; // Voeg de array toe
setTimeout(function() {
$('body').removeClass('notransition');
if ($('#home-map').length > 0) {
map = new google.maps.Map(document.getElementById('home-map'), options);
var styledMapType = new google.maps.StyledMapType(styles, {
name : 'Styled'
});
map.mapTypes.set('Styled', styledMapType);
map.setCenter(new google.maps.LatLng(51.8410662,4.3000354));
map.setZoom(13);
var oms = new OverlappingMarkerSpiderfier(map, {
markersWontMove: true,
markersWontHide: true,
basicFormatEvents: true
});
var markers = props.map(function(prop, i) {
var marker = new google.maps.Marker({
position: prop.position,
icon: new google.maps.MarkerImage(
'images/' + prop.markerIcon,
new google.maps.Size(36, 36),
new google.maps.Point(0, 0),
new google.maps.Point(18, 36),
new google.maps.Size(36, 36)
),
});
var infoboxContent = '<div class="infoW">' +
'<div class="propImg">' +
'<img style="object-fit:contain;" src="images/' + prop.image + '">' +
'<div class="propBg">'+
// '<div class="propPrice">' + prop.price + '</div>'+
'</div>' +
'</div>' +
'<div class="paWrapper">' +
'<div class="propTitle">' + prop.title + '</div>' +
'<div class="propAddress">' + prop.address + '</div>' +
'</div>' +
'<div class="clearfix"></div>' +
'<div class="infoButtons">' +
'<a class="btn btn-sm btn-round btn-gray btn-o closeInfo">Sluit</a>' +
'<a href="'+ prop.alias +'.html" class="btn btn-sm btn-round btn-green viewInfo">Bekijk</a>' +
'</div>' +
'</div>';
google.maps.event.addListener(marker, 'spider_click', (function(marker, i) {
return function() {
infobox.setContent(infoboxContent);
infobox.open(map, marker);
// map.setCenter(new google.maps.LatLng(marker.position.lat,marker.position.long));
// map.event.trigger(map, 'resize');
}
})(marker, i));
$(document).on('click', '.closeInfo', function() {
infobox.open(null,null);
});
return marker;
});
var markerCluster = new MarkerClusterer(
map,
markers,
{
imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m',
maxZoom: 15
}
);
var addMarkers = function(props, map) {
}
oms.addMarkers(props, map);
}
}, 300);
var options = {
zoom : 13,
mapTypeId : 'Styled',
disableDefaultUI: false,
mapTypeControlOptions : {
mapTypeIds : [ 'Styled' ]
},
zoomControl: true,
zoomControlOptions: {
position: google.maps.ControlPosition.RIGHT_CENTER
},
streetViewControl: true,
streetViewControlOptions: {
position: google.maps.ControlPosition.RIGHT_CENTER
},
scrollwheel: false
};
var styles = [{
stylers : [ {
hue : "#cccccc"
}, {
saturation : -100
}]
}, {
featureType : "road",
elementType : "geometry",
stylers : [ {
lightness : 100
}, {
visibility : "simplified"
}]
}, {
featureType : "road",
elementType : "labels",
stylers : [ {
visibility : "on"
}]
}, {
featureType: "poi",
stylers: [ {
visibility: "off"
}]
}];
var infobox = new InfoBox({
disableAutoPan: false,
maxWidth: 202,
pixelOffset: new google.maps.Size(-101, -285),
zIndex: null,
boxStyle: {
background: "url('images/infobox-bg.png') no-repeat",
opacity: 1,
width: "202px",
height: "245px"
},
closeBoxMargin: "28px 26px 0px 0px",
closeBoxURL: "",
infoBoxClearance: new google.maps.Size(1, 1),
pane: "floatPane",
enableEventPropagation: false
});
})(jQuery);