Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
154 views
in Technique[技术] by (71.8m points)

javascript - Google Maps stops loading when the code is moved to my JS file from script tags in index.html

My map worked fine when I had it in the script tags on my index page, however when I tried to move it to my js file so that I could use a variable in it, nothing worked.

Any help is appreciated, thanks

HTML

<div id="map"></div>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyD9utzeckd8pV-IwFik5jVgTtG84QhDjfI&callback=" type="text/javascript"></script>
<script type="text/javascript" src="js/index.js"></script>

JavaScript

       var im = 'http://i.imgur.com/aCONaAI.png';


    function initMap(position) {
        var map = new google.maps.Map(document.getElementById('map'),
            mapOptions);
        var myLatLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
        var mapOptions = {
            zoom: 16,
            center: {
                lat: 43.4678683,
                lng: -79.7006069
            },
            mapTypeId: google.maps.MapTypeId.SATELLITE,

        }


        var userMarker = new google.maps.Marker({
            position: myLatLng,
            map: map,
            icon: im,
            scaledSize: new google.maps.Size(50,50)
        });
        var stageIcon = {
            url: 'http://s31.postimg.org/i1fox68uz/bathroomicon.png',
            scaledSize: new google.maps.Size(50,50)
        } 
        var stage1 = new google.maps.Marker({
            position: { lat: 43.469222,
                       lng: -79.698804},
            map: map,
            title: 'Stage 1',
            icon: stageIcon

        });
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When you use "defer" you need to use it on all the scripts that are dependent on each other. If you defer loading the Google Maps Javascript API, you need to defer the loading of the scripts that are dependent on it.

working example (your code)

HTML:

<div id="map"></div>
<script async defer type="text/javascript" src="scripts/SO_20160419.js"></script>
<script async defer type="text/javascript" src="https://maps.googleapis.com/maps/api/js?callback=initMap" type="text/javascript"></script>

CSS:

html, body, #map {
  height: 100%;
  width: 100%;
}

Javascript (contents of scripts/SO_20160419.js):

var im = 'http://maps.google.com/mapfiles/ms/micons/blue.png';
var default_position = {coords: {latitude:42, longitude: -72}};
function initMap(position) {
    if (!position) position = default_position;
    var myLatLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
    var mapOptions = {
        zoom: 16,
        center: {
            lat: 43.4678683,
            lng: -79.7006069
        },
        mapTypeId: google.maps.MapTypeId.SATELLITE,
    }
    var map = new google.maps.Map(document.getElementById('map'),
        mapOptions);
    var userMarker = new google.maps.Marker({
        position: myLatLng,
        map: map,
        icon: im,
        scaledSize: new google.maps.Size(50,50)
    });
    var stageIcon = {
        url: 'http://maps.google.com/mapfiles/ms/micons/green.png',
        scaledSize: new google.maps.Size(50,50)
    } 
    var stage1 = new google.maps.Marker({
        position: { lat: 43.469222,
                   lng: -79.698804},
        map: map,
        title: 'Stage 1',
        icon: stageIcon
    });
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...