Android 上的 JavaScript 地理定位不起作用
我正在开发一个依赖于提取移动用户的地理位置数据的网站。我通过以下典型方式进行操作:
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] 。
这是在 Android 2.1 的 HTC Hero 上使用内置的任何浏览器。我的 GPS 已打开,并且我已指示浏览器允许网站查看我的位置。手机上的谷歌地图本机应用程序上的“位置”功能可以很好地获取我的位置
此外,如果我在个人计算机上使用 FireFox 3.5 访问我的网站,它会正确找到我的位置。(我相信它使用组合ip 和 ap 数据点)。无论哪种方式,它都使用相同的 JavaScript。
编辑:这是浏览器中的 html/js,而不是本机应用程序。另外,确切的错误消息是“最后一个位置提供程序已被禁用”
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.
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
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.
EDIT: This is html/js in a browser, not a native app. Also the exact error message is 'The last location provider was disabled'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我遇到了这个问题,我认为这是因为设备电量低。给它充电,问题就消失了。
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.
这是从
WebView
还是从 Android 浏览器应用访问的?如果来自WebView
,您可能需要通过 Java 调用启用地理定位。请参阅 WebView 参考。否则,我不确定你的 JS 到底出了什么问题,但这是我知道适用于 Android 浏览器的 HTML+JS:
Is this being accessed from a
WebView
or from the Android Browser app? If from aWebView
, you may need to enable geolocation via a Java call. See the WebView reference for that.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:
这在我的 Android 2.4 上不起作用,通过启用 GPS 之外的无线网络位置(不能仅使用 GPS)解决了问题
。地点及地点隐私->启用无线网络。
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)
Settings -> Location & Privacy -> Enable Wireless Networks.
我已经使用以下两款手机测试了 html5 地理定位功能:
两者都运行 Android 2.1 update1 操作系统,并且都无法使用内置 Google 浏览器和演示网站(例如:
但是,我开发了自己的应用程序(使用上面已提供的代码的组合),该应用程序成功运行在:
,我使用的代码是:
Javascript lib:
我的 html 文件中的实际代码:
抱歉,这个源代码只是不想正确格式化,请给我发消息,如果您愿意,我会直接向您发送代码。
I have tested html5 geolocation functionality with the following two phones:
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:
However, I have developed my own app (using a combination of the code already presented above) that successfully runs on:
and the code I am using is:
Javascript lib:
actual code in my html file:
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.