navigator.geolocation.getCurrentPosition fails on Android Browser

。_饼干妹妹 提交于 2020-06-24 10:55:21

问题


i'm trying to get the geolocation on Android Browser but nothing happens. I'm using a Samsung Galaxy S3 but i'm not sure about the version of my browser. Android version is 4.1.2 Here is my Code:

if (navigator.geolocation) {

            var timeoutVal = 10 * 1000 * 1000;
            navigator.geolocation.getCurrentPosition(
              displayPosition,
              displayError,
              { enableHighAccuracy: true, timeout: timeoutVal, maximumAge: 0 }
            );
        }

this is a code i copied and pasted from this site it gives me the "navigator.geolocation" but when it comes to "getCurrentPosition" my code stops working. Mobile Chrome works fine but this is not. I shared my position but still nothing happens. Any help will be appriciated. Thanks.

Thanks everyone i found the solution, i was getting the geolocation after some javascript operations. I tried to get the geolocation before document is ready. And it worked.


回答1:


seems like PhoneGap has a problem with geolocation I have the same issue I'm using S3 with Android 4.1.2, phonegap geolocation feature doesn't work




回答2:


In order to get the geolocation without errors, you have to make that code block work before using the values provided by the geolocation because operations are carried out asynchronously, in this question i found the solution by loading my geolocation script before other .js files. This solved my problem and another trick for this issue is, geolocation works more stable when you give "always" permission for browser to read your location. After loading for the first time, you never encounter geolocation errors.




回答3:


I know this is a bit old but it keeps coming up on searches so I thought I would add a tip that helped me.

Because I want to get the location right as the page loads I found that I needed to introduce a very short delay after the page loads. When I had no delay, I would get no error but also I would not activate the location protocols on the phone. This half second delay solved the issue. You can play with the delay and see if it solves your issues.

setTimeout(function() {getAutoLocation(true)},500);

I get the location in my "getAutoLocation(true)" function. This setTimeout only exists to introduce the delay.




回答4:


Did you give Internet permission in manifest?

<manifest xlmns:android...>
 ...
 <uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>



回答5:


I found that some Android phones (old and new) don't run properly the function getCurrentPosition, maybe trying to save some battery.

I played with the function watchPosition and then the high accuracy GPS kicked in.

Read this to know how to use the parameters properly: http://dev.w3.org/geo/api/spec-source.html#watch-position

In my case, this worked:

{
  maximumAge: 0, timeout: 2000, enableHighAccuracy: true
}

Hope this helps someone.



来源:https://stackoverflow.com/questions/14810966/navigator-geolocation-getcurrentposition-fails-on-android-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!