CoffeeScript 对象属性和闭包

发布于 2024-12-17 00:14:11 字数 3773 浏览 2 评论 0原文

我有一个 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
      )

LINK

为什么会发生这种情况以及我应该采取什么措施来避免这种情况?

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
      )

LINK

Why this occurs and what I should do for avoiding this situation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一刻暧昧 2024-12-24 00:14:11

您使用 null 位置初始化 @userLocationMarker

@userLocationMarker = @placeMarker(null, null)

然后在 handleMap 中设置“真实”位置:

handleMap = (position) =>
  #...
  @userLocationMarker.setPosition(pos)

该位置用作 handleMap 的回调code>getCurrentPosition:

if navigator.geolocation
  @userPosition = navigator.geolocation.getCurrentPosition(
    handleMap
  )

问题在于 getCurrentPosition 是异步的,因此您的 alerthandleMap 调用之前被调用获取当前位置。任何依赖于 getCurrentPosition 所做的事情都必须在 handleMap 回调中,否则它们需要准备好处理尚未到达的数据。

您的示例代码中也存在拼写错误,您在 if navigator.geolocation 块中拼写了 getCurrentPosition 错误。

当您尝试从 JavaScript 控制台检查位置时,getCurrentPosition 已调用 handleMap,并且 @userLocationMarker 将正确初始化其位置。

You initialize @userLocationMarker with a null position:

@userLocationMarker = @placeMarker(null, null)

And then you set the "real" positon in handleMap:

handleMap = (position) =>
  #...
  @userLocationMarker.setPosition(pos)

which is used as a callback for getCurrentPosition:

if navigator.geolocation
  @userPosition = navigator.geolocation.getCurrentPosition(
    handleMap
  )

The problem is that getCurrentPosition is asynchronous so your alert is getting called before handleMap has been called by getCurrentPosition. Anything that depends on what getCurrentPosition does has to be in the handleMap 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 your if navigator.geolocation block.

By the time you try checking the position from the JavaScript console, getCurrentPosition has called handleMap and @userLocationMarker will have had its position properly initialized.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文