admin管理员组文章数量:1384418
I'm trying to get user's geolocation in development environment. I believe I'm fulfilling every requirements, though I still don't see any prompt asking for my position.
Here's what I'm using:
navigator.geolocation
The Geolocation API is accessed via a call to
navigator.geolocation
; this will cause the user's browser to ask them for permission to access their location data.
- ngrok to have a SSL tunnel
Secure context: This feature [Geolocation API] is available only in secure contexts (HTTPS), in some or all supporting browsers.
- Firefox 99.0.1 (able to be located on other websites, no domain-specific rules)
PermissionState
is always prompt
, which mean a prompt is currently asking for the permission right ?
Am I missing something, or is there specific requirements when asking for localhost geolocation ?
I'm trying to get user's geolocation in development environment. I believe I'm fulfilling every requirements, though I still don't see any prompt asking for my position.
Here's what I'm using:
navigator.geolocation
The Geolocation API is accessed via a call to
navigator.geolocation
; this will cause the user's browser to ask them for permission to access their location data.
- ngrok to have a SSL tunnel
Secure context: This feature [Geolocation API] is available only in secure contexts (HTTPS), in some or all supporting browsers.
- Firefox 99.0.1 (able to be located on other websites, no domain-specific rules)
PermissionState
is always prompt
, which mean a prompt is currently asking for the permission right ?
Am I missing something, or is there specific requirements when asking for localhost geolocation ?
Share Improve this question edited Apr 18, 2022 at 19:24 Sumak asked Apr 18, 2022 at 17:45 SumakSumak 1,0811 gold badge10 silver badges26 bronze badges 2- More info here: developer.mozilla/en-US/docs/Web/API/Geolocation – Steve Commented Apr 18, 2022 at 18:06
- See: Can I answer my own question? – Yogi Commented Apr 18, 2022 at 18:53
1 Answer
Reset to default 3Somehow I'd to call navigator.geolocation.getCurrentPosition(callback)
in order to have a prompt displayed.
If it can help anyone, here's my test code:
// inside a Stimulus controller
connect() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(this.showPosition)
} else {
// I believe it may also mean geolocation isn't supported
alert('Geolocation denied')
}
}
showPosition(position) {
alert(`${position.coords.longitude} - ${position.coords.latitude}`)
}
本文标签: javascriptAsk geolocation permissionStack Overflow
版权声明:本文标题:javascript - Ask geolocation permission - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744535507a2611290.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论