【问题标题】:function updateMap for Google Maps API谷歌地图 API 的函数 updateMap
【发布时间】:2015-02-17 10:48:42
【问题描述】:

我在使用 google Maps API 中的 updateMap 函数时遇到问题...特别是使用多个“else if”语句来查询数据。我的代码如下。第一个“if”有效,第一个“else if”有效,最后一个“else”更新我的地图,第二个“else if”不会更新我的地图。用户通过下拉菜单传递“Type”和“Visibilit2”的值。

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

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
            }
        });
    }
}

【问题讨论】:

    标签: javascript google-maps google-maps-api-3 google-fusion-tables


    【解决方案1】:

    好的,所以我在发布后几乎立即解决了它。但!我不确定这是最有效的方法,我认为如果我添加更多变量也不会解决问题。代码如下,我非常欢迎任何关于更好方法的建议。我只是在第一个 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
                }
            });
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-17
      • 1970-01-01
      • 2013-08-05
      相关资源
      最近更新 更多