我的Google标记未显示在Google地图中

时间:2015-08-21 00:03:28

标签: javascript jquery google-maps google-maps-api-3

嘿我正在尝试使用Google的API创建Google地图。输入正确的代码后,标记没有显示出来。有人可以帮我找出为什么没有出现?我做了一些研究,大部分问题都是由位置问题引起的。

<!DOCTYPE html>
<html>
<head>
<meta name="viewport"></meta>
<title>Eastern Missouri School District Map  - Google Fusion Tables</title>
<style type="text/css">
html, body, #googft-mapCanvas {
  height: 500px;
  margin: 0;
  padding: 0;
  width: 100%;
}
.controls {
  margin-top: 10px;
  border: 1px solid transparent;
  border-radius: 2px 0 0 2px;
  box-sizing: border-box;
  -moz-box-sizing: border-box;
  height: 32px;
  outline: none;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
}

#pac-input {
  background-color: #fff;
  font-family: Roboto;
  font-size: 15px;
  font-weight: 300;
  margin-left: 12px;
  padding: 0 11px 0 13px;
  text-overflow: ellipsis;
  width: 300px;
}

#pac-input:focus {
  border-color: #4d90fe;
}

.pac-container {
  font-family: Roboto;
}

#type-selector {
  color: #fff;
  background-color: #4d90fe;
  padding: 5px 11px 0px 11px;
}

#type-selector label {
  font-family: Roboto;
  font-size: 13px;
  font-weight: 300;
}
</style>

<input id="pac-input" class="controls" type="text"
placeholder="Enter your address here">
  <div id="googft-mapCanvas"></div>


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

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=places"></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 ? '100%' : '1000px';
    mapDiv.style.height = isMobile ? '100%' : '300px';
    var map = new google.maps.Map(mapDiv, {
      center: new google.maps.LatLng(38.64936217820852, -90.53628850000001),
      zoom: 9,
      mapTypeId: google.maps.MapTypeId.HYBRID
    });
    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: "col26",
        from: "11Q0B7iRayT2JIOBl8_VRUmitimhX1W01byuFDnAv",
        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);


  var searchBox = new google.maps.places.SearchBox(document.getElementById('pac-input'));


            var marker = new google.maps.Marker({
        position: LatLng(38.64936217820852, -90.53628850000001),
        map: map,
        draggable:true,
        title: "Your New Home",

    });
    marker.setMap(map);

</script> 
</head>

<body>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

您的代码中出现javascript错误:Uncaught ReferenceError: LatLng is not defined。这样:

position: LatLng(38.64936217820852, -90.53628850000001),

应该是:

position: google.maps.LatLng(38.64936217820852, -90.53628850000001),

其他评论:

  1. 您要包含两次API。不要这样做,它可能会导致问题,将所有需要的参数组合到一个调用中(v=3&ibraries=places,不再需要传感器参数)
  2. 您需要在初始化地图初始化的初始化函数中初始化您的标记
  3. 您需要初始化初始化函数中的SearchBox,其中初始化地图并且在窗口加载事件上运行,因此DOM已经呈现(document.getElementById('pac-input')无法找到该元素,直到它已经存在附加到DOM)。
  4. working fiddle

    代码段

    &#13;
    &#13;
    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%' : '1000px';
      mapDiv.style.height = isMobile ? '100%' : '300px';
      var map = new google.maps.Map(mapDiv, {
        center: new google.maps.LatLng(38.64936217820852, -90.53628850000001),
        zoom: 9,
        mapTypeId: google.maps.MapTypeId.HYBRID
      });
      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: "col26",
          from: "11Q0B7iRayT2JIOBl8_VRUmitimhX1W01byuFDnAv",
          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';
        }
      }
      var searchBox = new google.maps.places.SearchBox(document.getElementById('pac-input'));
    
    
      var marker = new google.maps.Marker({
        position: new google.maps.LatLng(38.64936217820852, -90.53628850000001),
        map: map,
        draggable: true,
        title: "Your New Home",
    
      });
      marker.setMap(map);
    }
    
    
    google.maps.event.addDomListener(window, 'load', initialize);
    &#13;
    html,
    body,
    #googft-mapCanvas {
      height: 500px;
      margin: 0;
      padding: 0;
      width: 100%;
    }
    .controls {
      margin-top: 10px;
      border: 1px solid transparent;
      border-radius: 2px 0 0 2px;
      box-sizing: border-box;
      -moz-box-sizing: border-box;
      height: 32px;
      outline: none;
      box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
    }
    #pac-input {
      background-color: #fff;
      font-family: Roboto;
      font-size: 15px;
      font-weight: 300;
      margin-left: 12px;
      padding: 0 11px 0 13px;
      text-overflow: ellipsis;
      width: 300px;
    }
    #pac-input:focus {
      border-color: #4d90fe;
    }
    .pac-container {
      font-family: Roboto;
    }
    #type-selector {
      color: #fff;
      background-color: #4d90fe;
      padding: 5px 11px 0px 11px;
    }
    #type-selector label {
      font-family: Roboto;
      font-size: 13px;
      font-weight: 300;
    }
    &#13;
    <script src="https://maps.googleapis.com/maps/api/js?libraries=geometry,places"></script>
    <input id="pac-input" class="controls" type="text" placeholder="Enter your address here">
    <div id="googft-mapCanvas"></div>
    &#13;
    &#13;
    &#13;