我正在嘗試與backbone,coffescript和google maps api一起工作。我能夠渲染地圖並添加中心標記。我在地圖上添加地點集合作爲漫遊者時遇到問題。如何與視圖中的其他函數或應用程序的其他部分共享下面的@map對象?如何從Backbone/Coffee中的其他視圖函數訪問我的@map對象?
addMarker @map是未定義的。
render: ->
$(@el).html(@template())
primary = @collection.at(@collection.length - 1)
if primary
latlng = {}
@collection.each(@appendLocation)
latlng['latitude'] = primary.attributes.latitude;
latlng['longitude'] = primary.attributes.longitude;
@renderMap(latlng)
this
renderMap: (latlng) ->
view = new Bone.Views.Map()
$('#map').append(view.render().el)
latlng = new google.maps.LatLng(latlng['latitude'], latlng['longitude'])
myOptions =
zoom: 12
center: latlng
mapTypeId: google.maps.MapTypeId.ROADMAP
@map = new google.maps.Map(view.render().el, myOptions)
marker = new google.maps.Marker({
position: latlng,
animation: google.maps.Animation.DROP,
map: @map,
title:"Hello World!"
})
@collection.each(@addMarker)
addMarker: (location)->
console.log(@map) <-----UNDEFINED
latlng = new google.maps.LatLng(location.attributes.latitude, location.attributes.longitude)
console.log location.attributes.latitude
location_marker = new google.maps.Marker({
position: latlng,
animation: google.maps.Animation.DROP,
map: @map,
title:"Hello World!"
})