問題描述
我正在開發一個依賴于提取移動用戶的地理位置數據的網站.我通過以下方式以典型方式進行操作:
I am developing a website that relies on pulling the geolocation data of a mobile user. I am doing it the typical way via:
function initialize() {
if(window.google && google.gears) {
var geo = google.gears.factory.create('beta.geolocation');
geo.getCurrentPosition(useLocation, errorOnLocate);
}
else if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(useLocation, errorOnLocate);
}
else {
alert("no geo found");
}
}
function errorOnLocate(message)
{
alert(message);
zoomTo(-34.397, 150.644);
}
即使我切換地理定位方法順序,這總是會在 errorOnLocate 函數中出現 [object PositionError] 失敗.
This always fails with [object PositionError] in to the errorOnLocate function, even if I switch the geolocation method order.
這是使用任何內置瀏覽器的 HTC Hero 和 android 2.1.我的 gps 已打開,我已指示瀏覽器允許網站查看我的位置.手機自帶的谷歌地圖原生應用程序的位置"功能可以很好地獲取我的位置
This is on an HTC Hero with android 2.1 using whatever browser is built in. My gps is on and I have instructed the browser to allow websites to view my location. The "location" feature on the google map native application that comes on the phone picks up my location just fine
如果我在個人計算機上使用 FireFox 3.5 訪問我的網站,它會正確找到我的位置.(我相信它使用了 ip 和 ap 數據點的組合).無論哪種方式,它都使用相同的 javascript.
Further more if I visit my site using FireFox 3.5 on my personal computer it will find my location correctly.(I believe it uses a combination of ip and ap data points). Either way, it uses the same javascript.
這是瀏覽器中的 html/js,而不是本機應用程序.此外,確切的錯誤消息是最后一個位置提供程序被禁用"
This is html/js in a browser, not a native app. Also the exact error message is 'The last location provider was disabled'
推薦答案
這是從 WebView
還是從 Android 瀏覽器應用程序訪問的?如果來自 WebView
,您可能需要通過 Java 調用啟用地理定位.請參閱 WebView 參考.
Is this being accessed from a WebView
or from the Android Browser app? If from a WebView
, you may need to enable geolocation via a Java call. See the WebView reference for that.
否則,我不確定您的 JS 到底出了什么問題,但我知道這里的 HTML+JS 適用于 Android 瀏覽器:
Otherwise, I'm not sure precisely what's wrong with your JS, but here's HTML+JS that I know works with the Android browser:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function watchLocation(successCallback, errorCallback) {
successCallback = successCallback || function(){};
errorCallback = errorCallback || function(){};
// Try HTML5-spec geolocation.
var geolocation = navigator.geolocation;
if (geolocation) {
// We have a real geolocation service.
try {
function handleSuccess(position) {
successCallback(position.coords);
}
geolocation.watchPosition(handleSuccess, errorCallback, {
enableHighAccuracy: true,
maximumAge: 5000 // 5 sec.
});
} catch (err) {
errorCallback();
}
} else {
errorCallback();
}
}
function init() {
watchLocation(function(coords) {
document.getElementById('test').innerHTML = 'coords: ' + coords.latitude + ',' + coords.longitude;
}, function() {
document.getElementById('test').innerHTML = 'error';
});
}
</script>
</head>
<body onload="init()">
<div id="test">Loading...</div>
</body>
</html>
這篇關于android上的javascript地理定位不起作用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!