1

I have this code in my rails application. in this code calculate area of poly lines and i want to calculate sum of poly lines and save in a global variable and finally show that. but my global (s_area) var does not save data. how can I do this?

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)

2 Answers 2

0

So window.s_area is computed by display_on_map:

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

and display_on_map is called in the success callback of an AJAX call:

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'

and you're using s_area in a document-ready handler:

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

It is unlikely that the $.get AJAX calls will complete before the document-ready handler is triggered so s_area is still zero when you try to display it.

You could update the total as you calculate in display_on_map, something like this:

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

That at least should get things happening in the right order.


PS:

  • You can say window.s_area += area or just s_area += area.
  • You can use string interpolation: $.get "/tracksegments/#{id}.json", ... and "Sum of Area: #{s_area}".
1
  • thanks. that is great and work. and use your recommendations. I was searching for 6 hours for this solution. Commented Jul 18, 2013 at 17:52
0

This

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

needs to be this:

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

Reading up on how coffeescript does its scope will explain it.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.