如何从FusionTablesLayer获取字符串列表并将其与地图关联

时间:2018-05-24 14:20:23

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

我有使用FusionTablesLayer构建的谷歌地图

如何从此FusionTablesLayer获取字符串列表并将其与地图关联。要点击列表中的某个项目,就会在地图上打开一个气球。

以下是地图http://air-in.ru/ajax/chat/test.php

的示例

这是地图应该如何显示的示例。

how map should look



<body>
<style type="text/css">
    html, body, #googft-mapCanvas {
        height: 100% !important;
        margin: 0;
        padding: 0;
        width: 100% !important;
    }
</style>

<script type="text/javascript" src="https://maps.google.com/maps/api/js?key=AIzaSyBxMTfLhegh_rO8w0-vRP_oKdaJOvhOUUI"></script>

<script type="text/javascript">
    function initialize() {
        var isMobile = (navigator.userAgent.toLowerCase().indexOf('android') > -1) ||
            (navigator.userAgent.match(/(iPod|iPhone|iPad|BlackBerry|Windows Phone|iemobile)/));
        if (isMobile) {
            var viewport = document.querySelector("meta[name=viewport]");
            viewport.setAttribute('content', 'initial-scale=1.0, user-scalable=no');
        }
        var mapDiv = document.getElementById('googft-mapCanvas');
        mapDiv.style.width = isMobile ? '100%' : '500px';
        mapDiv.style.height = isMobile ? '100%' : '300px';

        var map = new google.maps.Map(mapDiv, {
            center: new google.maps.LatLng(56.32085914650647, 62.40977352913535),
            zoom: 4,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        });
        map.controls[google.maps.ControlPosition.RIGHT_BOTTOM].push(document.getElementById('googft-legend-open'));
        map.controls[google.maps.ControlPosition.RIGHT_BOTTOM].push(document.getElementById('googft-legend'));

        layer = new google.maps.FusionTablesLayer({
            map: map,
            heatmap: { enabled: false },
            query: {
                select: "col0",
                from: "1XlnmbK0m0s4rHdadG_hkmZP-dSr2ruBzQxnMK4Uv",
                where: ""
            },
            options: {
                styleId: 2,
                templateId: 2
            }
        });


        if (isMobile) {
            var legend = document.getElementById('googft-legend');
            var legendOpenButton = document.getElementById('googft-legend-open');
            var legendCloseButton = document.getElementById('googft-legend-close');
            legend.style.display = 'none';
            legendOpenButton.style.display = 'block';
            legendCloseButton.style.display = 'block';
            legendOpenButton.onclick = function() {
                legend.style.display = 'block';
                legendOpenButton.style.display = 'none';
            }
            legendCloseButton.onclick = function() {
                legend.style.display = 'none';
                legendOpenButton.style.display = 'block';
            }
        }
    }
    

    google.maps.event.addDomListener(window, 'load', initialize);

</script>
<div id="googft-mapCanvas"></div>
</body>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您正在寻找&#34;侧边栏&#34;。 FustionTablesLayer不允许直接访问FusionTable数据,但您可以使用visualization API查询表格并在侧栏中显示信息,然后将该信息添加到地图中。

来自http://www.geocodezip.com/v3_FusionTables_AfricaMap_kml_sidebar.html

的示例

screenshot of map

代码段

&#13;
&#13;
google.load('visualization', '1', {
  'packages': ['corechart', 'table', 'geomap']
});

function createSidebar() {
  //set the query using the parameter
  var queryText = encodeURIComponent("https://www.google.com/fusiontables/gvizdata?tq=SELECT * FROM 564705");
  var query = new google.visualization.Query(queryText);
  queryText = encodeURIComponent("SELECT 'description', 'name', 'Citizens', 'Country', 'Latitude', 'Longitude' FROM 564705");
  query = new google.visualization.Query('https://www.google.com/fusiontables/gvizdata?tq=' + queryText);

  //set the callback function
  query.send(getData);

}

// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(createSidebar);

var FTresponse = null;
//define callback function, this is called when the results are returned
function getData(response) {
  if (!response) {
    alert('no response');
    return;
  }
  if (response.isError()) {
    alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
    return;
  }
  FTresponse = response;
  //for more information on the response object, see the documentation
  //http://code.google.com/apis/visualization/documentation/reference.html#QueryResponse
  numRows = response.getDataTable().getNumberOfRows();
  numCols = response.getDataTable().getNumberOfColumns();

  //concatenate the results into a string, you can build a table here
  fusiontabledata = "<table><tr>";
  fusiontabledata += "<th>" + response.getDataTable().getColumnLabel(1) + "</th>";
  fusiontabledata += "</tr><tr>";

  for (i = 0; i < numRows; i++) {
    fusiontabledata += "<td><a href='javascript:myFTclick(" + i + ")'>" + response.getDataTable().getValue(i, 1) + "</a></td>";
    fusiontabledata += "</tr><tr>";
  }
  fusiontabledata += "</table>"
  //display the results on the page
  document.getElementById('sidebar').innerHTML = fusiontabledata;
}

function myFTclick(row) {
  var description = FTresponse.getDataTable().getValue(row, 0);
  var name = FTresponse.getDataTable().getValue(row, 1);
  var lat = FTresponse.getDataTable().getValue(row, 4);
  var lng = FTresponse.getDataTable().getValue(row, 5);
  var position = new google.maps.LatLng(lat, lng);
  // Set up and create the infowindow
  if (!infoWindow) infoWindow = new google.maps.InfoWindow({});
  infoWindow.setOptions({
    content: '<div class="FT_infowindow"><h3>' + name +
      '</h3><div>' + description + '</div></div>',
    pixelOffset: new google.maps.Size(0, 2),
    position: position
  });
  infoWindow.open(map);
}

var map = null;
var infoWindow = null;

function initialize() {

  var africa = new google.maps.LatLng(1.56, 16.07);

  map = new google.maps.Map(document.getElementById('map_canvas'), {
    center: africa,
    zoom: 3,
    mapTypeId: 'hybrid'
  });

  layer = new google.maps.FusionTablesLayer(564705, {
    suppressInfoWindows: true
  });
  layer.setMap(map);

  google.maps.event.addListener(layer, "click", function(event) {
    infoWindow.close();
    infoWindow.setContent(event.infoWindowHtml);
    infoWindow.setPosition(event.latLng);
    infoWindow.open(map);
  });

  infoWindow = new google.maps.InfoWindow();
}
google.maps.event.addDomListener(window, 'load', initialize);
&#13;
html,
body,
#map_canvas {
  width: 100%;
  height: 100%;
  margin: 0px;
  padding: 0px;
  background-color: white;
}

.infowindow * {
  font-size: 90%;
  margin: 0
}
&#13;
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="https://maps.google.com/maps/api/js"></script>
<table style="width:100%;height:100%">
  <tr style="width:100%;height:100%">
    <td style="width:100%;height:100%">
      <div id="map_canvas"></div>
    </td>
    <td>
      <div id="sidebar" style="width:260px;height:100%; overflow:auto"></div>
    </td>
  </tr>
</table>
&#13;
&#13;
&#13;