2013-06-26 26 views
0

我無法獲得此Android版本的HTML5地理定位應用程序運行。該應用程序在我的桌面上的Firefox瀏覽器中運行良好,但是一旦我使用Eclipse進行編譯 - PhoneGap,地理位置按鈕不會執行命令document.write,我已經嘗試使用alert()以及console.log(),但沒有積極的結果。用於Android的HTML5地理定位應用程序,使用phonegap eclipse

代碼如下,如果我缺少設置地理位置的任何手機傳感器,請指點我嗎?

Below is the code: 

<html> 
<head> 
<meta charset="UTF-8"> 

<title> Geolocation Test </title> 
<script src="js/jquery-1.9.1.min.js"></script> 
<script> // Begginning of Google Maps script 

window.google = window.google || {}; 
google.maps = google.maps || {}; 
(function() { 

    function getScript(src) { 
    document.write('<' + 'script src="' + src + '"' + 
        ' type="text/javascript"><' + '/script>'); 
    } 

    var modules = google.maps.modules = {}; 
    google.maps.__gjsload__ = function(name, text) { 
    modules[name] = text; 
    }; 

    google.maps.Load = function(apiLoad) { 
    delete google.maps.Load; 
    apiLoad([0.009999999776482582,[[["http://mt0.googleapis.com/[email protected]\u0026src=api\u0026hl=en-US\u0026","http://mt1.googleapis.com/[email protected]\u0026src=api\u0026hl=en-US\u0026"],null,null,null,null,"[email protected]"],[["http://khm0.googleapis.com/kh?v=131\u0026hl=en-US\u0026","http://khm1.googleapis.com/kh?v=131\u0026hl=en-US\u0026"],null,null,null,1,"131"],[["http://mt0.googleapis.com/[email protected]\u0026src=api\u0026hl=en-US\u0026","http://mt1.googleapis.com/[email protected]\u0026src=api\u0026hl=en-US\u0026"],null,null,"imgtp=png32\u0026",null,"[email protected]"],[["http://mt0.googleapis.com/[email protected],[email protected]\u0026src=api\u0026hl=en-US\u0026","http://mt1.googleapis.com/[email protected],[email protected]\u0026src=api\u0026hl=en-US\u0026"],null,null,null,null,"[email protected],[email protected]"],null,null,[["http://cbk0.googleapis.com/cbk?","http://cbk1.googleapis.com/cbk?"]],[["http://khm0.googleapis.com/kh?v=77\u0026hl=en-US\u0026","http://khm1.googleapis.com/kh?v=77\u0026hl=en-US\u0026"],null,null,null,null,"77"],[["http://mt0.googleapis.com/mapslt?hl=en-US\u0026","http://mt1.googleapis.com/mapslt?hl=en-US\u0026"]],[["http://mt0.googleapis.com/mapslt/ft?hl=en-US\u0026","http://mt1.googleapis.com/mapslt/ft?hl=en-US\u0026"]],[["http://mt0.googleapis.com/vt?hl=en-US\u0026","http://mt1.googleapis.com/vt?hl=en-US\u0026"]],[["http://mt0.googleapis.com/mapslt/loom?hl=en-US\u0026","http://mt1.googleapis.com/mapslt/loom?hl=en-US\u0026"]],[["https://mts0.googleapis.com/mapslt?hl=en-US\u0026","https://mts1.googleapis.com/mapslt?hl=en-US\u0026"]],[["https://mts0.googleapis.com/mapslt/ft?hl=en-US\u0026","https://mts1.googleapis.com/mapslt/ft?hl=en-US\u0026"]]],["en-US","US",null,0,null,null,"http://maps.gstatic.com/mapfiles/","http://csi.gstatic.com","https://maps.googleapis.com","http://maps.googleapis.com"],["http://maps.gstatic.com/intl/en_us/mapfiles/api-3/13/5","3.13.5"],[2518365001],1.0,null,null,null,null,1,"",["places"],null,0,"http://khm.googleapis.com/mz?v=131\u0026",null,"https://earthbuilder.googleapis.com","https://earthbuilder.googleapis.com",null,"http://mt.googleapis.com/vt/icon"], loadScriptTime); 
    }; 
    var loadScriptTime = (new Date).getTime(); 
    getScript("http://maps.gstatic.com/cat_js/intl/en_us/mapfiles/api-3/13/5/%7Bmain,places%7D.js"); 
})(); 

</script>/End of Google Maps script 

<script> // Script that runs the app  

$(document).ready(function() { 
    $('#startGeo').click(checkLocation); 

    function checkLocation() { 
     if (navigator.geolocation) { 
      navigator.geolocation.getCurrentPosition(getLocation, locationFail); 


     } 
     else { 
     document.write('You dont have geolocation'); 
     } 
    } // ends checkLocation() 

    function getLocation(position) { 

     var latitude = position.coords.latitude; 
     var longitude = position.coords.longitude; 
     var accuracy = position.coords.accuracy; 
     var timestamp = position.timestamp; 

     document.write(' latitude: ' + latitude + ' longitude: ' + longitude + ' accuracy: ' + accuracy + ' timestamp: ' + timestamp); 



    } 
    function locationFail() { 
    document.write('We did not get your location. You are safe from big broda'); 
    }  

}); 


</script> 

</headd> 

<body> 
<button id="startGeo"> Click here for geolocation </button> 
</body> 
</html> 

回答

1

文件撰寫似乎是你的問題的原因,而不是地理定位的失敗,假設當然你有你的AndroidManifest.xml足夠的權限:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> 

試試這個:

<html> 
<head> 
<meta charset="UTF-8"> 

<title> Geolocation Test </title> 
<script src="js/jquery-1.9.1.min.js"></script> 
<script> // Begginning of Google Maps script 

