0

我正在使用 Google Fusion Tables API v1 请求以 JSON 格式加载到融合表中的点 kml。我使用这个请求:

<script src="https://www.googleapis.com/fusiontables/v1/query?sql=SELECT * FROM 1CNJWjLDYgBkJGZVslJ67Fak4DyqadEFIabzQ60&callback=dataHandler&key=AIzaSyKBzv-rs32Zat-8fDZuWwOXo7hCtpBR78"></script>

查看请求数据

回调函数使用GeoJSON 到 Google Maps Utility来解析请求中的标记。几何数据位于名为“geometry”的 idx 2 列中:

    function dataHandler(results) {
        var rows = results['rows'];
        for (var i in rows){
            var geometries = rows[i][2]['geometry'];
            //alert(geometries.coordinates)
            if (geometries) {
              var options = {
                "icon": "http://labs.google.com/ridefinder/images/mm_20_red.png"
              };
              var overlay = new GeoJSON(geometries, options);
              if (overlay.error){
                alert("error in GeoJSON")
              }else{
                alert(map) // return undefined
                overlay.setMap(map); //not working
              } 
            }else {
              alert('No geometry column')
            }
          }
        }

JSON 或 GeoJSON 实用程序创建的覆盖没有错误,但谷歌地图变量“地图”在函数中是未定义的,即使它是全局定义的。这就是为什么 overlay.setMap(map) 不工作的原因。我想在定义 var 映射之前调用回调函数。我该如何解决这个问题?

4

1 回答 1

0
<script src="https://www.googleapis.com/fusiontables/v1/query?sql=SELECT * FROM
 TABLE_ID&callback=dataHandler&key=API_KEY"></script>

    var map;
    var overlays = [];


    function initialize(){
      map = new google.maps.Map(document.getElementById('map'), {
            center: new google.maps.LatLng(49.75, -71.25),
            zoom: 5,
            mapTypeId: 'roadmap'
      });

      for (var i = 0; i < overlays.length; i++){
          overlays[i].setMap(map);
      }
    }


    function dataHandler(results) {
        var rows = results['rows'];
        var cols = results['columns'];
        for (var i = 0; i < cols.length; i++){
          if (cols[i] == 'geometry') {
            var ColIndex = i;
          }
        }
        if (!ColIndex){
          alert('Geometry column "geometry" not found.')
        }
        for (var i in rows){
          var geometries = rows[i][ColIndex]['geometry'];
          if (geometries) {
            var options = {
              "icon": "http://labs.google.com/ridefinder/images/mm_20_red.png"
            };
            var overlay = new GeoJSON(geometries,options);
            if (overlay.error){
              alert("error in overlay")
            }else{
              overlays.push(overlay);
            } 
          }else {
            alert("error in geoJSON response")
          }
        }
于 2013-03-13T20:42:14.253 回答