Google maps api 3 仅显示路径上的第一个和最后一个标记

Google maps api 3 仅显示路径上的第一个和最后一个标记,google-maps-api-3,Google Maps Api 3,对于在google maps上生成标记,我使用下面的代码调用函数displayLocation: for (var i=0; i<data.length; i++) { displayLocation(data[i]); } 在我设置路径的这部分代码中,我调用: varBool = true; dibujaRutaAST1(map, rutaAST1, varBool); 其功能是: var ruta = null; function dibujaRutaAST1(mapa,

对于在google maps上生成标记,我使用下面的代码调用函数displayLocation:

for (var i=0; i<data.length; i++) {
     displayLocation(data[i]);
}
在我设置路径的这部分代码中,我调用:

varBool = true;
dibujaRutaAST1(map, rutaAST1, varBool);
其功能是:

var ruta = null;
function dibujaRutaAST1(mapa, rutaVar, varBool){
if(!ruta){
    var coordRuta = rutaVar;
    console.log("En función dibujo de rutas AST1: "+rutaVar);
    ruta= new google.maps.Polyline({
        map: mapa,
        path: coordRuta,
        geodesic: true,
        strokeColor: "#FF0000",
        strokeOpacity: 1.0,
        strokeWeight: 3
    });
    console.log(ruta);
}if(varBool){
    ruta.setMap(mapa);
}else{
    ruta.setMap(null);
}
}
标记数组上的迭代:

function mostrarMarcas(nombreEquipo){
                for(var i=0;i<arrayMarcadores.length;i++){
                    if(arrayMarcadores[i].title==nombreEquipo){
                        arrayMarcadores[i].setVisible(true);
                    }
                }
            }
函数mostrarMarcas(nombreEquipo){

对于(var i=0;i最后,我只需从数组中创建第一个和最后一个标记,其中包含所有点:

if(rutaAST1.length!=0){
    dibujaRutaAST1(map, rutaAST1, varBool);
    var imagen ='img/markers/blue_MarkerA.png';
    var markerIniAST1 = new google.maps.Marker({
        position: rutaAST1[0],
        map: map,
        icon: imagen,
        draggable: false,
        visible: true,
        title: "AST1"
    });
    google.maps.event.addListener(markerIniAST1, 'click', function(){
        infowindow.setContent(contentAST1[0]);
        infowindow.open(map, markerIniAST1);
    });
    var markerFinAST1 = new google.maps.Marker({
        position: rutaAST1[rutaAST1.length-1],
        map: map,
        icon: imagen,
        draggable: false,
        visible: true,
        title: "AST1"
    });
    google.maps.event.addListener(markerFinAST1, 'click', function(){
        infowindow.setContent(contentAST1[contentAST1.length-1]);
        infowindow.open(map, markerFinAST1);
    });
}

非常感谢您帮助我清理了这部分代码:)

在代码的某些部分,您使用
setMap()设置了路径和标记
。你能给我们看一下吗?好的,我看到了你的更新,但我指的是你使用
for
的任何
来迭代
arrayMarcadores
。在这一部分中,你可以只使用第一个和最后一个元素,如
arrayMarcadores[0]
arrayMarcadores[arrayMarcadores.length-1]
。对不起,我忘记了那个部分,谢谢你的帮助。嗯,不完全是这样。还有一个部分你添加了标记,但没有将其设置为可见。类似于
arrayMarcadores[I].setMap(mapa)
。我的代码中一定有错误,工作正常,但我没有那个部分。谢谢。
if(rutaAST1.length!=0){
    dibujaRutaAST1(map, rutaAST1, varBool);
    var imagen ='img/markers/blue_MarkerA.png';
    var markerIniAST1 = new google.maps.Marker({
        position: rutaAST1[0],
        map: map,
        icon: imagen,
        draggable: false,
        visible: true,
        title: "AST1"
    });
    google.maps.event.addListener(markerIniAST1, 'click', function(){
        infowindow.setContent(contentAST1[0]);
        infowindow.open(map, markerIniAST1);
    });
    var markerFinAST1 = new google.maps.Marker({
        position: rutaAST1[rutaAST1.length-1],
        map: map,
        icon: imagen,
        draggable: false,
        visible: true,
        title: "AST1"
    });
    google.maps.event.addListener(markerFinAST1, 'click', function(){
        infowindow.setContent(contentAST1[contentAST1.length-1]);
        infowindow.open(map, markerFinAST1);
    });
}