CoffeeScript 对象属性和闭包
我有一个 MapHandler 类。
我创建了一个对象 myMaphandler = new MapHandler 并调用了初始化方法。 但是 @userLocationMarker.getPosition() 返回 null :(
如果我评论警报并从 Chrome JS 控制台调用 @userLocationMarker.getPosition() 我会获得必要的坐标。
class window.MapHandler
initialize: (centerLocation) ->
@makeMap(centerLocation)
@defineUserLocation()
alert @userLocationMarker.getPosition()
makeMap: (centerLocation) ->
myOptions =
zoom: 14
center: centerLocation
mapTypeId: google.maps.MapTypeId.ROADMAP
@map = new google.maps.Map(document.getElementById("map_canvas"), myOptions)
placeMarker: (location, icon_path) ->
if icon_path
markerImage = new google.maps.MarkerImage(icon_path, null, null, null, new google.maps.Size(25, 25))
else
markerImage = null
marker = new google.maps.Marker(
position: location
map: @map
icon: markerImage)
defineUserLocation: () ->
@userLocationMarker = @placeMarker(null, null)
handleMap = (position) =>
pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude)
infowindow = new google.maps.InfoWindow(
map: @map
position: pos
content: 'Если это не ваше местоположение - передвиньте маркер'
)
@map.setCenter(pos)
@userLocationMarker.setPosition(pos)
if navigator.geolocation
@userPosition = navigator.geolocation.getCurrentPositon(
handleMap
)
为什么会发生这种情况以及我应该采取什么措施来避免这种情况?
I have a class MapHandler.
I created an object myMaphandler = new MapHandler and called initialize method.
But @userLocationMarker.getPosition() is returning null :(
If I'll comment alert and call @userLocationMarker.getPosition() from Chrome JS console I getting necessary coordinates.
class window.MapHandler
initialize: (centerLocation) ->
@makeMap(centerLocation)
@defineUserLocation()
alert @userLocationMarker.getPosition()
makeMap: (centerLocation) ->
myOptions =
zoom: 14
center: centerLocation
mapTypeId: google.maps.MapTypeId.ROADMAP
@map = new google.maps.Map(document.getElementById("map_canvas"), myOptions)
placeMarker: (location, icon_path) ->
if icon_path
markerImage = new google.maps.MarkerImage(icon_path, null, null, null, new google.maps.Size(25, 25))
else
markerImage = null
marker = new google.maps.Marker(
position: location
map: @map
icon: markerImage)
defineUserLocation: () ->
@userLocationMarker = @placeMarker(null, null)
handleMap = (position) =>
pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude)
infowindow = new google.maps.InfoWindow(
map: @map
position: pos
content: 'Если это не ваше местоположение - передвиньте маркер'
)
@map.setCenter(pos)
@userLocationMarker.setPosition(pos)
if navigator.geolocation
@userPosition = navigator.geolocation.getCurrentPositon(
handleMap
)
Why this occurs and what I should do for avoiding this situation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用
null
位置初始化@userLocationMarker
:然后在
handleMap
中设置“真实”位置:该位置用作
handleMap
的回调code>getCurrentPosition:问题在于
getCurrentPosition
是异步的,因此您的alert
在handleMap
调用之前被调用获取当前位置
。任何依赖于getCurrentPosition
所做的事情都必须在handleMap
回调中,否则它们需要准备好处理尚未到达的数据。您的示例代码中也存在拼写错误,您在
if navigator.geolocation
块中拼写了getCurrentPosition
错误。当您尝试从 JavaScript 控制台检查位置时,
getCurrentPosition
已调用handleMap
,并且@userLocationMarker
将正确初始化其位置。You initialize
@userLocationMarker
with anull
position:And then you set the "real" positon in
handleMap
:which is used as a callback for
getCurrentPosition
:The problem is that
getCurrentPosition
is asynchronous so youralert
is getting called beforehandleMap
has been called bygetCurrentPosition
. Anything that depends on whatgetCurrentPosition
does has to be in thehandleMap
callback or they need to be prepared to deal with data that hasn't arrived yet.There's also a typo in your example code, you spelled
getCurrentPosition
wrong in yourif navigator.geolocation
block.By the time you try checking the position from the JavaScript console,
getCurrentPosition
has calledhandleMap
and@userLocationMarker
will have had its position properly initialized.