class PostRunner::TrackView

A TrackView object uses OpenLayers to draw a map with the waypoints of the activity. Lap end are marked with enumerated lap markers.

Public Class Methods

new(activity) click to toggle source

Create a new TrackView object for a given Activity. @param activity [Activity] The activity to render

# File lib/postrunner/TrackView.rb, line 25
def initialize(activity)
  @activity = activity
  @session = @activity.fit_activity.sessions[0]
  @has_geo_data = @session.has_geo_data?
end

Public Instance Methods

to_html(doc) click to toggle source

Render the map widget with the track and marker overlay as HTML code. @param doc [HTMLBuilder] HTML document to add to.

# File lib/postrunner/TrackView.rb, line 33
def to_html(doc)
  return unless @has_geo_data

  doc.body_init_script('pr_trackview_init_xy();')

  doc.head {
    doc.unique(:trackview_style) {
      doc.style(style)
      doc.link({ 'rel' => 'stylesheet',
                 'href' => 'openlayers/ol.css',
                 'type' => 'text/css' })
      doc.script({ 'language' => 'javascript', 'type' => 'text/javascript',
                   'src' => 'openlayers/ol.js' })
      doc.script({ 'language' => 'javascript', 'type' => 'text/javascript',
                   'src' => 'postrunner/trackview.js' })
    }
    doc.script(java_script)
  }

  ViewFrame.new('map', 'Map', 600, nil, true) {
    doc.div({ 'id' => 'map', 'class' => 'trackmap' })
  }.to_html(doc)

end

Private Instance Methods

java_script() click to toggle source
# File lib/postrunner/TrackView.rb, line 74
    def java_script
      center_long = @session.swc_long +
        (@session.nec_long - @session.swc_long) / 2.0
      center_lat = @session.swc_lat +
        (@session.nec_lat - @session.swc_lat) / 2.0

      <<"EOT"
#{track_points}
#{lap_markers}
function pr_trackview_init_xy() {
  pr_trackview_init(#{center_long}, #{center_lat});
};
EOT
    end
lap_markers() click to toggle source

Generate a javascript variable with an Array with the coordinates of the start point and the lap end points. Each coordinate is an Array with a longitude and latitude in EPSG:4326.

# File lib/postrunner/TrackView.rb, line 113
def lap_markers
  "var pr_lap_markers = [\n" +
    "[ #{@session.start_position_long}," +
    " #{@session.start_position_lat} ], " +
    @activity.fit_activity.laps.map do |lap|
      "[ #{lap.end_position_long}, #{lap.end_position_lat} ]"
    end.join(', ') +
    "\n];\n"
end
style() click to toggle source
# File lib/postrunner/TrackView.rb, line 60
    def style
      <<EOT
.olControlAttribution {
    bottom: 5px;
}

.trackmap {
  width: 565px;
  height: 400px;
  border: 2px solid #545454;
}
EOT
    end
track_points() click to toggle source

Generate a javascript variable with an Array with the coordinates of the track points. Each coordinate is an Array with a longitude and latitude in EPSG:4326. Generate a javascript variable with an Array of track points.

# File lib/postrunner/TrackView.rb, line 93
def track_points
  points = []
  @activity.fit_activity.sessions.map do |session|
    session.records.map do |record|
      long = record.position_long
      lat = record.position_lat
      next unless long && lat

      points << "[ #{long}, #{lat} ]"
    end
  end

  "var pr_track_points = [\n" +
  points.join(', ') +
  "\n];\n"
end