2014-02-21 125 views
1

使用Firefox的插件-SDK我下一個有關創建可重用模塊,該示例使用geolocation API built into Firefox教程,所以代碼很簡單:無法導入nsIDOMGeoGeolocation XPCOM接口

function getCurrentPosition(callback){ 
    var xpcomGeolocation = Cc["@mozilla.org/geolocation;1"].getService(Ci.nsIDOMGeoGeolocation); 
xpcomGeolocation.getCurrentPosition(callback); 
} 

var widget = require("sdk/widget").Widget({ 
    id: "whereami", 
    label: "Where Am I?", 
    contentURL: "http://www.mozilla.org/favicon.ico", 
    onClick: function(){ 
     console.log("clicked!");   
     getCurrentPosition(function(position){ 
      console.log("latitude: " + position.coords.latitude); 
      console.log("longitude: " + position.coords.longitude); 
     }); 
    } 
}); 

運行時火狐插件上,單擊控件給這個錯誤:

Message: [Exception... "Component returned failure code: 0x80570018 (NS_ERROR_XPC_BAD_IID) [nsIJSCID.getService]" nsresult: "0x80570018 (NS_ERROR_XPC_BAD_IID)" location: "JS frame :: resource://gre/modules/XPIProvider.jsm -> jar:file:///tmp/tmpTFowYc.mozrunner/extensions/[email protected]!/bootstrap.js -> resource://gre/modules/commonjs/toolkit/loader.js -> resource://jid1-libifbk6zvwaiq-at-jetpack/whereami/lib/main.js :: getCurrentPosition :: line 7" data: no]

回答

1

According to docs,有可能得到一個錯誤導入nsIDGeoGeolocation時,所以你必須使用改爲。此外,您必須要求獲取地理位置的權限,請參閱部分提示中的 Using geolocation 參考。

順便說一句,作爲一個建議,我認爲使用navigator.geolocation.getCurrentPosition(successCallback, errorCallback)會更簡單,因爲它會處理提示您的權限,但我不知道您在此之外要做什麼。只是稍微解釋一下,你可以通過插件頁面data/index.html告訴用戶你要提示他的地理位置。除此之外,您必須擁有一個PageMod,該頁面運行data/script.js,該頁面可訪問navigator.geolocation。該內容腳本可能會與您的lib/main.js文件進行通信,因此它可以訪問用戶地理位置。有些事情是這樣的:

data/index.html

<html> 
    <body> 
    <h1>Hello user!</h1> 
    </body> 
</html> 

data/script.js

var successCallback = function(position) { 
    /* this way this script will talk to the pagemod in lib/main.js the user's position */ 
    self.port.emit("gotGeolocation", position.coords.latitude, position.coords.longitude); 
}; 
navigator.geolocation.getCurrentPosition(successCallback, errorCallback); 

lib/main.js

var data = require("sdk/self").data; 
var pageMod = require("sdk/page-mod"); 
var tabs = require("sdk/tabs"); 

pageMod.PageMod({ 
    /* attach the contentScriptFile to this html page */ 
    include: data.url("index.html"), 
    contentScriptFile: data.url("script.js"), 
    onAttach: function(worker) { 
    /* listen to the script.js worker "gotGeolocation" message */ 
    worker.port.on("gotGeolocation", function(latitude, longitude) { 
     console.log("latitude:", latitude); 
     console.log("longitude:", longitude); 
    }); 
    } 
}); 

/* this will open the index.jtml page and promt the user to access his geo position */ 
tabs.open(data.url("index.html")); 

所有這些代碼,這只是爲了更好地描述這個想法,現在也沒有經過測試,實際上有一些不明確的對象。但是我希望給你一個關於如何訪問navigator.geolocation並將它的座標傳遞給main.js腳本的想法。

+0

第三方模塊的SDK不提供API與navigator.geolocation訪問地理位置。我嘗試使用nsISupports併發生另一個錯誤。我想要做的就是完成教程。 – elaich

+0

我編輯了我以前的答案,以解釋如何訪問導航器對象。希望能幫助到你。 – matagus

+0

謝謝我很肯定,在內容頁面中注入腳本會使用'navigator'工作,但我想訪問chrome服務,並且不知道爲什麼我不能,以及如果我做錯了。 – elaich