我有一个班级
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方法调用错误?
答案 0 :(得分:0)
navigator.geolocation.getCurrentPosition
是一个异步函数。它的回调(您设置@userLocationMarker
的位置)会在alert
来电后运行。你可以,例如将您的alert
行放入回调中。