2014-04-09 110 views
0

我無法找到代碼中的谷歌地圖使用複選框層 但是當我選中的複選框的錯誤爬我需要做的是這樣that但我的層使用複選框層谷歌地圖

toggleLayers(0) [setMap([object Object]] returns status: undefined 
toggleLayers(0) [setMap(null] returns status: INVALID_REQUEST 
toggleLayers(1) [setMap([object Object]] returns status: undefined 
toggleLayers(1) [setMap(null] returns status: undefined 

我所做的不正確?我出了什麼問題? P.S.對不起,我的英語

function initialize() { 
    var latlng = new google.maps.LatLng(59.93644, 30.152664); 
    var myOptions = { 
      zoom: 10, 
      center: latlng, 
      mapTypeIds: google.maps.MapTypeId.ROADMAP 
    } 
    map = new google.maps.Map(document.getElementById("map_canvas"),myOptions); 
} 

var layers=[]; 

layers[0] = new google.maps.KmlLayer('ban.kml', 
{preserveViewport: true}); 

layers[1] = new google.maps.KmlLayer('http://wrf1.geology.um.maine.edu/bipush/kml/plot_5938.kmz', 
{preserveViewport: true}); 
var map; 



function toggleLayers(i) 
{ 

    if(layers[i].getMap()==null) { 
    layers[i].setMap(map); 
    } 
    else { 
    layers[i].setMap(null); 
    } 
    document.getElementById('status').innerHTML += "toggleLayers("+i+") [setMap("+layers[i].getMap()+"] returns status: "+layers[i].getStatus()+"<br>"; 
} 

<body onload="initialize()"> 
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 /> 
<div id="map_canvas" style="height:500px; width:600px;"></div> 
<div id="status"></div> 

回答

1

試試,看看下面的代碼將工作,如果不考慮改變你的「ban.kml」文件的URL,我覺得可能是原因,爲了讓谷歌抓取您的kml文件它需要有一個Http://

請嘗試這個版本的代碼註釋,您將需要更改座標和API密鑰;

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=YourAPI  "></script> 
<script type="text/javascript" src="~/Scripts/test.js"></script> 
<script type="text/javascript"> 
var layers=[]; 
layers[0] = new google.maps.KmlLayer('ban.kml', { preserveViewport: true }); 
layers[1] = new google.maps.KmlLayer('http://wrf1.geology.um.maine.edu/bipush/kml/plot_5938.kmz', { preserveViewport: true }); 
var map; 
function initialize() { 
var latlng = new google.maps.LatLng(-23.670035, 27.536779); 
var myOptions = { 
     zoom: 13, 
     center: latlng, 
     mapTypeIds: google.maps.MapTypeId.SATELLITE 
} 
map = new google.maps.Map(document.getElementById("map_canvas"),myOptions); 
} 
function toggleLayers(i) 
{ 
if(layers[i].getMap()==null) { 
    layers[i].setMap(map);  
} 
else { 
layers[i].setMap(null); 
} 
document.getElementById('status').innerHTML += "toggleLayers("+i+")[setMap("+layers[i].getMap()+"] returns status: "+layers[i].getStatus()+"<br>"; 
} 
</script> 
    </head> 
    <body onload="initialize()"> 
letterbox locations <input type="checkbox" id="layer_01" onclick="toggleLayers(0);"/> 
landscape locations <input type="checkbox" id="layer_02" onclick="toggleLayers(1);"/> 
<div id="map_canvas" style="height:500px; width:600px;"></div> 
<div id="status"></div> 
</body>