ReferenceError:google未定义,仅限(mozilla)

时间:2016-07-04 06:46:09

标签: javascript google-maps-api-3

我尝试使用谷歌地图API和自定义标记,在Chrome工作正常,但其他浏览器我有错误。

第一个错误:

ReferenceError: google is not defined

第二个错误:

InvalidValueError: mapInitialize is not a function

JSfiddle示例:example

在测试网站上:testsite

我做错了什么?请帮忙!

3 个答案:

答案 0 :(得分:2)

<强>被修改

mapInitialize功能

的末尾添加此代码
var mapCenter = new google.maps.LatLng(56.946528, 24.118665);
map.setCenter(mapCenter);

Image result

答案 1 :(得分:1)

您未正确初始化地图:

&#13;
&#13;
function loadScript(src, callback) {
    var script = document.createElement("script");
    script.type = "text/javascript";
    if(callback)script.onload=callback;
    document.getElementsByTagName("head")[0].appendChild(script);
    script.src = src;
}

loadScript('http://maps.googleapis.com/maps/api/js?v=3&sensor=false&callback=initialize',
    function() {
        log('google-loader has been loaded, but not the maps-API ');
    });

function initialize() {
    log('maps-API has been loaded, ready to use');
    var mapOptions = {
          zoom: 8,
          center: new google.maps.LatLng(-34.397, 150.644),
          mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById('map_canvas'),
            mapOptions);
}

function log(str) {
    document.getElementsByTagName('pre')[0].appendChild(document.createTextNode('['+new Date().getTime()+']\n'+str+'\n\n'));
}
&#13;
<pre><div id="map_canvas" style="height:200px"></div><pre>
&#13;
&#13;
&#13;

答案 2 :(得分:-1)

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap" async defer></script>

猜猜你没有加载JavaScript。