CoffeeScript绑定变量

时间:2011-11-11 22:45:01

标签: javascript variables binding coffeescript

我有一个班级

class window.MapHandler
  map = null
  userLocationMarker = null

  makeMap: (location) ->
    myOptions =
      zoom: 14
      center: location
      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: () ->
    if navigator.geolocation
      navigator.geolocation.getCurrentPosition(
        (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 = @placeMarker(pos, null)
      )
    alert @userLocationMarker.getPosition()

为什么最后我在这一点上有一个居中的地图和制造者,但@userLocationMarker未定义且getPosition方法调用错误?

1 个答案:

答案 0 :(得分:0)

navigator.geolocation.getCurrentPosition是一个异步函数。它的回调(您设置@userLocationMarker的位置)会在alert来电后运行。你可以,例如将您的alert行放入回调中。