admin管理员组文章数量:1026318
I'm trying to play with the scope of js to pull a variable out of navigator.geolocation.getCurrentPosition
var lat;
function callback (position) {
lat = position.coords.latitude;
}
navigator.geolocation.getCurrentPosition(callback,fail,{timeout:10000});
// after getCurrentPosition succeeds
alert(lat); // this alerts null
The above code cannot store position.coords.latitude in the lat variable because of the scope. Is there a way to do this?
I'm trying to play with the scope of js to pull a variable out of navigator.geolocation.getCurrentPosition
var lat;
function callback (position) {
lat = position.coords.latitude;
}
navigator.geolocation.getCurrentPosition(callback,fail,{timeout:10000});
// after getCurrentPosition succeeds
alert(lat); // this alerts null
The above code cannot store position.coords.latitude in the lat variable because of the scope. Is there a way to do this?
Share Improve this question asked Mar 29, 2012 at 23:28 DerekDerek 12.4k31 gold badges106 silver badges166 bronze badges2 Answers
Reset to default 2You have to remember the async\ajax nature.
this is the execution order of your code:
var lat;
alert(lat); // this alerts null
navigator.geolocation.getCurrentPosition(callback,fail,{timeout:10000});
function callback (position) {
lat = position.coords.latitude;
}
This why you get null. async!, async! :)
You can save your Position variable to input hidden field on document ready. After that, you can use jQuery to get back Geolocation value
Javscript:
<script>
var lat;
alert(lat); // this alerts null
navigator.geolocation.getCurrentPosition(callback,fail,{timeout:10000});
function callback (position) {
jQuery('#pos_lat').val(position.coords.latitude);
}
</script>
HTML:
<input hidden id='pos_lat' value='' />
//value = position latitude on load
To get back value:
jQuery('#pos_lat').val();
I'm trying to play with the scope of js to pull a variable out of navigator.geolocation.getCurrentPosition
var lat;
function callback (position) {
lat = position.coords.latitude;
}
navigator.geolocation.getCurrentPosition(callback,fail,{timeout:10000});
// after getCurrentPosition succeeds
alert(lat); // this alerts null
The above code cannot store position.coords.latitude in the lat variable because of the scope. Is there a way to do this?
I'm trying to play with the scope of js to pull a variable out of navigator.geolocation.getCurrentPosition
var lat;
function callback (position) {
lat = position.coords.latitude;
}
navigator.geolocation.getCurrentPosition(callback,fail,{timeout:10000});
// after getCurrentPosition succeeds
alert(lat); // this alerts null
The above code cannot store position.coords.latitude in the lat variable because of the scope. Is there a way to do this?
Share Improve this question asked Mar 29, 2012 at 23:28 DerekDerek 12.4k31 gold badges106 silver badges166 bronze badges2 Answers
Reset to default 2You have to remember the async\ajax nature.
this is the execution order of your code:
var lat;
alert(lat); // this alerts null
navigator.geolocation.getCurrentPosition(callback,fail,{timeout:10000});
function callback (position) {
lat = position.coords.latitude;
}
This why you get null. async!, async! :)
You can save your Position variable to input hidden field on document ready. After that, you can use jQuery to get back Geolocation value
Javscript:
<script>
var lat;
alert(lat); // this alerts null
navigator.geolocation.getCurrentPosition(callback,fail,{timeout:10000});
function callback (position) {
jQuery('#pos_lat').val(position.coords.latitude);
}
</script>
HTML:
<input hidden id='pos_lat' value='' />
//value = position latitude on load
To get back value:
jQuery('#pos_lat').val();
本文标签: Saving variables outside of navigatorgeolocationgetCurrentPosition (javascript)Stack Overflow
版权声明:本文标题:Saving variables outside of navigator.geolocation.getCurrentPosition? (javascript) - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745627940a2159978.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论