Google Map API使用中出现的“google未定义”解决办法

在网站中使用Google Map API V3版本,按照网站https://developers.google.com/maps/documentation/javascript/tutorial?hl=zh-cn中的例子,代码如下:

<!DOCTYPE html>
<html>
<head>
    <title>GoogleMap</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
      html { height: 100% }
      body { height: 100%; margin: 0px; padding: 0px }
      #map_canvas { height: 100% }
    </style>
    <script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=false"></script>

    <script type="text/javascript">
        function initialize() {
            var latlng = new google.maps.LatLng(-34.397, 150.644);
            var myOptions = {
                zoom: 8,
                center: latlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            var map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
        }
    </script>
</head>
<body οnlοad="initialize()">
  <div id="map_canvas" style="width:100%; height:100%"></div>                 
</body>
</html>

运行时,总是会出现“google未定义”的错误提示,在网上搜索了很久,大都是说这是google本身的问题,过段时间可能就好了。但是还是不甘心,继续寻找解决方案,终于,柳暗花明。

解决方法很简单,只需要将载入Google Map API的那句代码中的" https ”协议改为" http ”即可。

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

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

真是好无语的答案。

不过,深思一下,为什么会因为一个协议的问题而出现"google 未定义"的错误呢?

个人猜想,可能是因为https协议进行了加密保护,限制了我们的访问权限。但看到网上也有说直接用https协议可以成功运行的,其中缘由就不得而知了。

这个问题还有待进一步的研究。

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值