【问题标题】:Google MAPs API / Javascript / Search box functionGoogle MAPs API / Javascript / 搜索框功能
【发布时间】:2016-10-09 22:59:29
【问题描述】:

我正在尝试通过调用从谷歌融合表中提取数据的谷歌地图 API 来嵌入搜索框功能。搜索框应搜索地图上的标记名称。但是当我在框中输入任何字符时,搜索失败。我不确定为什么。

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport"></meta>
    <title>Merge of Vino_Brew_BBQ and Markers - Google Fusion Tables</title>
    <style type="text/css">
        html, body, #googft-mapCanvas {
            height: 300px;
            margin: 0;
            padding: 0;
            width: 500px;
        }
    </style>

    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAI_3RMI43f45RgHC9MLdZbePINAqH_ZM0"></script>


    <script type="text/javascript">

    var map;
    var VinoBrewBBQLayer;
    var VinoBrewBBQTable = '15qpgfFdsMTpuwQ_gR2-LgBSlN6Z7EPhCegikJ0xy';
    var VinoBrewBBQColumn = 'Geometry';


    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 ? '100%' : '500px';
        mapDiv.style.height = isMobile ? '100%' : '300px';
        map = new google.maps.Map(mapDiv, {
            center: new google.maps.LatLng(31.340072697821267, -98.913833984375),
            zoom: 5,
            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'));

        VinoBrewBBQLayer = new google.maps.FusionTablesLayer({
            map: map,
            heatmap: { enabled: false },
            query: {
                select: VinoBrewBBQColumn,
                from: VinoBrewBBQTable,
                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';
            }
        }
    }

    function SearchLayer(VinoBrewBBQLayer, VinoBrewBBQTable, map){
        var store = document.getElementById('StoreTextInput').value;
        var search = "NAME CONTAINS IGNORING CASE '" + store + "'";

            if(!VinoBrewBBQLayer.getMap()){
                VinoBrewBBQLayer.setMap(map);
                alert (search);
            }
            VinoBrewBBQLayer.setOptions({
                query:  {
                    select: VinoBrewBBQColumn,
                    from: VinoBrewBBQTable,
                    where: search
                },
                options: {
                    styleId: 3,
                    templateId: 3
                }
            });
    }
    google.maps.event.addDomListener(window, 'load', initialize);
</script>

搜索

【问题讨论】:

  • “搜索失败”是什么意思?当我运行你的代码时,它并没有失败,它可以工作,但是所有的标记都会变成红色。

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


【解决方案1】:

您的search 字符串中有错字。 FusionTables 中的列名区分大小写。 “姓名”应该是“姓名”:

var search = "NAME CONTAINS IGNORING CASE '" + store + "'";

应该是:

var search = "Name CONTAINS IGNORING CASE '" + store + "'";

proof of concept fiddle

代码 sn-p:

var map;
var VinoBrewBBQLayer;
var VinoBrewBBQTable = '15qpgfFdsMTpuwQ_gR2-LgBSlN6Z7EPhCegikJ0xy';
var VinoBrewBBQColumn = 'Geometry';


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 ? '100%' : '500px';
  mapDiv.style.height = isMobile ? '100%' : '300px';
  map = new google.maps.Map(mapDiv, {
    center: new google.maps.LatLng(31.340072697821267, -98.913833984375),
    zoom: 5,
    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'));

  VinoBrewBBQLayer = new google.maps.FusionTablesLayer({
    map: map,
    heatmap: {
      enabled: false
    },
    query: {
      select: VinoBrewBBQColumn,
      from: VinoBrewBBQTable,
      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(document.getElementById('btn'), 'click', function(evt) {
    SearchLayer(VinoBrewBBQLayer, VinoBrewBBQTable, map);
  })
}

function SearchLayer(VinoBrewBBQLayer, VinoBrewBBQTable, map) {
  var store = document.getElementById('StoreTextInput').value;
  var search = "Name CONTAINS IGNORING CASE '" + store + "'";

  if (!VinoBrewBBQLayer.getMap()) {
    VinoBrewBBQLayer.setMap(map);
    alert(search);
  }
  console.log("select:" + VinoBrewBBQColumn + "\nfrom:" + VinoBrewBBQTable + "\nwhere:" + search);
  VinoBrewBBQLayer.setOptions({
    query: {
      select: VinoBrewBBQColumn,
      from: VinoBrewBBQTable,
      where: search
    },
    options: {
      styleId: 3,
      templateId: 3
    }
  });
}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#googft-mapCanvas {
  height: 300px;
  margin: 0;
  padding: 0;
  width: 500px;
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<input id="StoreTextInput" value="City Market" />
<input id="btn" value="search" type="button" />
<div id="googft-mapCanvas"></div>
<div id="googft-legend-open"></div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-08
    • 2013-06-28
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    • 1970-01-01
    • 2021-09-02
    • 1970-01-01
    相关资源
    最近更新 更多