融合表地图中图标附近的标签

label near icon in fusion tables map

我创建了一张地图,显示特定区域的一些餐馆。我使用融合表作为数据输入和生成的代码来显示这张地图:

here is the link

我尝试在地标(图标)附近添加带有餐厅名称的文本标签,但没有成功。 关于我该怎么做的任何想法? 我在论坛里找了很多,都没有找到解决方法。

非常感谢

JT

(这里是代码)

  <!DOCTYPE html>
    <html>
    <head>
    <meta name="viewport"></meta>
    <title>Locali in zona Fiume e Quarnero - Google Fusion Tables</title>
    <style type="text/css">
    html, body, #googft-mapCanvas { height:100%; margin: 0; padding: 0;}
    </style>

    <script type="text/javascript" src="https://maps.google.com/maps/api/js?     sensor=false&amp;v=3"></script>

    <script type="text/javascript">
      function initialize() {
        google.maps.visualRefresh = true;
        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 ?  '500px':'100%' ;
    mapDiv.style.height = isMobile ? '300px':'100%' ;
    var map = new google.maps.Map(mapDiv, {
      center: new google.maps.LatLng(45.19975321105807, 14.824613028515614),
      zoom: 10,
      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: "col6",
        from: "1e8PEAUCopFkL9XqgVp1gJAv6Hh6ttGkMViOGhSZx",
        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>
</head>

<body>
  <div id="googft-mapCanvas"></div>
</body>
</html>

一个选项(对于少于几百行的 tables)是使用 GViz 查询 table 数据并使用它来向地图添加标签。

proof of concept fiddle

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

var tableid ="1e8PEAUCopFkL9XqgVp1gJAv6Hh6ttGkMViOGhSZx"
var map;
var labels = [];
function displayLabels() {
  //set the query using the current bounds
  var queryStr = "SELECT Posizione, Nome FROM "+ tableid;   
  var queryText = encodeURIComponent(queryStr);
  var query = new google.visualization.Query('http://www.google.com/fusiontables/gvizdata?tq='  + queryText);
  // alert(queryStr);

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

}
function displayLabelText(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();
  // alert(numRows);
  // var bounds = new google.maps.LatLngBounds();
  for(i = 0; i < numRows; i++) {
      var label = response.getDataTable().getValue(i, 1);
      var labelStr = label.toString()
      var positionStr = response.getDataTable().getValue(i, 0);
      var position = positionStr.split(',');
      var point = new google.maps.LatLng(
          parseFloat(position[0]),
          parseFloat(position[1]));
      // bounds.extend(point);
      labels.push(new InfoBox({
     content: labelStr
    ,boxStyle: {
       border: "1px solid black"
      ,textAlign: "center"
          ,backgroundColor:"white"
      ,fontSize: "8pt"
      ,width: "50px"
     }
    ,disableAutoPan: true
    ,pixelOffset: new google.maps.Size(-25, 0)
    ,position: point
    ,closeBoxURL: ""
    ,isHidden: false
    ,enableEventPropagation: true
      }));
      labels[labels.length-1].open(map);
  }
  // zoom to the bounds
  // map.fitBounds(bounds);
}
google.setOnLoadCallback(displayLabels);

代码片段:

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

var tableid = "1e8PEAUCopFkL9XqgVp1gJAv6Hh6ttGkMViOGhSZx"
var map;
var labels = [];

function displayLabels() {
  //set the query using the current bounds
  var queryStr = "SELECT Posizione, Nome FROM " + tableid;
  var queryText = encodeURIComponent(queryStr);
  var query = new google.visualization.Query('http://www.google.com/fusiontables/gvizdata?tq=' + queryText);
  // alert(queryStr);

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

}

function displayLabelText(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();
  // alert(numRows);
  // var bounds = new google.maps.LatLngBounds();
  for (i = 0; i < numRows; i++) {
    var label = response.getDataTable().getValue(i, 1);
    var labelStr = label.toString()
    var positionStr = response.getDataTable().getValue(i, 0);
    var position = positionStr.split(',');
    var point = new google.maps.LatLng(
      parseFloat(position[0]),
      parseFloat(position[1]));
    // bounds.extend(point);
    labels.push(new InfoBox({
      content: labelStr,
      boxStyle: {
        border: "1px solid black",
        textAlign: "center",
        backgroundColor: "white",
        fontSize: "8pt",
        width: "50px"
      },
      disableAutoPan: true,
      pixelOffset: new google.maps.Size(-25, 0),
      position: point,
      closeBoxURL: "",
      isHidden: false,
      enableEventPropagation: true
    }));
    labels[labels.length - 1].open(map);
  }
  // zoom to the bounds
  // map.fitBounds(bounds);
}
google.setOnLoadCallback(displayLabels);

function initialize() {
  google.maps.visualRefresh = true;
  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 ? '500px' : '100%';
  mapDiv.style.height = isMobile ? '300px' : '100%';
  map = new google.maps.Map(mapDiv, {
    center: new google.maps.LatLng(45.19975321105807, 14.824613028515614),
    zoom: 10,
    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: "col6",
      from: "1e8PEAUCopFkL9XqgVp1gJAv6Hh6ttGkMViOGhSZx",
      where: ""
    },
    options: {
      styleId: 2,
      templateId: 2
    }

  });
  google.maps.event.addListenerOnce(map, 'bounds_changed', displayLabels);

  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);
html,
body,
#googft-mapCanvas {
  height: 100%;
  margin: 0;
  padding: 0;
}
<script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=geometry,places&ext=.js"></script>
<script src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobox/src/infobox.js"></script>
<script src="http://www.google.com/jsapi"></script>
<div id="googft-mapCanvas"></div>