我有一個類MapHandler。CoffeeScript對象屬性和閉包
我創建了一個對象myMaphandler = new MapHandler並調用initialize方法。 但@ 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
)
爲什麼發生這種情況,什麼我應該避免這種情況做
只是澄清.. console.log(@ userLocationMarker.getPosition())爲您提供正確的數據?我覺得你的文章有些遺漏......你在用圖書館嗎? – Chris
不,它也返回null – rkotov93
好吧,以及getPosition是在哪裏定義的? – Chris