所以这是我第一次同时使用 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)