简体   繁体   English

使用CoffeeScript的多个InfoWindows for Google映射

[英]Multiple InfoWindows for Google maps with CoffeeScript

I cant seem to make infowindows work with coffeescript . 我似乎无法使infowindows与coffeescript一起使用。 My initialisation code creates a Class with the constructor creating a Google map and then according to the needs of the user interface ,I use the methods of the class like 'addSingleMarkers' to place markers on the map if is needed. 我的初始化代码使用构造函数创建了一个Google地图,然后创建了一个Class,然后根据用户界面的需要,我使用了诸如“ addSingleMarkers”之类的方法来在地图上放置标记。 The markers work they are beig displayed on the map properly, but when I try to place infoWindows for each marker it fails. 这些标记可以正常显示在地图上,但是当我尝试为每个标记放置infoWindows时,它会失败。 With this code once and only once on the first click of any marker an infoWindow is displayed with the correct information for the marker, if this infoWindow is closed then it cannot open again. 使用此代码一次,在任何标记的第一次单击上仅一次,将显示一个带有正确标记信息的infoWindow,如果此infoWindow关闭,则无法再次打开。 I have red series of posts on this for javascript but just cant get it to work with coffeescript. 我在这方面有一些关于javascript的红色系列文章,但无法使其与coffeescript一起使用。 what am I doing wrong ? 我究竟做错了什么 ?

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

The problem was with the line : google.maps.event.addListener marker, "click", -> where it should have been google.maps.event.addListener marker, "click", => Also I created a seperate method call to create only one instance of InfoWindow. 问题出在以下一行: google.maps.event.addListener marker, "click", ->应该在其中的google.maps.event.addListener marker, "click", =>我还创建了一个单独的方法调用仅创建一个InfoWindow实例。 There is a difference between -> and => . ->和=>之间有区别。 The latter creates a temp variable var _this = this and uses it later with the infoWindow 后者创建一个临时变量var _this = this,以后将其与infoWindow一起使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM