Nothing I’ve tried seems to work.
I found these two links and thought they’d be helpful, but that hasn’t worked either.
Dynamically load JavaScript with JavaScript
https://developers.google.com/loader/
Here’s roughly what my Greasemonkey script looks like currently:
var init_map = function() {
new google.maps.Geocoder().geocode({address:'1125 NW 12th Ave, Portland, OR'},function(result){
alert(result);
});
}
function loadMaps() {
GM_log("loadMaps called");
google.load("maps", "3", {"callback" : init_map, "other_params":"key=foo&sensor=false"});
}
function loadScript(filename,callback){
var fileref=document.createElement('script');
fileref.setAttribute("type","text/javascript");
fileref.onload = callback;
fileref.setAttribute("src", filename);
if (typeof fileref!="undefined"){
document.getElementsByTagName("head")[0].appendChild(fileref);
}
}
$(document).ready(
function() {
GM_log("document ready");
loadScript('http://www.google.com/jsapi?key=ABQIAAAAfoo',function(){
loadMaps();
});
}
);
I’ve found that if I don’t include
// @require http://www.google.com/jsapi?key=ABQIAAAAfoo
in the Greasemonkey script, I get a google is undefined error. If I do include it, init_map() never gets called. Any suggestions?
I flagged this question as duplicate of how to use the google maps api with greasemonkey to read a table of addresses and trace the route? but the mod “found no evidence to support it”.
So i will just copy-paste what i did in my question, since its not a duplicate…
Nah, just kidding 🙂
Lets start with your last statement:
Yes.
First, the google maps API should not be loaded as a
@require. Instead, do it like thisSecond, add
google = unsafeWindow.google, otherwise you get the “google is undefined” error.So, your code should start like this
About the rest of your code… well, just click on the link above and there you will find how to create a DIV on the fly, add the map to it, append the DIV to the page in a fixed position, etc.
Feel free to copy whatever you want.
Greasemonkey scripts are free anyway 🙂