2010-06-28 95 views
0

我想通過Google地圖API動態獲取客戶位置。通過Google地圖API獲取客戶位置

下面是我的代碼:

/*Call external api dynamically*/ 
var script = document.createElement("script"); 
    script.src = "http://www.google.com/jsapi?key=" + api_key + "&callback=loadm"; 
    script.type = "text/javascript"; 
    document.getElementsByTagName("head")[0].appendChild(script); 

/*function */ 
function loadm() { 
google.load("maps", "3", {other_params:"sensor=false", "callback" : defmap}); 
} 

function defmap() { 

if(google.loader.ClientLocation){ 
    alert(google.loader.ClientLocation.latitude+" "+google.loader.ClientLocation.longitude}; 
} 

我已經嘗試過了,但在返回空值。代碼中有錯誤嗎?

回答

2

在我看來,您的alert代碼行中出現了一些語法錯誤。 嘗試關閉所有括號,這條線的工作對我來說:

if (google.loader.ClientLocation) { 
    alert(google.loader.ClientLocation.latitude+" "+google.loader.ClientLocation.longitude); 
}; 
+0

喜坎德人,對不起,我已經離開了右括號即時通訊在我的問題的編碼部分。但它存在於應用程序中。應用程序運行時沒有語法錯誤。但無法獲得客戶位置。它返回空值。 – davidlee 2010-06-28 15:15:27

+0

好吧,我把它放在一個.html文件中的確切代碼,放在標籤中,給了我我的(大約)位置......使用Firefox和Firefox在Linux上進行了測試 – kender 2010-06-29 12:08:39

相關問題