我有一個代碼,用於切換按類別標記的可見性,這很好用(請參閱下面的代碼)。按類別選擇標記並將fitbounds選擇到可見標記
我現在想什麼和不能弄清楚如何:
每個顯示/隱藏一個markergroup後,我想變焦和地圖的中心進行調整,以顯示所有可見的標記。
我已經發現了另一個代碼fitBounds()
,但不要設法得到兩個一起工作(我絕對新Javascript
)。
我發現剛裝修界於可見光標記的例子,但不與顯示/按類別隱藏標記組合。 (如Center/Set Zoom of Map to cover all visible Markers?)
這裏的兩個代碼示例:
顯示/隱藏標記按類別:
function xmlParse(str) {
if (typeof ActiveXObject != 'undefined' && typeof GetObject != 'undefined') {
var doc = new ActiveXObject('Microsoft.XMLDOM');
doc.loadXML(str);
return doc;
}
if (typeof DOMParser != 'undefined') {
return (new DOMParser()).parseFromString(str, 'text/xml');
}
return createElement('div', null);
}
var infoWindow = new google.maps.InfoWindow();
var customIcons = {
monumento: {
icon: 'http://maps.google.com/mapfiles/ms/icons/blue.png'
},
hotel: {
icon: 'http://maps.google.com/mapfiles/ms/icons/green.png'
},
restaurantes: {
icon: 'http://maps.google.com/mapfiles/ms/icons/yellow.png'
},
museus: {
icon: 'http://maps.google.com/mapfiles/ms/icons/purple.png'
}
};
var markerGroups = {
"museus": [],
"monumentos": [],
"restaurantes": [],
"hotel": []
};
function load() {
var map = new google.maps.Map(document.getElementById("mapCanvas"), {
center: new google.maps.LatLng(38.639104, -8.210413),
zoom: 12,
mapTypeId: 'satellite'
});
var infoWindow = new google.maps.InfoWindow();
map.set('styles', [{
zoomControl: false
}, {
featureType: "road.highway",
elementType: "geometry.fill",
stylers: [{
color: "#ffd986"
}]
}, {
featureType: "road.arterial",
elementType: "geometry.fill",
stylers: [{
color: "#9e574f"
}]
}, {
featureType: "road.local",
elementType: "geometry.fill",
stylers: [{
color: "#d0cbc0"
}, {
weight: 1.1
}
]
}, {
featureType: 'road',
elementType: 'labels',
stylers: [{
saturation: -100
}]
}, {
featureType: 'landscape',
elementType: 'geometry',
stylers: [{
hue: '#ffff00'
}, {
gamma: 1.4
}, {
saturation: 82
}, {
lightness: 96
}]
}, {
featureType: 'poi.school',
elementType: 'geometry',
stylers: [{
hue: '#fff700'
}, {
lightness: -15
}, {
saturation: 99
}]
}]);
// downloadUrl("markers.xml", function (data) {
var xml = xmlParse('<markers><marker name="Castelo" address="Rua da Condessa de Valença" lat="38.64351973190569" lng="-8.216521812152905" type="monumento" /><marker name="Anta 1 de Tourais" address="Estrada Nacional 114" lat="38.64260059929888" lng="-8.159376865959189" type="monumento" /><marker name="Hotel da Ameira" address="Herdade da Ameira" lat="38.64109640475479" lng="-8.180432206726096" type="hotel" /><marker name="Hotel Montemor" address="Avenida Gago Coutinho 8, 7050-248 Montemor-o-Novo" lat="38.64925541964039" lng="-8.216489625644726" type="hotel" /><marker name="Restaurante Monte Alentejano" address="Av. Gago Coutinho 8" lat="38.6492329" lng="-8.216665" type="restaurantes" /><marker name="Restaurante A Ribeira" address="Rua de São Domingos" lat="38.6347498199708" lng="-8.206468892765088" type="restaurantes" /><marker name="Núcleo Museológico do Convento de S. Domingos" address="" lat="38.643139" lng="-8.212732" type="museus" /><marker name="Centro Interpretativo do Castelo de Montemor-o-Novo" address="Rua Condessa de Valença" lat="38.64258748216167" lng="-8.21467108793263" type="museus" /></markers>');
// var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var name = markers[i].getAttribute("name");
var address = markers[i].getAttribute("address");
var type = markers[i].getAttribute("type");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var html = "<b>" + name + "</b> <br/>" + address;
// var icon = customIcons[type] || {};
var marker = createMarker(point, name, address, type, map);
bindInfoWindow(marker, map, infoWindow, html);
}
// });
}
function createMarker(point, name, address, type, map) {
var icon = customIcons[type] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
icon: icon.icon,
// shadow: icon.shadow,
type: type
});
if (!markerGroups[type]) markerGroups[type] = [];
markerGroups[type].push(marker);
var html = "<b>" + name + "</b> <br/>" + address;
bindInfoWindow(marker, map, infoWindow, html);
return marker;
}
function toggleGroup(type) {
for (var i = 0; i < markerGroups[type].length; i++) {
var marker = markerGroups[type][i];
if (!marker.getVisible()) {
marker.setVisible(true);
} else {
marker.setVisible(false);
}
}
}
function bindInfoWindow(marker, map, infoWindow, html) {
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(html);
infoWindow.open(map, marker);
});
}
function downloadUrl(url, callback) {
var request = window.ActiveXObject ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
request.onreadystatechange = function() {
if (request.readyState == 4) {
request.onreadystatechange = doNothing;
callback(request, request.status);
}
};
request.open('GET', url, true);
request.send(null);
}
function doNothing() {}
google.maps.event.addDomListener(window, 'load', load);
fitBounds()
可見的標記:
function fitBoundsToVisibleMarkers() {
var bounds = new google.maps.LatLngBounds();
for (var i = 0; i < markers.length; i++) {
if (markers[i].getVisible()) {
bounds.extend(markers[i].getPosition());
}
}
map.fitBounds(bounds);
}
[Center/Set Zoom of Map to cover all marker visible Markers?](http://stackoverflow.com/questions/19304574/center-set-zoom-of-map-to-cover-所有標誌物 - 可見 - 標記)。另外,您可以使用這些示例創建一個片段或垃圾箱,以便我們可以幫助您? –
嗨Mosh Feu, 謝謝你的評論! 這裏是第一個代碼的jsfiddle:http://jsfiddle.net/YEPB7/6/ 這是否有幫助? – gliitschi8