我计划显示一个带有下拉列表的空白 map ,这将允许用户选择希望显示的图层(来自 Fusion Tables)。在网上我没有找到这方面的示例代码。我当前的代码如下。
<html>
<head>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var center = new google.maps.LatLng(-40.979898,173.305662);
var infowindow = new google.maps.InfoWindow();
function init() {
map = new google.maps.Map(document.getElementById('map_canvas'), {
center: center,
zoom: 6,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var street_address1 = new google.maps.FusionTablesLayer({
query: {
select: 'street_address_Auckland_000001_080000',
from: '1WoKvD3GvX8Rif7TFmHGEvvbtpbYWFs-K-DeF7WU'
},
suppressInfoWindows: true
});
google.maps.event.addListener(street_address1, 'click', function(e) {
if(infowindow) infowindow.close();
else infowindow = new google.maps.InfoWindow();
infoWindowContent = infowindow.setContent(
'<h3 style="font-family: Arial; font-size: 13px; color: #FFFFFF; background-color: #0099FF; text-align: center; font-style: italic">' + 'Street Address'
+ '<h1 style="font-family: Arial; font-size: 12px; color: #333333; background-color: #E2E2E2; text-align: left "> ID: ' + e.row['id'].value
+ '</a>') ;
infowindow.setPosition(e.latLng);
map.setCenter(e.latLng);
infowindow.open(map);
});
var street_address2 = new google.maps.FusionTablesLayer({
query: {
select: 'street_address_Auckland_080000_160000',
from: '1dlsTxt8kyA92pq3jwODwQtaEcWxn8dax0slrULM'
},
suppressInfoWindows: true
});
google.maps.event.addListener(street_address2, 'click', function(e) {
if(infowindow) infowindow.close();
else infowindow = new google.maps.InfoWindow();
infoWindowContent = infowindow.setContent(
'<h3 style="font-family: Arial; font-size: 13px; color: #FFFFFF; background-color: #0099FF; text-align: center; font-style: italic">' + 'Street Address'
+ '<h1 style="font-family: Arial; font-size: 12px; color: #333333; background-color: #E2E2E2; text-align: left "> ID: ' + e.row['id'].value
+ '</a>') ;
infowindow.setPosition(e.latLng);
map.setCenter(e.latLng);
infowindow.open(map);
});
var street_address3 = new google.maps.FusionTablesLayer({
query: {
select: 'street_address_Auckland_160000_240000',
from: '1bb-M9LcG_dFS3ejYzT3hnpOQUzMMBT2GR8VH5hY'
},
suppressInfoWindows: true
});
google.maps.event.addListener(street_address3, 'click', function(e) {
if(infowindow) infowindow.close();
else infowindow = new google.maps.InfoWindow();
infoWindowContent = infowindow.setContent(
'<h3 style="font-family: Arial; font-size: 13px; color: #FFFFFF; background-color: #0099FF; text-align: center; font-style: italic">' + 'Street Address'
+ '<h1 style="font-family: Arial; font-size: 12px; color: #333333; background-color: #E2E2E2; text-align: left "> ID: ' + e.row['id'].value
+ '</a>') ;
infowindow.setPosition(e.latLng);
map.setCenter(e.latLng);
infowindow.open(map);
});
$('#street_address').select(function(){
if ($(this).is('1'))
street_address1.setMap(map);
else
street_address1.setMap(null);
});
$('#street_address').removeAttr('disabled');
$('#street_address').select(function(){
if ($(this).is('2'))
street_address2.setMap(map);
else
street_address2.setMap(null);
});
$('#street_address').removeAttr('disabled');
$('#street_address').select(function(){
if ($(this).is('3'))
street_address3.setMap(map);
else
street_address3.setMap(null);
});
$('#street_address').removeAttr('disabled');
}
</script>
</head>
<body id="body" onload="init();">
<div id="list" style="position:absolute; left:25; top:5px; width:100px; height:25px;">
<select name="street_address" id="street_address">
<option value="1">Layer 1</option>
<option value="2">Layer 2</option>
<option value="3">Layer 3</option>
</select>
</div>
<div id="map_canvas" style="position:absolute; left:25; top:35px; width:1000px; height:760px"></div>
</body>
</html>
有人有此功能的示例代码吗?
最好的, 达尔科
最佳答案
To make things clear, I'm not using jQuery.
(1) 我正在为 SELECT
标签使用 onchange="selectChange()"
事件,下面是代码。
function selectChange(){
var value = document.getElementById("street_address").value;
if (value =='1')
street_address1.setMap(map);
else if (value =='2')
street_address2.setMap(map);
else if (value =='3')
street_address2.setMap(map);
else
street_address1.setMap(null);
document.getElementById("street_address").removeAttribute("disabled");
};
我在这里优化了你的 if..else..
语句。
(2) onload="init();"
是一小段代码,因此很简单。
function init() {
map = new google.maps.Map(document.getElementById('map_canvas'), {
center: center,
zoom: 6,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
}
保持不变,这里是删除 jQuery 的完整源代码。
<html>
<head>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var center = new google.maps.LatLng(-40.979898,173.305662);
var infowindow = new google.maps.InfoWindow();
function init() {
map = new google.maps.Map(document.getElementById('map_canvas'), {
center: center,
zoom: 6,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
}
var street_address1 = new google.maps.FusionTablesLayer({
query: {
select: 'street_address_Auckland_000001_080000',
from: '1WoKvD3GvX8Rif7TFmHGEvvbtpbYWFs-K-DeF7WU'
},
suppressInfoWindows: true
});
google.maps.event.addListener(street_address1, 'click', function(e) {
if(infowindow) infowindow.close();
else infowindow = new google.maps.InfoWindow();
infoWindowContent = infowindow.setContent(
'<h3 style="font-family: Arial; font-size: 13px; color: #FFFFFF; background-color: #0099FF; text-align: center; font-style: italic">' + 'Street Address'
+ '<h1 style="font-family: Arial; font-size: 12px; color: #333333; background-color: #E2E2E2; text-align: left "> ID: ' + e.row['id'].value
+ '</a>') ;
infowindow.setPosition(e.latLng);
map.setCenter(e.latLng);
infowindow.open(map);
});
var street_address2 = new google.maps.FusionTablesLayer({
query: {
select: 'street_address_Auckland_080000_160000',
from: '1dlsTxt8kyA92pq3jwODwQtaEcWxn8dax0slrULM'
},
suppressInfoWindows: true
});
google.maps.event.addListener(street_address2, 'click', function(e) {
if(infowindow) infowindow.close();
else infowindow = new google.maps.InfoWindow();
infoWindowContent = infowindow.setContent(
'<h3 style="font-family: Arial; font-size: 13px; color: #FFFFFF; background-color: #0099FF; text-align: center; font-style: italic">' + 'Street Address'
+ '<h1 style="font-family: Arial; font-size: 12px; color: #333333; background-color: #E2E2E2; text-align: left "> ID: ' + e.row['id'].value
+ '</a>') ;
infowindow.setPosition(e.latLng);
map.setCenter(e.latLng);
infowindow.open(map);
});
var street_address3 = new google.maps.FusionTablesLayer({
query: {
select: 'street_address_Auckland_160000_240000',
from: '1bb-M9LcG_dFS3ejYzT3hnpOQUzMMBT2GR8VH5hY'
},
suppressInfoWindows: true
});
google.maps.event.addListener(street_address3, 'click', function(e) {
if(infowindow) infowindow.close();
else infowindow = new google.maps.InfoWindow();
infoWindowContent = infowindow.setContent(
'<h3 style="font-family: Arial; font-size: 13px; color: #FFFFFF; background-color: #0099FF; text-align: center; font-style: italic">' + 'Street Address'
+ '<h1 style="font-family: Arial; font-size: 12px; color: #333333; background-color: #E2E2E2; text-align: left "> ID: ' + e.row['id'].value
+ '</a>') ;
infowindow.setPosition(e.latLng);
map.setCenter(e.latLng);
infowindow.open(map);
});
</script>
</head>
<body id="body" onload="init();">
<div id="list" style="position:absolute; left:25; top:5px; width:100px; height:25px;">
<select name="street_address" id="street_address" onchange="selectChange()">
<option value="1">Layer 1</option>
<option value="2">Layer 2</option>
<option value="3">Layer 3</option>
</select>
</div>
<div id="map_canvas" style="position:absolute; left:25; top:35px; width:1000px; height:760px"></div>
</body>
</html>
检查这个JSFiddle . 截图:
希望你明白。
关于javascript - Google Maps API V3 - 从下拉列表中选择 Fusion Table 图层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17342665/