我的 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)