当标记放置在地图上时,我尝试使用几何从Google地图的融合表图层中获取行ID。这是我第一次尝试使用融合层,尽管阅读了文档并花了很多时间进行测试,但我现在还是很困惑。

如果用户单击地图,则可以毫无问题地获得row [“ 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);
}


我已经花了好几个小时试图解决这个问题。我已经尝试如下将监听器放置在bounds_changed和center_changed的fusionLayer上,但由于未定义e而没有用:

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


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

非常感谢

最佳答案

我认为您实际上无法在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可能会过大,但这毕竟是一个便宜的请求。

10-04 22:38
查看更多