函数更新地图谷歌地图 API

function updateMap for Google Maps API

本文关键字:API 谷歌地图 新地图 更新 函数      更新时间:2023-09-26

我在谷歌地图API中的updateMap功能上遇到了问题...特别是使用多个"else if"语句来查询数据。我的代码如下。第一个"如果"有效,第一个"else if"有效,最后一个"else"用于更新我的地图,第二个"else if"不会更新我的地图。用户通过下拉菜单传递"类型"和"Visibilit2"的值。

有什么建议可以解决这个问题吗?地图需要根据来自其中之一、两者或两者都不是的用户值进行更新,方法是将下拉菜单设置为空白(这是最后一个 else 的考虑(。理想情况下,我想将其扩展为包括三个或四个额外的查询选项。数据存储在融合表中。

google.maps.event.addDomListener(document.getElementById('Type'), 'change', function(){updateMap(layer, tableID, locationColumn);});
google.maps.event.addDomListener(document.getElementById('Visibilit2'), 'change', function(){updateMap(layer, tableID, locationColumn);});
function updateMap(layer, tableID, locationColumn){
    var Type = document.getElementById('Type').value;
    var Visibilit2 = document.getElementById('Visibilit2').value
    if (Type, Visibilit2){
        layer.setOptions({
            query: {
                select: locationColumn,
                from: tableID,
                where: "Type = '" + Type + "' AND Visibilit2 = '" + Visibilit2 + "'"
            }
        });
    } else if (Type){
        layer.setOptions({
            query: {
                select: locationColumn,
                from: tableID,
                where: "Type = '" + Type + "'"
            }
        });
    } else if (Visibilit2){
        layer.setOptions({
            query: {
                select: locationColumn,
                from: tableID,
                where: "Visibilit2 = '" + Visibilit2 + "'"
            }
        });
    }
    else {
        layer.setOptions({
            query: {
                select: locationColumn,
                from: tableID
            }
        });
    }
}

好的,所以我在发布后几乎立即解决了它。但!我不确定这是最有效的方法,如果我添加更多变量,我认为它不会解决问题。代码如下,我非常欢迎任何更好的方法的建议。我只是在第一个 if 中嵌套了一个 if,我仍然对为什么多个"else if"不起作用感到困惑......

谢谢!

function updateMap(layer, tableID, locationColumn){
    var Type = document.getElementById('Type').value;
    var Visibilit2 = document.getElementById('Visibilit2').value
    if (Type){
        if (Visibilit2){
            layer.setOptions({
                query: {
                    select: locationColumn,
                    from: tableID,
                    where: "Type = '" + Type + "' AND Visibilit2 = '" + Visibilit2 + "'"
                }
            });
        }  else {
                layer.setOptions({
                query: {
                    select: locationColumn,
                    from: tableID,
                    where: "Type = '" + Type + "'"
                    }
                });
            }
    }  else if (Visibilit2){
        layer.setOptions({
            query: {
                select: locationColumn,
                from: tableID,
                where: "Visibilit2 = '" + Visibilit2 + "'"
            }
        });
    }
    else {
        layer.setOptions({
            query: {
                select: locationColumn,
                from: tableID
            }
        });
    }
}