2012-01-23 106 views
2

這是一個Rails應用程序中的視圖 - 不幸的是我得到一個我無法修復的錯誤(「未捕獲的語法錯誤:意外的輸入結束」)。雖然我確信答案很簡單,但增加額外的});並不能解決問題。捕捉JavaScript「輸入的意外結束」

任何人有任何想法,我可能已經忘記關閉一些東西了嗎?

謝謝!

在HAML:

- content_for :before_head do 
    = javascript_include_tag "http://maps.google.com/maps/api/js?sensor=false" 


- content_for :page_javascript do 
    = javascript_include_tag "http://www.mapnificent.net/media/api/1/mapnificent.js" 
    :javascript 
    var MAP, HEATMAP, DATAPOINTS; 
    var mapnificent, urbanDistance, positions = {}; 
    $(document).ready(function() { 
     DATAPOINTS = {}; 
     var myLatlng = new google.maps.LatLng(40.75297891717686, -73.93936157226562); 

     var myOptions = { 
      zoom: 12, 
      center: myLatlng, 
      mapTypeId: google.maps.MapTypeId.ROADMAP, 
      disableDefaultUI: true, 
      scrollwheel: true, 
      draggable: true, 
      navigationControl: true, 
      mapTypeControl: false, 
      scaleControl: true, 
      disableDoubleClickZoom: false, 
      streetViewControl: false, 
      mapTypeControl: false, 
      overviewMapControlOptions: false, 
      styles: [ 
        { 
        featureType: "administrative", 
        elementType: "geometry", 
        stylers: [ 
         { visibility: "off" } 
        ] 
        },{ 
        featureType: "administrative.locality", 
        stylers: [ 
         { visibility: "on" } 
        ] 
        },{ 
        featureType: "transit.line", 
        stylers: [ 
         { visibility: "off" } 
        ] 
        },{ 
        elementType: "geometry", 
        stylers: [ 
         { saturation: -100 }, 
         { lightness: -100 }, 
         {gamma:0.24} 
        ] 
        },{ 
        featureType: "road.arterial", 
        elementType:"labels", 
        stylers: [ 
         { visibility: "simplified" }, 
      //  { saturation: -100 }, 
      //  {lightness:70}, 
      //  {hue: "#ffc243"} 
        ] 
        },{ 
        featureType: "road.highway", 
        elementType: "labels", 
        stylers: [ 
         { visibility: "simplified" }, 
      //  { saturation: -100 }, 
      //  {lightness:70}, 
      //  {gamma: 0}, 
        ] 
        },{ 
        featureType: "landscape", 
        elementType: "geometry", 
        stylers: [ 
         { lightness: 92 }, 
         { saturation: 20 }, 
         { hue: "#e2ded3" }, 
        ] 
        },{ 
        featureType: "road", 
        elementType: "geometry", 
        stylers: [ 
         { visibility: "simplified"}, 
         { saturation: 10 }, 
         { lightness: 100 }, 
         { hue: "#ffc243" } 
        ] 
        },{ 
        featureType: "water", 
        stylers: [ 
         { visibility: "simplified" }, 
         { lightness: 70 }, 
         { saturation: 15 }, 
         { hue: "#00ffdd" } 
        ] 
        },{ 
        featureType: "poi", 
        elementType: "geometry", 
        stylers: [ 
         { visibility: "on" }, 
         { saturation: 15 }, 
         { lightness: 89 }, 
         { hue: "#e2ded3" } 
        ] 
        },{ 
        featureType: "transit.station", 
        elementType: "geometry", 
        stylers: [ 
         { visibility: "simplified" }, 
         { lightness: 85 }, 
         { hue: "#e2ded3" }, 
         { saturation: 15 } 
        ] 
        }, 
        { 
        featureType: "poi.park", 
        elementType: "geometry", 
        stylers: [ 
         { hue: "#61bb00" }, 
         { lightness: -15}, 
         { saturation: 20}, 
         { visibility: "simplified" } 
        ] 
        },{ 
        featureType: "transit.station", 
        elementType: "labels", 
        stylers: [ 
         { hue: "#06bca9" }, 
         { saturation: -30}, 
         { lightness: -10}, 
         { visibility: "on" } 
        ] 
        } 
       ] 

      }; 

     MAP = new google.maps.Map(document.getElementById("map"), myOptions); 








     /* 
     This is the UrbanDistance's (i.e. the public transport layer) UI function. 
     This function gets called during the setup process and you can easily setup your UI in there 
     Parameters: 
      mapnificent: the mapnificent instance, responsible for how to draw on the map 
      that: the urbanDistance layer object, responsible for handling the public transport travel time calculation and drawing 
      $: jQuery for your convenience, scoped to this function, shortcut to window 
      undefined: the real undefined value, only 4 arguments are passed 

     */ 

     var UrbanDistanceUI = function(mapnificent, that, $, window, undefined){ 
      /* that.bind binds a function to an event of the layer 
       setup is called when the setup of the layer is complete 
       and before the loading of necessary data. 
      */ 
      that.bind("setup", function(){ 
       /* There are many options for the public transport layer 
       They can be read with .getOption and set with .setOption */ 
       var color = that.getOption("color"); 
       that.setOption("color", true); 
       /* you must display this copyright notice somewhere on your page */ 
       $("#copyright").html(that.getOption("copyright")); 
       console.log("setup done"); 
      }); 


      /* 
      loadProgress binds to the data loading progress, 
      you will receive one parameter: a percentage */ 
      that.bind("loadProgress", function(progress){ 

       $('#loading').text(progress); 
       if(progress >= 100){ 
        $('#loading').text("Almost done..."); 
       } 
      }); 

      /* 
      fires when the data has finished loading */ 
      that.bind("dataLoaded", function(){ 
       $('#loading').text("Done!"); 
       var geopos = {lat:40.75297891717686,lng:-73.93936157226562}, time = 30 * 60; 
       /* adds a position to the map at the specificed location. 
       Time can be left out for the default time (15 minutes) */ 
       var pos = that.addPosition(geopos, time); // time in seconds! 
       /* The call returns a position object that you can store. 
       You can store it by it's index pos.index */ 
       positions[pos.index] = {position: pos, latlng: geopos}; 
       /* You only created a position, you need to start the 
       calculation process explicitly */ 
       pos.startCalculation(); 
      }); 

      /* fires when a calculation has started, receives position as parameter */ 
      that.bind("calculationStarted", function(position){ 
       $('#loading').text("Starting calculation: 0.0%"); 
      }); 

      /* fires when there is a calculation progress, receives position as parameter */ 
      that.bind("calculationUpdated", function(position){ 
       /* estimating the progress is not an exact since 
       the option value "estimatedMaxCalculateCalls" is 
       an estimated upper bound of position.calculationProgress 
       */ 
       var percent = position.calculationProgress/
        that.getOption("estimatedMaxCalculateCalls") * 100; 
       $('#loading').text("Calculating: "+percent+"%"); 
      }); 

      /*fires when the calculation is done */ 
      that.bind("calculationDone", function(position){ 
       $('#loading').text("Calculation Done!"); 
       console.log("done"); 
       /* you need to trigger a redraw of the canvas yourself at appropriate moments */ 
       mapnificent.trigger("redraw"); 
       /* see getBlobs function further down */ 
       getBlobs(); 
      }); 

      var movePosition = function(index, pos){ 
       /* you can move your position with the .move method of a position 
       it takes an geo object {lat: X, lng:Y} as first parameter. 
       last parameter indicates if positionMoved should be triggered */ 
       var result = positions[index].position.move(pos, true); 
       /* result indicates if the position is in range of the current city data 
       if it is not, positionMoved has not been triggered, regardless of second parameter 
       it it is true, postionMoved has been triggered (it already executed) 
       the latlng field of the position is set regardless of anything 
       */ 
       if (!result){ 
        console.log("moved outside of range!"); 
       } 
      }; 
      /* triggered when a position moved inside the range */ 
      that.bind("positionMoved", function(pos){ 
       positions[pos.index].latlng = pos.latlng; 
       console.log("position move to ", pos.latlng); 
      }); 

      var removePosition = function(index){ 
       /* you can call removePosition on the UB layer with an index 
       to remove this position. This will also stop all ongoing calculations */ 
       that.removePosition(index); 
      }; 

      /* fires after successful removal */ 
      that.bind("positionRemoved", function(index){ 
       console.log("position removed"); 
       delete startPositions[index]; 
       mapnificent.trigger("redraw"); 
      }); 

      var getBlobs = function(){ 
       /* A blob is a currently visible*, highlighted area that is 
       separate from other highlighted areas. The blob contains the 
       maxbounds in canvas coordinates, and some other information */ 

       var blobs = that.search.detectBlobs(); 
       console.log(blobs); 
      }; 

     /* tried adding in a '}' here */  


















     google.maps.event.addListenerOnce(MAP, "idle", function() { 
     HEATMAP = new HeatmapOverlay(MAP, { 
      "radius": 30, 
      "visible": true, 
      "opacity": 60 
     }); 

     getDataPoints("theater", "http://doug.cartodb.com/api/v2/sql/?q=SELECT ST_x(geom) AS lng, ST_y(geom) AS lat, count(*) FROM (SELECT ST_SnapToGrid(the_geom,0.005) AS geom FROM v_theaters) AS foo GROUP BY lng, lat"); 
     getDataPoints("noise", "http://doug.cartodb.com/api/v2/sql/?q=SELECT ST_x(geom) AS lng, ST_y(geom) AS lat, count(*) FROM (SELECT ST_SnapToGrid(the_geom,0.005) AS geom FROM noise) AS foo GROUP BY lng, lat"); 
     getDataPoints("4sqDrug", "http://doug.cartodb.com/api/v2/sql/?q=SELECT ST_x(geom) AS lng, ST_y(geom) AS lat, count(*) FROM (SELECT ST_SnapToGrid(the_geom,0.005) AS geom FROM _4sq_15_32_47_drugstores_20or_20pharmacies) AS foo GROUP BY lng, lat");   
     }); 





     if(typeof Mapnificent !== "undefined" && Mapnificent.isBrowserSupported()){ 
      /* load Mapnificent options for these coordinates */ 
      Mapnificent.forCoordinates({lat:40.75297891717686,lng:-73.93936157226562}, function(options){ 
       if(options == null){ 
        /* if null, there is no Mapnificent data for this point */ 
        return; 
       } 
       /* if there are options, you can instantiate a new Mapnificent instance */ 
       mapnificent = Mapnificent(options); 
       /* add the "urbanDistance" layer with the UI function */ 
       mapnificent.addLayer("urbanDistance", UrbanDistanceUI); 
       /* you can bind to the initDone event of mapnificent */ 
       mapnificent.bind("initDone", function(){ 
        // mapnificent.hide(); 
       }); 
       /* finally add mapnificent to your map by passing the Google Maps instance */ 
       mapnificent.addToMap(map); 
      }); 
     } 





     /* tried removing a "});" from two lines below this comment */ 

    }); 

    function showHeatmap() { 
     var max = 0; 
     var all_points = []; 
     for(var key in DATAPOINTS) { 
     DATAPOINTS[key].rows.forEach(function(row) { 
      if (row.count > max) { 
      max = row.count 
      } 
     }); 
     all_points = all_points.concat(DATAPOINTS[key].rows); 
     } 

     var testData = { 
     max: max, 
     data: all_points 
     } 

     HEATMAP.setDataSet(testData); 
    } 

    function getDataPoints(name, url) { 
     $.get(url, function(data) { 
     DATAPOINTS[name] = data; 
     showHeatmap(); 
     }); 
    } 

%b 
    #loading 
    #copyright 

的錯誤出現在這裏這下一塊之前 - 未捕獲的語法錯誤:輸入

.map_holder{:style => "float: left; width: 600px; height: 600px;"} 
    #map{:style => "width: 100%; height: 100%; border: 1px solid #333;"} 
.side_bar{:style => "float: right;"} 
    %p SideBar 
    = label_tag "Noise Data" 
    = check_box_tag "Test" 
.cl{:style => "clear: both;"} 
+0

這是服務器端錯誤(Ruby/Haml)還是客戶端錯誤(瀏覽器中的JS)?如果是後者,那麼發佈你的Haml gobs是無用的:查看發送給瀏覽器的內容並向我們展示_that_代碼。 – Phrogz

回答

0

意外結束從使用的console.log()的,我接受它你現在沒有在IE中測試。但是,如果錯誤是在IE中,我會從中說行刪除尾隨逗號:

{ visibility: "simplified" }, 

在這樣的情況下,最好的調試技術,我知道是做了「二進制搜索」。刪除一半的代碼(當然,不要在一個會留下不匹配的括號或大括號的地方砍掉它),嘗試在瀏覽器中運行它,並查看是否仍然存在語法錯誤。重複,直到你縮小到一小段代碼。

+0

Hi @Alex,我一直在Firefox,Chrome和Safari中測試。 Firebug提供了比Chrome DevTools更多的幫助 - 我嘗試在我的評論('/ *嘗試刪除「})」之後更改該行;「從這個註釋* /'下面的兩行開始,並將它從'});'改成'}});'而現在頁面避免了錯誤,地圖上的圖層不會加載。 – Doug