我正在尝试在地图上实现多标记集群,但是我对这种语言一无所知。 这是我的代码,可以正常工作:
/* Google Maps
---------------------------------------------------*/
var map;
var InforObj = [];
var centerCords = {
lat: 51.14431,
lng: 4.34605
};
var markersOnMap = [
{
placeName: "<p>content</p>",
LatLng: [{
lat: 51.21851,
lng: 4.40678
}]
},
{
placeName: "<p>content</p>",
LatLng: [{
lat: 51.20843,
lng: 3.22377
}]
},
{
placeName: "<p>content</p>",
LatLng: [{
lat: 50.92935,
lng: 5.33815
}]
},
{
placeName: "<p>content</p>",
LatLng: [{
lat: 51.08416,
lng: 3.44807
}]
},
{
placeName: "<p>content</p>",
LatLng: [{
lat: 50.87792,
lng: 4.69971
}]
},
{
placeName: "<p>content</p>",
LatLng: [{
lat: 51.20585,
lng: 4.42898
}]
}
];
window.onload = function () {
initMap();
};
function addMarkerInfo() {
for (var i = 0; i < markersOnMap.length; i++) {
var contentString = '<div id="content" style="text-align:left;padding:20px;font-size:16px;font-family:Poppins;">' + markersOnMap[i].placeName + '</div>';
const marker = new google.maps.Marker({
icon: 'https://www.website.be/assets/img/map-marker.png',
position: markersOnMap[i].LatLng[0],
map: map
});
const infowindow = new google.maps.InfoWindow({
content: contentString,
width: 400,
height: 400
});
marker.addListener('click', function () {
closeOtherInfo();
infowindow.open(marker.get('map'), marker);
InforObj[0] = infowindow;
});
}
}
function closeOtherInfo() {
if (InforObj.length > 0) {
InforObj[0].set("marker", null);
InforObj[0].close();
InforObj.length = 0;
}
}
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 9,
center: centerCords,
styles: stylesArray,
mapTypeId: 'terrain',
disableDefaultUI: true,
zoomControl: true,
zoomControlOptions: {
position: google.maps.ControlPosition.LEFT_CENTER
},
});
addMarkerInfo();
}
var stylesArray = [
{
"elementType": "geometry",
"stylers": [
{
"color": "#f5f5f5"
}
]
},
{
"elementType": "labels.icon",
"stylers": [
{
"visibility": "off"
}
]
},
{
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#616161"
}
]
},
{
"elementType": "labels.text.stroke",
"stylers": [
{
"color": "#f5f5f5"
}
]
},
{
"featureType": "administrative",
"elementType": "geometry",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "administrative.land_parcel",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#bdbdbd"
}
]
},
{
"featureType": "poi",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "poi",
"elementType": "geometry",
"stylers": [
{
"color": "#eeeeee"
}
]
},
{
"featureType": "poi",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#757575"
}
]
},
{
"featureType": "poi.park",
"elementType": "geometry",
"stylers": [
{
"color": "#e5e5e5"
}
]
},
{
"featureType": "poi.park",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#9e9e9e"
}
]
},
{
"featureType": "road",
"elementType": "geometry",
"stylers": [
{
"color": "#ffffff"
}
]
},
{
"featureType": "road",
"elementType": "labels.icon",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "road.arterial",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#757575"
}
]
},
{
"featureType": "road.highway",
"elementType": "geometry",
"stylers": [
{
"color": "#dadada"
}
]
},
{
"featureType": "road.highway",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#616161"
}
]
},
{
"featureType": "road.local",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#9e9e9e"
}
]
},
{
"featureType": "transit",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "transit.line",
"elementType": "geometry",
"stylers": [
{
"color": "#e5e5e5"
}
]
},
{
"featureType": "transit.station",
"elementType": "geometry",
"stylers": [
{
"color": "#eeeeee"
}
]
},
{
"featureType": "water",
"elementType": "geometry",
"stylers": [
{
"color": "#c9c9c9"
}
]
},
{
"featureType": "water",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#9e9e9e"
}
]
}
];
但是现在我想添加集群代码,而且我不知道该怎么做,因为我不了解JavaScript。
var markerCluster = new MarkerClusterer(map,markers,{imagePath:'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
有人可以帮我把这两个代码组合在一起吗?谢谢。
答案 0 :(得分:0)
您需要将标记添加到数组中并将其传递到标记聚类器构造函数中。 需要在代码中添加以下几行。
var markersArray = new Array();
和
markersArray.push(marker);
和
var markerCluster = new MarkerClusterer(map, markersArray, {imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
完整的代码如下。我在CAPS中添加了带有前缀“ J-”的评论
/* Google Maps
---------------------------------------------------*/
var map;
// J - CREATE AN EMPTY MARKER ARRAY
var markersArray = new Array();
var InforObj = [];
var centerCords = {
lat: 51.14431,
lng: 4.34605
};
var markersOnMap = [
{
placeName: "<p>content</p>",
LatLng: [{
lat: 51.21851,
lng: 4.40678
}]
},
{
placeName: "<p>content</p>",
LatLng: [{
lat: 51.20843,
lng: 3.22377
}]
},
{
placeName: "<p>content</p>",
LatLng: [{
lat: 50.92935,
lng: 5.33815
}]
},
{
placeName: "<p>content</p>",
LatLng: [{
lat: 51.08416,
lng: 3.44807
}]
},
{
placeName: "<p>content</p>",
LatLng: [{
lat: 50.87792,
lng: 4.69971
}]
},
{
placeName: "<p>content</p>",
LatLng: [{
lat: 51.20585,
lng: 4.42898
}]
}
];
window.onload = function () {
initMap();
};
function addMarkerInfo() {
for (var i = 0; i < markersOnMap.length; i++) {
var contentString = '<div id="content" style="text-align:left;padding:20px;font-size:16px;font-family:Poppins;">' + markersOnMap[i].placeName + '</div>';
const marker = new google.maps.Marker({
icon: 'https://www.website.be/assets/img/map-marker.png',
position: markersOnMap[i].LatLng[0],
map: map
});
const infowindow = new google.maps.InfoWindow({
content: contentString,
width: 400,
height: 400
});
marker.addListener('click', function () {
closeOtherInfo();
infowindow.open(marker.get('map'), marker);
InforObj[0] = infowindow;
});
// J - ADD MARKER TO THE ARRAY.
markersArray.push(marker);
}
}
function closeOtherInfo() {
if (InforObj.length > 0) {
InforObj[0].set("marker", null);
InforObj[0].close();
InforObj.length = 0;
}
}
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 9,
center: centerCords,
styles: stylesArray,
mapTypeId: 'terrain',
disableDefaultUI: true,
zoomControl: true,
zoomControlOptions: {
position: google.maps.ControlPosition.LEFT_CENTER
},
});
addMarkerInfo();
// J - CREATE MARKER CLUSTER USING THE MARKERS ARRAY
var markerCluster = new MarkerClusterer(map, markersArray, {imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
}
var stylesArray = [
{
"elementType": "geometry",
"stylers": [
{
"color": "#f5f5f5"
}
]
},
{
"elementType": "labels.icon",
"stylers": [
{
"visibility": "off"
}
]
},
{
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#616161"
}
]
},
{
"elementType": "labels.text.stroke",
"stylers": [
{
"color": "#f5f5f5"
}
]
},
{
"featureType": "administrative",
"elementType": "geometry",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "administrative.land_parcel",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#bdbdbd"
}
]
},
{
"featureType": "poi",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "poi",
"elementType": "geometry",
"stylers": [
{
"color": "#eeeeee"
}
]
},
{
"featureType": "poi",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#757575"
}
]
},
{
"featureType": "poi.park",
"elementType": "geometry",
"stylers": [
{
"color": "#e5e5e5"
}
]
},
{
"featureType": "poi.park",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#9e9e9e"
}
]
},
{
"featureType": "road",
"elementType": "geometry",
"stylers": [
{
"color": "#ffffff"
}
]
},
{
"featureType": "road",
"elementType": "labels.icon",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "road.arterial",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#757575"
}
]
},
{
"featureType": "road.highway",
"elementType": "geometry",
"stylers": [
{
"color": "#dadada"
}
]
},
{
"featureType": "road.highway",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#616161"
}
]
},
{
"featureType": "road.local",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#9e9e9e"
}
]
},
{
"featureType": "transit",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "transit.line",
"elementType": "geometry",
"stylers": [
{
"color": "#e5e5e5"
}
]
},
{
"featureType": "transit.station",
"elementType": "geometry",
"stylers": [
{
"color": "#eeeeee"
}
]
},
{
"featureType": "water",
"elementType": "geometry",
"stylers": [
{
"color": "#c9c9c9"
}
]
},
{
"featureType": "water",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#9e9e9e"
}
]
}
];