【问题标题】:Google Maps API with CoffeeScript: infoWindow won't close when user clicks off it带有 CoffeeScript 的 Google Maps API:用户单击时不会关闭 infoWindow
【发布时间】:2013-01-28 01:23:34
【问题描述】:

所以这是我第一次同时使用 Google Maps API 和 CoffeeScript。我们有在标记单击时弹出的 infoWindows,但它们仅在有人单击另一个标记并显示不同的 infoWindow 时才会被删除,否则不会关闭。当用户点击离开它时,我们只需要关闭弹出框。这是我们正在使用的代码(抱歉,如果它有点多,我不确定确切的调用应该在哪里):

        @infoWindow = new google.maps.InfoWindow unless @infoWindow?
        @infoWindow.close()

        places = if @options.places? and @options.places.length > 0 then @options.places else null

        if places?
          # add pins
          @add_pins(places)

          # fit bounds
          if @options.googleMap.setBounds == true
            @set_bounds(places)

        # ie8 hack
        ie_8_map()
        # results
        if @options.resultType == 'floating'
          $(".places-map .overlay").show()

      #######
      add_pins: (places) ->
        @pins = {}
        if @markers
          for marker in @markers
            marker.setMap null

        for place, i in places
          @pins[place.id] = '/assets/pins/'+ String.fromCharCode('A'.charCodeAt()+i)+'.png'
          project_pin = new google.maps.MarkerImage '/assets/pins/'+ String.fromCharCode('A'.charCodeAt()+i)+'.png', new google.maps.Size(40, 63), new google.maps.Point(0,0), new google.maps.Point(20, 63)
          project_pin_shadow = new google.maps.MarkerImage '/assets/pin-shadow.png', new google.maps.Size(74, 63), new google.maps.Point(0,0), new google.maps.Point(22, 63)
          marker = new google.maps.Marker map: @googleMap, position: @_latLng(place), icon: project_pin, shadow: project_pin_shadow
          @markers.push marker
          # Adding the pop-up info window
          if place.name
            google.maps.event.addListener marker, 'click', @clickMapMarker.bind(this, place, marker)
        # Add pin pictures
        for k,v of @pins
          pinImg = $(".place-result ." + k + "_place_pin")
          pinImg.parent(".place_pins_popup_img").show()
          pinImg.attr("src", v)

      #######
      clickMapMarker: (place, marker) ->
        @infoWindow.close()
        if @options.resultType == 'leftbar'
          if place.result_html
            @infoWindow.setContent place.result_html
          else
            pop = $('<div class="place-result"></div>').html($('#res_' + place.id).clone().attr("id":"cln_"+place.id))
            pop.find('.place_pins_popup_img').remove()
            #pop.find('.place_pins_popup_img').remove()
            @infoWindow.setContent pop.html()
        else
          @infoWindow.setContent JST["templates/places_map_listings"](places: [place])
        @infoWindow.open(@googleMap, marker)

【问题讨论】:

    标签: google-maps google-maps-api-3 coffeescript onclicklistener infowindow


    【解决方案1】:

    恐怕我对CoffeeScript还不熟悉,但从外观上看,您需要在地图上添加另一个点击事件监听器,以便当用户点击地图时,InfoWindow 会关闭。

    所以是这样的:

    google.maps.event.addListener @googleMap, "click", ->
      @infoWindow.close()    
    
    # @googleMap.panTo(someGoogleMapLatLng);
    # @googleMap.setZoom(someIntZoomLevel);  
    

    【讨论】:

      猜你喜欢
      • 2011-01-03
      • 1970-01-01
      • 2012-01-06
      • 1970-01-01
      • 1970-01-01
      • 2011-10-10
      • 1970-01-01
      • 2011-12-09
      • 1970-01-01
      相关资源
      最近更新 更多