Loading Google Maps API V3 asynchronously with doj

2019-07-21 21:51发布

问题:

Smushing Google's asynchronous loading docs into dojo's asynchronous loader yields:

dojo.io.script.get({
  url: 'http://maps.googleapis.com/maps/api/js',
  jsonp: 'callback',
  content: {
    sensor: 'false'
  },
  load: function() {
    console.log('done');
  },
  error: function() {
    console.log('error');
  }
});

Or fiddle with it: http://jsfiddle.net/sKNmS/

The maps JS files are loaded, but the callback is never called. Why?

回答1:

dojo.io.script.get is only usable for JSONP services. Your callback is never called because, as far as I can tell, that URL does not return a JSONP-formatted response, just plain JavaScript.

dojo.io.script.get sets up a callback, which is expected to be fired by the code returned from that URL. Since that URL points to plain ol' JavaScript, the function never gets fired, because the response isn't JSONP.



回答2:

Looks like dojo.io.script.get() doesn't support Google Maps' delayed invocation of the JSONP callback.

I've filled a dojo feature request.



回答3:

There is another way (tested on dojo 1.6):

dojo.io.script.get({
  url: 'http://maps.googleapis.com/maps/api/js'
}).then(function() {
  console.log('done');
});

Since dojo.io.script.get returns Deferred object.