使用咖啡脚本类初始化地图标记

提供了 App.GoogleMap 咖啡脚本类 ,并且标记信息存储在 .google_map div 的 data-address-fields 属性中,地图标记可以在地图上初始化,如下所示:

# app/assets/javascripts/google_maps.js.coffee
# ...
class App.GoogleMap
  # ...
  markers: []

  constructor: (map_div)->
    # ...
    @init_markers()

  address_fields: ->
    @map_div.data('address-fields')

  init_markers: ->
    self = this  # to reference the instance as `self` when `this` is redefined.
    self.markers = []
    for address_field in self.address_fields()
      marker = new google.maps.Marker {
        map: self.map,
        position: {
          lng: address_field.longitude,
          lat: address_field.latitude
        },
        # # or, if `position` is defined in `ProfileFields::Address#as_json`:
        # position: address_field.position,
        title: address_field.value
      }
      self.markers.push marker

要了解有关标记选项的更多信息,请查看 Google 的 MarkerOptions 文档及其标记指南