简体   繁体   中英

Load google maps API when internet is available via Ext.data.connection Sencha Touch and Phonegap

I use Sencha Touch and Phonegap to build a small app.
In this app I need the google maps API as I want to display a map.

There is a problem if the device has no internet connection, so the google maps API couldn't be loaded in the html. So I check for the internet connection.
If the internet connection is available I try to load the google maps API with Ext.data.connection:

var conn = new Ext.data.Connection();
var url = 'http://maps.google.com/maps/api/js?sensor=true&'; 
conn.request({  
     url: url,
     callback: function(options, success, response){
           if(response && response.responseText){
                // do something
           }
     },
     method:'GET'}
);

If I test the app in google chrome I receive following error: XMLHttpRequest cannot load http://maps.google.com/maps/api/js?sensor=true&amp ;_dc=1302077479281. Origin null is not allowed by Access-Control-Allow-Origin. XMLHttpRequest cannot load http://maps.google.com/maps/api/js?sensor=true&amp ;_dc=1302077479281. Origin null is not allowed by Access-Control-Allow-Origin.

It doesn't matter if I try to invoke the HTML via file:/// or via a webserver ( http://localhost/app )

How can I get the google maps API and "bypass" or avoid this Access-Control-behaviour?

Hering, sorry, this won't be an easy answer.

I think you want to use a conditional script loader. These are api's that will add the script to the document only if a condition (you have a network connection) is met.

A good one is http://yepnopejs.com/

According to this thread, it is possible to load google maps, but you must do as it says in the thread. Google Maps with YepNope

Specifically, include yepnope.js and remove any references to google maps.

yepnope([{
        load: 'http://www.google.com/jsapi',
        callback: function(){
            google.load("maps", "3", {
                callback: function(){
                    console.log("loaded");
                },
                other_params: "sensor=false"
            });
        }
    }]);

When it is time to render the map (which must be done after Google Maps is fully loaded) make sure you add the Ext.Map card, then call:

this.doLayout();

I think this'll work great for you, it's working for me...

I found some instructions from google, so the following will work:

var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://maps.google.com/maps/api/js?sensor=true&callback=initializeMap";
document.body.appendChild(script);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM