如何使用GreaseMonkey在网页中插入Google地图? [英] How to insert a Google map in a webpage using GreaseMonkey?

查看:98
本文介绍了如何使用GreaseMonkey在网页中插入Google地图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过的东西似乎没有用。
我发现这两个链接,并认为他们会有所帮助,但这也没有奏效。
用JavaScript动态加载JavaScript
https://developers.google.com/loader/



以下是我的Greasemonkey脚本目前的样子:

  var init_map = function(){
new google.maps .Geocoder()。geocode({address:'1125 NW 12th Ave,Portland,OR'},function(result){
alert(result);
});


函数loadMaps(){
GM_log(loadMaps called);
google.load(maps,3,{callback:init_map,other_params:key = foo& sensor = false});


函数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();
});
}
);

我发现如果我不包含
// @require http://www.google.com/jsapi?key=ABQIAAAAfoo
in Greasemonkey脚本,我得到一个谷歌是未定义的错误。如果我包含它,init_map()永远不会被调用。任何建议?

解决方案

我将此问题标记为如何使用谷歌地图API与greasemonkey来读取地址表并追踪路线?,但mod发现没有证据支持它。

所以我只是复制粘贴我在我的问题中所做的,因为它不是重复的...

Nah,只是在开玩笑:)

让我们从你最后的声明开始:


我发现如果我不包含// @require
http://www.google.com/jsapi?key=ABQIAAAAfoo 在Greasemonkey
脚本中,我得到一个google是未定义的错误。如果我包含它,
init_map()永远不会被调用。有什么建议吗?

是的。

首先,google maps API不应该被加载为 @require 。相反,像这样做

  API_js_callback =http://maps.google.com/maps/api/js?sensor =假安培;区域= BR&安培;回调=初始化; 

var script = document.createElement('script');
script.src = API_js_callback;
var head = document.getElementsByTagName(head)[0];
(head || document.body).appendChild(script);

其次,添加 google = unsafeWindow.google ,否则你会得到google is undefined错误。

所以,你的代码应该像这样开始

  var init_map = function(){
google = unsafeWindow.google
google.maps.Geocoder()。geocode。 。 。 。 。 。

关于您的其他代码......只需点击上面的链接,找到如何快速创建DIV,将地图添加到它,将DIV追加到页面的固定位置等。



随时随地复制您的任何内容想。

Greasemonkey脚本无论如何都是免费的:)

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:

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?

Yes.
First, the google maps API should not be loaded as a @require. Instead, do it like this

API_js_callback = "http://maps.google.com/maps/api/js?sensor=false&region=BR&callback=initialize";

var script = document.createElement('script');
    script.src = API_js_callback;
var head = document.getElementsByTagName("head")[0];
    (head || document.body).appendChild(script);

Second, add google = unsafeWindow.google, otherwise you get the "google is undefined" error.
So, your code should start like this

var init_map = function() {
    google = unsafeWindow.google
    new google.maps.Geocoder().geocode . . . . . .

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 :)

这篇关于如何使用GreaseMonkey在网页中插入Google地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