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

时间:2015-02-18 08:49:10

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

我创建了一张地图,显示了某个特定区域的餐馆。我使用融合表作为数据输入,并生成代码以显示此映射:

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>

1 个答案:

答案 0 :(得分:0)

一个选项(对于少于几百行的表)将使用GViz在表中查询数据,并使用该选项向地图添加标签。

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);

代码段:

&#13;
&#13;
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);
&#13;
html,
body,
#googft-mapCanvas {
  height: 100%;
  margin: 0;
  padding: 0;
}
&#13;
<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>
&#13;
&#13;
&#13;