我正在使用谷歌地图 API,并尝试将 map 置于用户位置的中心。在 CoffeeScript 中,我有:
lat = undefined
long = undefined
navigator.geolocation.getCurrentPosition (position) ->
lat = position.coords.latitude
long = position.coords.longitude
return
alert(lat)
警报(纬度)会给我未定义的信息。
最佳答案
getCurrentPosition
是异步的,因此,您需要在 getCurrentPosition
的内部调用alert(lat)
打电话。
lat = undefined
long = undefined
navigator.geolocation.getCurrentPosition (position) ->
lat = position.coords.latitude
long = position.coords.longitude
alert(lat)
return
关于javascript - 改变 CoffeeScript 中的公共(public)变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30986947/