1

我的 Rails 应用程序中有此代码。在这段代码中计算折线的面积,我想计算折线的总和并保存在一个全局变量中,最后显示出来。但我的全局 (s_area) 变量不保存数据。我怎样才能做到这一点?

window.s_area = 0

jQuery ->
  $('#track_color').minicolors()


gm_init = ->
  gm_center = new google.maps.LatLng()#remove center lat lng

  gm_map_type = google.maps.MapTypeId.HYBRID
  map_options = {
    zoom: 8,
    center: gm_center,
    mapTypeId: google.maps.MapTypeId.HYBRID
  }
  new google.maps.Map(@map_canvas,map_options);



load_track = (id,map, info, point_data, name_data, ayear) ->
  callback = (data) -> display_on_map(data,map, id, info, point_data, name_data, ayear)
  $.get '/tracksegments/'+id+'.json', {}, callback, 'json'


display_on_map = (data,map, id, info, point_data, name_data, ayear) ->
  decoded_path = google.maps.geometry.encoding.decodePath(data.polyline)
  if ayear == '1'
    color = '#00FA15'
  else if ayear == '2'
    color = '#FA0400'
  else if ayear == '3'
    color = '#0532FA'
  #alert color
  path_options = { path: decoded_path, strokeColor: color , strokeOpacity: 0.5, strokeWeight: 4}
  track_path = new google.maps.Polyline(path_options)
  gm_path = track_path.getPath()
  area = google.maps.geometry.spherical.computeArea(gm_path)
  area = Math.round(area, 2)
  $("#area-val" + id).append(area)
  $("#show-area-val" + id).append(area + ' متر مربع')
  window.s_area = window.s_area + area

  if state != 2
    myCenter=new google.maps.LatLng(point_data[0][0], point_data[0][1])
  else
    myCenter=new google.maps.LatLng(point_data[0], point_data[1])
  marker=new google.maps.Marker({position:myCenter, map:map});
  track_path.setMap(map)
  infowindow = new google.maps.InfoWindow({content:'' + '<strong>' + 'شناسه: ' + '</strong>' + '<a href="/tracksegments/'+ '' + id + '' +'/edit">' +''+ info + '' +'</a>' + '<br/>' + '<strong>' + 'نام زمین: ' + '</strong>' + name_data + '' + '<br/>' + '<strong>' + 'مساحت به متر مربع: ' + '</strong>' + area + ''})
  #infowindow.open(map,marker) 
  google.maps.event.addListener(marker, 'click', -> (infowindow.open(map,marker)))
  map.fitBounds(calc_bounds(track_path));

calc_bounds = (track_path) ->
  b = new google.maps.LatLngBounds()
  gm_path = track_path.getPath()
  path_length = gm_path.getLength()
  i = [0,(path_length/3).toFixed(0),(path_length/3).toFixed(0)*2]
  b.extend(gm_path.getAt(i[0]))
  b.extend(gm_path.getAt(i[1]))
  b.extend(gm_path.getAt(i[2]))


#$ ->
 # map = gm_init()
  #load_track(js_track_id2,map)

$ -> 

    if state == 2
      map = gm_init()
      load_track(js_track_id2,map,info_data, point_data, name_single, ayear_single)
    else
      map = gm_init()
      ages = {}
      ages = js_track_id
      #for l,v of ages
       # load_track(v,map,v)
      for i of ages
        load_track(js_track_id[i],map,info_data[i], point_data[i], name_data[i], ayear_data[i])

$ ->      
  $('#total').append('Sum of Area: '+s_area)
4

2 回答 2

0

这个

$('#total').append('Sum of Area: '+s_area)

需要是这样的:

$('#total').append('Sum of Area: '+window.s_area)

阅读coffeescript 如何实现其范围将对其进行解释。

于 2013-07-18T16:39:47.030 回答
0

因此window.s_area由 计算display_on_map

display_on_map = (data,map, id, info, point_data, name_data, ayear) ->
  #...
  window.s_area = window.s_area + area

display_on_map在 AJAX 调用的成功回调中调用:

load_track = (id,map, info, point_data, name_data, ayear) ->
  callback = (data) -> display_on_map(data,map, id, info, point_data, name_data, ayear)
  $.get '/tracksegments/'+id+'.json', {}, callback, 'json'

并且您正在s_area文档就绪处理程序中使用:

$ ->      
  $('#total').append('Sum of Area: '+s_area)

$.get在触发文档就绪处理程序之前,AJAX 调用不太可能完成,因此s_area当您尝试显示它时仍然为零。

您可以在计算时更新总数display_on_map,如下所示:

window.s_area = window.s_area + area
$('#total').html("Sum of Area: #{window.s_area}")

这至少应该让事情以正确的顺序发生。


PS:

  • 你可以说window.s_area += area或只是s_area += area
  • 您可以使用字符串插值:$.get "/tracksegments/#{id}.json", ..."Sum of Area: #{s_area}".
于 2013-07-18T17:26:37.257 回答