【问题标题】:Google Maps not showing on webpage but shows on localhost谷歌地图未显示在网页上,但显示在本地主机上
【发布时间】:2015-08-10 04:27:25
【问题描述】:

所以我试图在网站上实现谷歌地图,在我的本地主机上它工作正常,我看得很完美。 但是,当我将其上传到我的托管公司时,地图根本不显示。 当我检查元素并检查控制台时,我没有看到任何错误。 代码如下:

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=MYAPIKEY"></script>
<script>
  function initialize() {
    var mapCanvas = document.getElementById('map-canvas');
    var myLatlng = new google.maps.LatLng(53.092975, -7.895479);

    var mapOptions = {
      center: myLatlng,
      zoom:16,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }

    var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

    var marker = new google.maps.Marker({
      position: myLatlng,
      map: map,
      title: 'LCCEurotex'
    });

  }
  google.maps.event.addDomListener(window, 'load', initialize);
</script>

我不知道是什么原因造成的,因为我的代码看起来与 google 文档中的相同。 关于可能导致它的任何想法都会很棒。 感谢您的帮助!

【问题讨论】:

  • 你有链接吗?我认为这是在加载函数之前调用函数的问题。
  • 你能得到chrome devtool中网络面板的截图吗?
  • 您是否设置了正确的 API 密钥?这些坐标的地址是什么?
  • 尝试 (
  • @howderek 嘿,链接是 www.lcceurotex.com

标签: javascript html css web-services web


【解决方案1】:

将这部分代码移动到页脚页面,可能您已经绑定了地图并且 div 容器未加载属性,此外,托盘将高度添加到 id map_canvas 作为您的 css 中的默认值。

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

    <script>
      function initialize() {
        var mapCanvas = document.getElementById('map-canvas');
        var myLatlng = new google.maps.LatLng(53.092975, -7.895479);

        var mapOptions = {
          center: myLatlng,
          zoom:16,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        }

        var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

        var marker = new google.maps.Marker({
          position: myLatlng,
          map: map,
          title: 'LCCEurotex'
        });


      }
      google.maps.event.addDomListener(window, 'load', initialize);
    </script>

在你的 CSS 中

div#map-canvas {
  height: 500px !important;
}

【讨论】:

    【解决方案2】:

    google maps api 的 url 只添加 http://,改变这一行:

    <script type="text/javascript" src="maps.google.com/maps/api/js?sensor=false"></script>
    

    通过这个:

    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script> 
    

    【讨论】:

    • 我添加了
    • 如果您查看您的网络面板,发现此库未找到,因为它已在您的服务器本地加载,放置 http:// 或 https:// 应该可以解决您的问题
    • 嘿抱歉,我现在才注意到它格式化了我发布的地址。我用了http,它没有用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-25
    • 2021-07-07
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多