使用CoffeeScript为谷歌地图提供多个InfoWindows

Multiple InfoWindows for Google maps with CoffeeScript

本文关键字:InfoWindows CoffeeScript 谷歌地图 使用      更新时间:2023-09-26

我似乎无法让信息窗口与coffeescript一起工作。我的初始化代码创建了一个类,构造函数创建了一张谷歌地图,然后根据用户界面的需要,如果需要,我使用类的方法,如"addSingleMarkers",在地图上放置标记。标记可以正常工作,它们会正确地显示在地图上,但当我尝试为每个标记放置infoWindows时,它失败了。使用此代码一次,并且仅在第一次单击任何标记时显示一次infoWindow,其中包含该标记的正确信息,如果此infoWindow关闭,则无法再次打开。我有一系列关于javascript的红色帖子,但就是不能让它与coffeescript一起工作。我做错了什么?

class window.GoogleMap
  constructor: (@canvasID   = "#google_map",
            @searchID   = "#google_map_search",
            @rectBounds = "#google_map_rect") ->
  mapOptions = {
   center: new google.maps.LatLng(38, 23.5),
   zoom: 8,
   mapTypeId: google.maps.MapTypeId.ROADMAP
   }
  @gmap = new google.maps.Map($(@canvasID)[0], mapOptions) if $(@canvasID).length 
  true

  addSingleMarkers: (data) ->
    for coordinates, text of data
      point_length = coordinates.length
      data_array =
      coordinates.substring(6,point_length).
      replace('(','').replace(')','').split(" ")
      point = new google.maps.LatLng(data_array[1],data_array[0])
      marker = new google.maps.Marker(
        position: point
        map: @gmap
        title: text)
      @bindInfoW marker, text   

  bindInfoW : (marker, contentString) ->
    google.maps.event.addListener marker, "click", ->
      infowindow = new google.maps.InfoWindow    
      infowindow.setContent contentString
      infowindow.open @gmap, marker

问题出在这行:google.maps.event.addListener marker, "click", ->,它本应该是google.maps.event.addListener marker, "click", =>。此外,我创建了一个单独的方法调用,只创建了InfoWindow的一个实例。->和=>之间存在差异。后者创建一个临时变量var_this=this,稍后将其与infoWindow 一起使用