Arrays 将多条多段线添加到单个阵列
我刚开始使用Maps API V3,但我找不到一种方法将多条多段线放在一个数组中,因此我可以使用按钮(一次全部)使它们显示/隐藏。这是我的密码 //设置点击事件监听器:只需将地图设置为波哥大的中心Arrays 将多条多段线添加到单个阵列,arrays,google-maps-api-3,google-polyline,Arrays,Google Maps Api 3,Google Polyline,我刚开始使用Maps API V3,但我找不到一种方法将多条多段线放在一个数组中,因此我可以使用按钮(一次全部)使它们显示/隐藏。这是我的密码 //设置点击事件监听器:只需将地图设置为波哥大的中心 var bogota = new google.maps.LatLng(4.697316, -74.044805); var directionsDisplay; var directionsService = new google.maps.DirectionsService(); //功能化 f
var bogota = new google.maps.LatLng(4.697316, -74.044805);
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
//功能化
function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer();
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
// passing in this DIV.
var botonesDiv = document.createElement('div');
var espacioControles = new Controles(botonesDiv, map);
botonesDiv.index = 1;
map.controls[google.maps.ControlPosition.TOP_RIGHT].push(botonesDiv);
// Ingresamos las lineas de la CICLORUTAS
// Son de color rojo
var ColorViaCR = "#FF0000";
var OpacidadCR = 1.0;
var GrosorCR = 2;
var CRArray = [];
var CiclorutasCoordinates116 = [
new google.maps.LatLng(4.698555, -74.049835),
new google.maps.LatLng(4.698437, -74.049004),
new google.maps.LatLng(4.696328, -74.043339)
];
var Cicloruta116 = new google.maps.Polyline({
path: CiclorutasCoordinates116,
strokeColor: ColorViaCR,
strokeOpacity: OpacidadCR,
strokeWeight: GrosorCR
});
CRArray.push(Cicloruta116);
var CiclorutasCoordinates112 = [
new google.maps.LatLng(4.696723396389598, -74.05013248790056),
new google.maps.LatLng(4.696632507503862, -74.04982671607286),
new google.maps.LatLng(4.696480134933544, -74.04946461785585),
new google.maps.LatLng(4.687099216812973, -74.03546721674502),
new google.maps.LatLng(4.687072484420475, -74.03525800444186)
];
var Cicloruta112 = new google.maps.Polyline({
path: CiclorutasCoordinates112,
strokeColor: ColorViaCR,
strokeOpacity: OpacidadCR,
strokeWeight: GrosorCR
});
CRArray.push(Cicloruta112);
directionsDisplay.setMap(map);
}
//FIN取消初始化
//显示/隐藏多段线
function showCR() {
for (var i=0; i< CRArray.length; i++) {
if(CRArray[i].getMap(map)) {
CRArray[i].setMap(null);
} else {
CRArray[i].setMap(map);
}
}
}
函数showCR(){
对于(变量i=0;i
//创建按钮
function Controles(DivGeneral, mapa) {
// Set CSS for the control border.
var InfoUI = document.createElement('div');
InfoUI.style.backgroundColor = 'white';
InfoUI.style.borderStyle = 'solid';
InfoUI.style.borderWidth = '1pt';
InfoUI.style.borderColor = '#CCC';
InfoUI.style.cursor = 'pointer';
InfoUI.style.textAlign = 'left';
InfoUI.title = 'Ver Ciclorutas';
InfoUI.style.marginTop = '2px';
DivGeneral.appendChild(InfoUI);
// Set CSS for the control interior.
var InfoText = document.createElement('div');
InfoText.style.fontFamily = 'Arial,sans-serif';
InfoText.style.color = '#000';
InfoText.style.fontSize = '12px';
InfoText.style.paddingLeft = '8px';
InfoText.style.paddingRight = '8px';
InfoText.style.paddingTop = '12px';
InfoText.style.paddingBottom = '12px';
InfoText.innerHTML = '<strong>Ver Ciclorutas</strong>';
InfoUI.appendChild(InfoText);
google.maps.event.addDomListener(InfoUI, 'click', function() {
showCR();
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
功能控件(DivGeneral,mapa){
//为控件边框设置CSS。
var InfoUI=document.createElement('div');
InfoUI.style.backgroundColor='白色';
InfoUI.style.borderStyle='solid';
InfoUI.style.borderWidth='1pt';
InfoUI.style.borderColor='#CCC';
InfoUI.style.cursor='pointer';
InfoUI.style.textAlign='left';
InfoUI.title='Ver Ciclorutas';
InfoUI.style.marginTop='2px';
DivGeneral.appendChild(InfoUI);
//为控件内部设置CSS。
var InfoText=document.createElement('div');
InfoText.style.fontFamily='Arial,无衬线';
InfoText.style.color='#000';
InfoText.style.fontSize='12px';
InfoText.style.paddingLeft='8px';
InfoText.style.paddingRight='8px';
InfoText.style.paddingTop='12px';
InfoText.style.paddingBottom='12px';
InfoText.innerHTML='verciclorutas;
InfoUI.appendChild(InfoText);
google.maps.event.addDomListener(InfoUI,'click',function(){
showCR();
});
}
google.maps.event.addDomListener(窗口“加载”,初始化);
我认为答案在于您对CRArray的定义
通过在initialize()内部定义CRArray,您已经将数组的作用域设置为该函数的内部。要在其他函数中访问CRArray,最简单的方法是在全局范围中定义它(将var CRArray移到initialize()之外)
请尝试以下代码:
var CRArray = new Array();
function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer();
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
// passing in this DIV.
var botonesDiv = document.createElement('div');
var espacioControles = new Controles(botonesDiv, map);
botonesDiv.index = 1;
map.controls[google.maps.ControlPosition.TOP_RIGHT].push(botonesDiv);
// Ingresamos las lineas de la CICLORUTAS
// Son de color rojo
var ColorViaCR = "#FF0000";
var OpacidadCR = 1.0;
var GrosorCR = 2;
var CiclorutasCoordinates116 = [
new google.maps.LatLng(4.698555, -74.049835),
new google.maps.LatLng(4.698437, -74.049004),
new google.maps.LatLng(4.696328, -74.043339)
];
var Cicloruta116 = new google.maps.Polyline({
path: CiclorutasCoordinates116,
strokeColor: ColorViaCR,
strokeOpacity: OpacidadCR,
strokeWeight: GrosorCR
});
CRArray.push(Cicloruta116);
var CiclorutasCoordinates112 = [
new google.maps.LatLng(4.696723396389598, -74.05013248790056),
new google.maps.LatLng(4.696632507503862, -74.04982671607286),
new google.maps.LatLng(4.696480134933544, -74.04946461785585),
new google.maps.LatLng(4.687099216812973, -74.03546721674502),
new google.maps.LatLng(4.687072484420475, -74.03525800444186)
];
var Cicloruta112 = new google.maps.Polyline({
path: CiclorutasCoordinates112,
strokeColor: ColorViaCR,
strokeOpacity: OpacidadCR,
strokeWeight: GrosorCR
});
CRArray.push(Cicloruta112);
directionsDisplay.setMap(map);
}
魔法就是这样发生的。非常感谢你,布莱恩。我应该在三天前问你。