【问题标题】:Google Maps API v3 & fusion layers: get row dataGoogle Maps API v3 和融合层:获取行数据
【发布时间】:2015-01-12 05:11:12
【问题描述】:

当标记放置在地图上时,我正在尝试使用几何图形从谷歌地图中的融合表图层中获取行 ID。这是我第一次尝试使用融合层,尽管阅读了文档并花费了数小时进行测试,但我现在被难住了。

如果用户点击地图,我可以获取行 [“id”] 没问题,但如果我使用任何其他方法(例如 autocomplete.getPlace)以编程方式放置标记,我无法从 FusionLayer 获取行 ID这就是我所需要的。我的代码如下:

/* get row id if user clicks on the map */
google.maps.event.addListener(fusionLayer, "click", function(e) {
    document.getElementById("MY_HIDDEN_FIELD").value = e.row["id"].value;
    placeMarker(e.latLng, map);
});

function placeMarker(position, map) {
    addMarker(map,true,position);
    map.panTo(position);
    document.getElementById("latitude").value = position.lat();
    document.getElementById("longitude").value = position.lng();
}

function addMarker(map,draggable,position){
    deleteMarkers();
    if (infowindow) {
        infowindow.close();
    }
    draggable = typeof draggable !== "undefined" ? draggable : true;
    var icon = document.getElementById("map-icon").value;
    var marker = new google.maps.Marker({
        map: map,
        draggable: true,
        position: position,
        icon: icon
    });
    arr_markers.push(marker);
}

我已经搞砸了好几个小时试图解决这个问题。我尝试在 fusionLayer 上的 bounds_changed 和 center_changed 上放置侦听器,如下所示,但这不起作用,因为未定义 e:

google.maps.event.addListener(fusionLayer, "center_changed", function(e) {
    document.getElementById("MY_HIDDEN_FIELD").value = e.row["id"].value;
    placeMarker(e.latLng, map);
});

谁能帮我指出正确的方向?

非常感谢

【问题讨论】:

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


    【解决方案1】:

    我认为您实际上不能对 fusionTables 层进行编程查询。相反,您使用包装器让 google maps API 为您查询 fusionTable 本身,并将结果呈现在您的地图上。

    您要实现的目标不应依赖于渲染的 fusionTablesLayer 对象,而应直接查询fusion table API。 Derek Eder 的Fusiontables Map Template 使用如下函数:

       // replace with your table ID
       var fusionTableId = "1FQeX0LbhOo7M6NH19xwHGB6sozCsL1GK4sngqiTy";     
    
       //*You need an API key. found at https://code.google.com/apis/console/
       var googleApiKey =  "[YOUR API KEY]";
    
       var queryStr = [];
       queryStr.push("SELECT * ");
       queryStr.push(" FROM " + fusionTableId);
    
       // replace with your geo field or fields accordingly, coordinates and tolerance radius
       // queryStr.push(" WHERE ST_INTERSECTS(geometry, CIRCLE(LATLNG(37.4, -122.1), 500))");
    
       var sql = encodeURIComponent(queryStr.join(" "));
       $.ajax({
          url: "https://www.googleapis.com/fusiontables/v1/query?sql="+sql+"&key="+googleApiKey,
          dataType: "json"
       }).done(function (response) {
          console.log(response);
       });
    

    免责声明:我是 Derek 项目的谦虚贡献者。

    注意事项

    • 您需要一个 API 密钥,但它是免费的,值得付出努力。
    • 您需要细化该 queryStr 以实际查明您想要的位置。
    • 即使在要求精确的坐标对时,您也可能得到不止一个匹配的行,或者没有。
    • 我使用 jQuery 是因为我需要 ajax 方法,但这部分取决于你。
    • 查询表只是为了获取 row_id 可能有点过头了,但毕竟这是一个便宜的请求。

    【讨论】:

    • 非常感谢 amenadiel,这正是我想要的,stackoverflow 的社区非常棒,非常感谢您的想法和建议。
    • 很高兴它对您有所帮助。
    • @amenadiel 我对某个功能的可行性有疑问。我们可以点击谷歌地图的行数据,比如说代表建筑物的灰色块吗?我想单击它并更改块颜色。欢迎您提出建议和想法。
    • @MehulJoisar 你应该创建一个新问题来解释你的用例。我们还在谈论 fusionTablesLayer 吗?
    【解决方案2】:

    为了完整起见,我删除了融合层,因为它并不是真正需要的,在 amenadiel 的帮助下,解决方案现在看起来像这样:

    /* get row id if user clicks on the map */
    google.maps.event.addListener(map, "click", function(e) {
        placeMarker(e.latLng, map);
    });
    
    function getROWID(lat,lng){
        var queryStr = [];
        queryStr.push("SELECT id ");
        queryStr.push(" FROM " + fusionTableId);
        // set coordinates and tolerance radius
        queryStr.push(" WHERE ST_INTERSECTS(geometry, CIRCLE(LATLNG("+lat+", "+lng+"), 1))");
    
        var sql = encodeURIComponent(queryStr.join(" "));
        $.ajax({
            url: "https://www.googleapis.com/fusiontables/v2/query?sql="+sql+"&key="+googleApiKey,
            dataType: "json"
        }).done(function (response) {
            document.getElementById("MY_HIDDEN_FIELD").value = response.rows[0][0];
        });
    }
    
    function placeMarker(position, map) {
        addMarker(map,true,position);
        map.panTo(position);
        document.getElementById("latlon").value = position;
        document.getElementById("latitude").value = position.lat();
        document.getElementById("longitude").value = position.lng();
        getROWID(position.lat(),position.lng());
    }
    
    function addMarker(map,draggable,position){
        deleteMarkers();
        if (infowindow) {
            infowindow.close();
        }
        draggable = typeof draggable !== "undefined" ? draggable : true;
        var icon = document.getElementById("map-icon").value;
        var marker = new google.maps.Marker({
            map: map,
            draggable: true,
            position: position,
            icon: icon
        });
        arr_markers.push(marker);
    }
    

    【讨论】:

    • 很好,您使用的是 API 的 v2。您是否必须更改任何内容才能使查询返回一些行?
    • 上面的查询是我使用的。我将 * 换成了“id”,因为这就是我所需要的,但除此之外,您上面给出的查询示例运行良好。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-13
    • 2014-10-05
    • 1970-01-01
    • 1970-01-01
    • 2015-03-04
    • 2016-05-28
    • 1970-01-01
    相关资源
    最近更新 更多