0

I´m working a dynamic way to map certain locations, I´m basing my code on an example by Googlemaps, in this example they map locations statically, using an external file (week) where you write the call to the function, an online initialization and mapping functions, like this:

function initialize() {
    map = new google.maps.Map(document.getElementById('map_canvas'), {
      zoom: 16,
      center: new google.maps.LatLng(19.43,-99.15),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
    var script = document.createElement('script');
    script.src = 'week';
    document.getElementsByTagName('head')[0].appendChild(script);
    }

function eqfeed_callback(results){
    mapping code
}

the content of week, the external file, is:

eqfeed_callback({"type":"FeatureCollection","features":[{feature01, feature02,... feature_n}]});

I´m able to generate dinamically the features content of week (in fact, the whole content, with the very same structure), but now I need to pass it to the initialization function, now that is the value of a global variable instead of an external file´s content, what I´ve made is to rewrite initialize as a parameter dependant function, in order to make it wait for its parameter to be generated, like this:

function initialize(scriptSource){
    map = new google.maps.Map(document.getElementById('map_canvas'), { zoom: 16, center: new google.maps.LatLng(19.43,-99.15), mapTypeId:google.maps.MapTypeId.ROADMAP});
    var script = document.createElement('script');
    script.src = scriptSource;
    document.getElementsByTagName('head')[0].appendChild(script);
}

when initialize is called, scriptSource will be the value of a global variable, with its value being exactly the same as the content of the external file (but now generated dinamically) week; I´ve been trying to make it work, but I think there´s a problem with the way I´m passing the src, how do I do this correctly?

1 Answer 1

1

It appears you are attempt to load javascript into a script tag. Rather than setting the src member, instead set the innerHtml.

The src member is actually the url, not the content, of a script tag.

Also, be weary of other places you are setting the src.

script.src = 'week'; will not work as a uri

script.src = 'week.js'; will work as a uri

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.