我正在構建自定義的Google地圖(http://www.southdevonaonb.org.uk/cordialemapping/),並遇到切換圖層打開和關閉的問題。打開和關閉Google地圖圖層
我使用的HTML一個簡單的複選框 - 例如:
Parish boundary line <input type="checkbox" id="layer100" onclick="toggleLayer(100)" checked><br />
Letterbox locations and results <input type="checkbox" id="layer0" onclick="toggleLayer(0)" checked><br />
Landscape challenges <input type="checkbox" id="layer1" onclick="toggleLayer(1)" checked><br />
和下面的JavaScript:
var geocoder;
var map;
var marker;
var layers = [];
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng (50.31697, -3.670807);
var myOptions = {
zoom: 10,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map-container"),
myOptions);
marker = new google.maps.Marker({map:map});
layers[100] = new google.maps.KmlLayer('http://www.southdevonaonb.org.uk/cordialemapping/kmzdata/100.kml', {preserveViewport: true});
layers[200] = new google.maps.KmlLayer('http://www.southdevonaonb.org.uk/cordialemapping/kmzdata/200.kml', {preserveViewport: true});
layers[300] = new google.maps.KmlLayer('http://www.southdevonaonb.org.uk/cordialemapping/kmzdata/boundaryline.kml', {preserveViewport: true});
layers[0] = new google.maps.KmlLayer('http://www.southdevonaonb.org.uk/cordialemapping/kmzdata/beta0.kml', {preserveViewport: true});
layers[1] = new google.maps.KmlLayer('http://www.southdevonaonb.org.uk/cordialemapping/kmzdata/beta1.kml', {preserveViewport: true});
layers[2] = new google.maps.KmlLayer('http://www.southdevonaonb.org.uk/cordialemapping/kmzdata/beta2.kml', {preserveViewport: true});
layers[10] = new google.maps.KmlLayer('http://www.southdevonaonb.org.uk/cordialemapping/kmzdata/beta10.kml', {preserveViewport: true});
layers[11] = new google.maps.KmlLayer('http://www.southdevonaonb.org.uk/cordialemapping/kmzdata/beta11.kml', {preserveViewport: true});
layers[46] = new google.maps.KmlLayer('http://www.southdevonaonb.org.uk/cordialemapping/kmzdata/holbeton.kml', {preserveViewport: true});
for (var i = 1; i < layers.length; i++) {
layers[i].setMap(map);
}
}
function codeAddress() {
var address = document.getElementById ("address").value;
geocoder.geocode ({ 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results [1].geometry.location);
marker.setPosition(results [1].geometry.location);
map.setZoom(14);
}
else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
function toggleLayer(i) {
if(layers[i].getMap() === null) {
layers[i].setMap(map);
}
else {
layers[i].setMap(null);
}
}
google.maps.event.addDomListener(window, 'load', initialize);
你會從我還沒有被添加到所有的腳本見上面鏈接的頁面列表中的圖層,以及我使用jQuery Accordian Plugin爲選項卡設置了菜單。
我到目前爲止添加的圖層可以打開和關閉,但是我遇到的問題是,當頁面加載時,有時會顯示圖層,而其他時間會隱藏圖層,並且需要勾選複選框它會出現幾次。
我想找到一個解決方案來解決這個問題,這樣地圖頁面將首先加載未勾選的複選框並隱藏圖層。然後當勾選複選框時,圖層將會打開,然後再次關閉複選框未勾選?
是的,它缺少,但JavaScript不需要分號。 – 2013-03-25 22:37:24
不一定是這樣,我見過缺少亞冒號的情況會導致IE出現異常。 – user1452425 2013-03-25 22:48:19
感謝您的幫助...我嘗試添加您建議的檢查,但那麼地圖不會加載...? – Riverbum75 2013-03-27 06:07:14