android上的javascript地理定位不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3367994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript geolocation on android doesn't work
提问by Dan.StackOverflow
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);
}
This always fails with [object PositionError] in to the errorOnLocate function, even if I switch the geolocation method order.
即使我切换了地理定位方法的顺序,这总是会因 [object PositionError] 失败而出现在 errorOnLocate 函数中。
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
这是在使用任何内置浏览器的 android 2.1 的 HTC Hero 上。我的 GPS 已开启,我已指示浏览器允许网站查看我的位置。手机自带的谷歌地图本机应用程序的“位置”功能可以很好地获取我的位置
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.
此外,如果我在个人计算机上使用 FireFox 3.5 访问我的站点,它将正确找到我的位置。(我相信它使用了 ip 和 ap 数据点的组合)。无论哪种方式,它都使用相同的 javascript。
EDIT: This is html/js in a browser, not a native app. Also the exact error message is 'The last location provider was disabled'
编辑:这是浏览器中的 html/js,而不是本机应用程序。此外,确切的错误消息是“最后一个位置提供程序已禁用”
回答by Roman Nurik
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 referencefor that.
这是从WebView
Android 浏览器应用程序访问还是从 Android 浏览器应用程序访问?如果来自WebView
,您可能需要通过 Java 调用启用地理定位。请参阅WebView 参考。
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:
否则,我不确定你的 JS 到底出了什么问题,但我知道这里是 HTML+JS,我知道它适用于 Android 浏览器:
<!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>
回答by Eduardo Chongkan
This wasn't working on my Android 2.4, the problem was solved by enabling Wireless Networks Location besides GPS (wasn't working with just GPS)
这在我的 Android 2.4 上不起作用,通过启用 GPS 之外的无线网络位置解决了问题(不能仅使用 GPS)
Settings -> Location & Privacy -> Enable Wireless Networks.
设置 -> 位置和隐私 -> 启用无线网络。
回答by Tom Kincaid
I was having this problem and I think it was because the power on the device was low. Charged it up and the problem went away.
我遇到了这个问题,我认为这是因为设备上的电源很低。给它充电,问题就消失了。
回答by Rok
I have tested html5 geolocation functionality with the following two phones:
我已经用以下两部手机测试了 html5 地理定位功能:
- HTC Wildfire
- HTC Galaxy
- 宏达电野火
- 宏达银河
Both were running the Android 2.1 update1 OS and both failed to do geolocation using the built in Google browser and demo sites like:
两者都运行 Android 2.1 update1 操作系统,并且都无法使用内置的 Google 浏览器和演示站点进行地理定位,例如:
However, I have developed my own app (using a combination of the code already presented above) that successfully runs on:
但是,我已经开发了自己的应用程序(使用上面已经提供的代码的组合),它成功地运行在:
- HTC Wildfire
- Android 2.1 update1 emulator
- have not had HTC Galaxy yet to test
- 宏达电野火
- Android 2.1 update1 模拟器
- 尚未测试 HTC Galaxy
and the code I am using is:
我使用的代码是:
Javascript lib:
function is_browser_gps_capable() { var _locator_object; try { _locator_object = navigator.geolocation; } catch (e) { return false; } if (_locator_object) return true; else return false; }
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(); }
}
actual code in my html file:
if (is_browser_gps_capable()) { $('geo_location').innerHTML = 'Locating device...'; watchLocation(function(coords) { var latlng = new google.maps.LatLng(coords.latitude, coords.longitude); var myOptions = { zoom: 16, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP }; var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); var marker = new google.maps.Marker({ position: latlng, map: map, });
$('geo_location').innerHTML = coords.latitude + ',' + coords.longitude; }, function() { $('geo_location').innerHTML = 'Not supported'; }); } else { $('geo_location').innerHTML = 'Not supported'; }
Javascript库:
function is_browser_gps_capable() { var _locator_object; try { _locator_object = navigator.geolocation; } catch (e) { return false; } if (_locator_object) return true; else return false; }
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(); }
}
我的 html 文件中的实际代码:
if (is_browser_gps_capable()) { $('geo_location').innerHTML = 'Locating device...'; watchLocation(function(coords) { var latlng = new google.maps.LatLng(coords.latitude, coords.longitude); var myOptions = { zoom: 16, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP }; var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); var marker = new google.maps.Marker({ position: latlng, map: map, });
$('geo_location').innerHTML = coords.latitude + ',' + coords.longitude; }, function() { $('geo_location').innerHTML = 'Not supported'; }); } else { $('geo_location').innerHTML = 'Not supported'; }
Sorry this source code just does not want to format properly, message me and i'll send you the code directly if you wish so.
抱歉,此源代码只是不想正确格式化,请给我发消息,如果您愿意,我会直接将代码发送给您。