<script>
  (function(map) {
    <%
       if event.payload[:horizontal_accuracy] && event.payload[:vertical_accuracy]
         radius = (event.payload[:horizontal_accuracy].to_f + event.payload[:vertical_accuracy].to_f) / 2.0
       elsif event.payload[:horizontal_accuracy]
         radius = event.payload[:horizontal_accuracy].to_f
       elsif event.payload[:vertical_accuracy]
         radius = event.payload[:vertical_accuracy].to_f
       elsif event.payload[:accuracy]
         radius = event.payload[:accuracy].to_f
       else
         radius = 0
       end
    %>

    var pos = new google.maps.LatLng(<%= event.lat %>, <%= event.lng %>);

    <% if radius > 0 %>
      var accuracyCircle = new google.maps.Circle({
        strokeColor: '#FF0000',
        strokeOpacity: 0.8,
        strokeWeight: 2,
        fillColor: '#FF0000',
        fillOpacity: 0.35,
        map: map,
        center: pos,
        radius: <%= radius %>
      });
    <% else %>
      var marker = new google.maps.Marker({
        position: pos,
        map: map,
        title: 'Recorded Location'
      });
    <% end %>


    <% if event.payload[:course] && event.payload[:course].to_f > -1 %>
      var p1 = new LatLon(pos.lat(), pos.lng());
      var p2 = p1.destinationPoint(<%= event.payload[:course].to_f %>, <%= [0.2, (event.payload[:speed] || 1).to_f].max * 0.1 %>);

      var lineCoordinates = [ pos, new google.maps.LatLng(p2.lat(), p2.lon()) ];

      var lineSymbol = {
        path:google.maps.SymbolPath.FORWARD_CLOSED_ARROW
      };

      var line = new google.maps.Polyline({
        path: lineCoordinates,
        icons: [
          {
            icon: lineSymbol,
            offset: '100%'
          }
        ],
        map: map
      });
    <% end %>
  })(map);
</script>