【发布时间】:2015-03-06 03:07:34
【问题描述】:
我正在显示带有一组固定标记的 Google 地图,这些标记已存储在数组中。显示的特定标记取决于用户选择的日期。选择日期后,我会显示相应的标记,并更新每个标记的信息窗口以了解在该地点举行的活动。
我遇到的问题如下。有两个标记相对靠近,称为 M1 和 M2。我选择了一个应该显示 M2 的日期,并且一切都按预期工作。将显示正确的标记,并在信息窗口中显示正确的信息。然后我选择一个应该显示 M1 的日期。
单步执行代码表示显示了相应的标记 (M1)。但是,信息窗口仍然显示 M2 上次显示时的信息。
奇怪的是,如果我放大以关注标记所在的区域,然后单击标记以显示信息窗口,则会显示 M1 的相应信息。如果我缩小,则会显示 M2 的信息。
如果我在地图已经放大的情况下重复上述初始步骤,信息窗口总是会显示适当的信息。
没有使用缩放事件注册的处理程序,所有标记都使用设置为 false 的优化属性进行初始化。
我不知道为什么会这样。有什么想法吗?
class ottb.Map
constructor: () ->
@map = new google.maps.Map(document.getElementById("schedule-map"), gMapOptions)
@gameMarkers = {}
@displayedGames = {}
@lastInfoWindow = null
# Updates the map to show the games for the currently selected date.
displayGames: (newGames) ->
@lastInfoWindow.close() if @lastInfoWindow isnt null
# Fade out currently displayed games if they are not in the new list of
# games to display
for own teamAbbr, displayedGame of @displayedGames
hasIt = newGames.some( (newGame) -> newGame.home_team_abbr == displayedGame.home_team_abbr)
if not hasIt
@fadeOutMarker(@gameMarkers[displayedGame.home_team_abbr])
delete @displayedGames[displayedGame.home_team_abbr]
for newGame in newGames
do (newGame) =>
marker = @gameMarkers[newGame.home_team_abbr]
if not marker?
marker = new google.maps.Marker
position: new google.maps.LatLng(parseFloat(newGame.lat), parseFloat(newGame.lon))
map: @map
opacity: 0
opacities: []
marker.setTitle(newGame.away_team_name + ' @ ' + newGame.home_team_name)
hasIt = @displayedGames[newGame.home_team_abbr]?
@fadeInMarker(marker, 0) if not hasIt
source = $("#info-window").html()
template = Handlebars.compile(source)
context =
game_id: newGame.id,
away_team: newGame.away_team_abbr,
home_team: newGame.home_team_abbr,
game_time: newGame.game_time
displaySelectGameLink: true
google.maps.event.addListener(marker, 'click', =>
@lastInfoWindow.close() if @lastInfoWindow isnt null
@lastInfoWindow = new google.maps.InfoWindow()
@lastInfoWindow.setContent(template(context))
@lastInfoWindow.open(that.map, marker)
return false)
@gameMarkers[newGame.home_team_abbr] = marker
@displayedGames[newGame.home_team_abbr] = newGame
【问题讨论】:
-
很难说没有一些代码示例......
-
添加了咖啡脚本代码。
-
另一个有趣的花絮。我刚刚通过人为将M2的坐标更改为远离M1的方式测试了代码,不再出现问题。
标签: javascript google-maps coffeescript