admin管理员组文章数量:1315951
I did a website and I want to read the 'real' position GPS on mobile (Android & iPhone). When I try set the location on my website from my Android with W3C javascript method the GPS is not enabled and the position is set by IP (When I try with Google Maps app the GPS is enabled and blink on the status bar). Is any way for read the GPS (real GPS) from a web on a mobile? Thanks in advance!
I did a website and I want to read the 'real' position GPS on mobile (Android & iPhone). When I try set the location on my website from my Android with W3C javascript method the GPS is not enabled and the position is set by IP (When I try with Google Maps app the GPS is enabled and blink on the status bar). Is any way for read the GPS (real GPS) from a web on a mobile? Thanks in advance!
Share Improve this question edited Apr 16, 2012 at 20:06 gakhov 1,9714 gold badges28 silver badges39 bronze badges asked Apr 16, 2012 at 18:12 user512663user512663 1731 silver badge10 bronze badges2 Answers
Reset to default 7With HTML5 you can do that. You need to check Geolocation API: Dive Into HTML5 and W3C Geolocation API Specification
The simplest example looks like:
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
function (position) {
do_something(position.coords.latitude,position.coords.longitude);
},
function (error){
switch(error.code){
case error.TIMEOUT:
// Timeout
break;
case error.POSITION_UNAVAILABLE:
// Position unavailable
break;
case error.PERMISSION_DENIED:
// Permission denied
break;
case error.UNKNOWN_ERROR:
// Unknown error
break;
default: break;
}
}
);
}
Probably you're also interesting in some browser specific implementations: Mozilla, IE, Chrome
Updated. As Mozilla said here, Devices with a GPS, for example, can take a minute or more to get a GPS fix, so less accurate data (IP location or wifi) may be returned to getCurrentPosition() to start.
So, if you need to have high accuracy (like GPS only), use watchPosition
instead of getCurrentPosition
.
As stated in the W3C specification, you might need to use the third argument of both watchPosition and getCurrentPosition, which is a PositionOptions interface to set enableHighAccuracy to true, else, using its default value false, the GPS won't necessarily be switched on by the device.
本文标签: javascriptCan I read the 39real39 mobile GPS coordinates from a websiteStack Overflow
版权声明:本文标题:javascript - Can I read the 'real' mobile GPS coordinates from a website? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741969449a2407745.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论