window.google = window.google || {}; 
google.maps = google.maps || {}; 
(function() { 

    function getScript(src) { 
    document.write('<' + 'script src="' + src + '"' + 
        ' type="text/javascript"><' + '/script>'); 
    } 

    var modules = google.maps.modules = {}; 
    google.maps.__gjsload__ = function(name, text) { 
    modules[name] = text; 
    }; 

    google.maps.Load = function(apiLoad) { 
    delete google.maps.Load; 
    apiLoad([0.009999999776482582,[[["http://mt0.googleapis.com/[email protected]\u0026src=api\u0026hl=en-US\u0026","http://mt1.googleapis.com/[email protected]\u0026src=api\u0026hl=en-US\u0026"],null,null,null,null,"[email protected]"],[["http://khm0.googleapis.com/kh?v=131\u0026hl=en-US\u0026","http://khm1.googleapis.com/kh?v=131\u0026hl=en-US\u0026"],null,null,null,1,"131"],[["http://mt0.googleapis.com/[email protected]\u0026src=api\u0026hl=en-US\u0026","http://mt1.googleapis.com/[email protected]\u0026src=api\u0026hl=en-US\u0026"],null,null,"imgtp=png32\u0026",null,"[email protected]"],[["http://mt0.googleapis.com/[email protected],[email protected]\u0026src=api\u0026hl=en-US\u0026","http://mt1.googleapis.com/[email protected],[email protected]\u0026src=api\u0026hl=en-US\u0026"],null,null,null,null,"[email protected],[email protected]"],null,null,[["http://cbk0.googleapis.com/cbk?","http://cbk1.googleapis.com/cbk?"]],[["http://khm0.googleapis.com/kh?v=77\u0026hl=en-US\u0026","http://khm1.googleapis.com/kh?v=77\u0026hl=en-US\u0026"],null,null,null,null,"77"],[["http://mt0.googleapis.com/mapslt?hl=en-US\u0026","http://mt1.googleapis.com/mapslt?hl=en-US\u0026"]],[["http://mt0.googleapis.com/mapslt/ft?hl=en-US\u0026","http://mt1.googleapis.com/mapslt/ft?hl=en-US\u0026"]],[["http://mt0.googleapis.com/vt?hl=en-US\u0026","http://mt1.googleapis.com/vt?hl=en-US\u0026"]],[["http://mt0.googleapis.com/mapslt/loom?hl=en-US\u0026","http://mt1.googleapis.com/mapslt/loom?hl=en-US\u0026"]],[["https://mts0.googleapis.com/mapslt?hl=en-US\u0026","https://mts1.googleapis.com/mapslt?hl=en-US\u0026"]],[["https://mts0.googleapis.com/mapslt/ft?hl=en-US\u0026","https://mts1.googleapis.com/mapslt/ft?hl=en-US\u0026"]]],["en-US","US",null,0,null,null,"http://maps.gstatic.com/mapfiles/","http://csi.gstatic.com","https://maps.googleapis.com","http://maps.googleapis.com"],["http://maps.gstatic.com/intl/en_us/mapfiles/api-3/13/5","3.13.5"],[2518365001],1.0,null,null,null,null,1,"",["places"],null,0,"http://khm.googleapis.com/mz?v=131\u0026",null,"https://earthbuilder.googleapis.com","https://earthbuilder.googleapis.com",null,"http://mt.googleapis.com/vt/icon"], loadScriptTime); 
    }; 
    var loadScriptTime = (new Date).getTime(); 
    getScript("http://maps.gstatic.com/cat_js/intl/en_us/mapfiles/api-3/13/5/%7Bmain,places%7D.js"); 
})(); 

</script>/End of Google Maps script 

<script> // Script that runs the app  

$(document).ready(function() { 
    $('#startGeo').click(checkLocation); 

    function checkLocation() { 
     if (navigator.geolocation) { 
      navigator.geolocation.getCurrentPosition(getLocation, locationFail); 
     } 
     else { 
     $('#result').append('You dont have geolocation'); 
     } 
    } // ends checkLocation() 

    function getLocation(position) { 
     var latitude = position.coords.latitude; 
     var longitude = position.coords.longitude; 
     var accuracy = position.coords.accuracy; 
     var timestamp = position.timestamp; 

     $('#result').append("location: "+' latitude: ' + latitude + ' longitude: ' + longitude + ' accuracy: ' + accuracy + ' timestamp: ' + timestamp); 


    } 
    function locationFail() { 
    $('#result').append('We did not get your location. You are safe from big broda'); 
    }  

}); 


</script> 

</head> 

<body> 
<button id="startGeo"> Click here for geolocation </button> 
<div id="result"></div> 
</body> 

BTW,有一個在你的源代碼一個錯字應該是</head>

您還應該真的使用$(document).on("deviceready")而不是$(document).ready() Phonegap作爲'就緒'事件僅指示DOM已準備好,而'deviceready'指示Phonegap已完全加載。

+0

非常感謝你,它現在起作用!至於你關於使用$(document).on(「deviceready」)而不是$(document).ready()的建議,第一行應該是這樣嗎?當我將其更改爲$(document).on(「deviceready」,function()時,該按鈕不再有效... –

+0

很高興我能幫助:-)請接受我的回答,以便其他人知道它已被回答。對於deviceready事件,你的語法看起來是正確的,所以我不知道爲什麼這不起作用 - 也許嘗試使用bind:'$(document).bind(「deviceready」,function(){...}); Phonegap如果它有幫助,deviceready事件的文檔是[here](http://docs.phonegap.com/en/2.9.0/cordova_events_events.md.html#deviceready)... – DaveAlden

相關問題