我收到错误this.clusterer undefined 。 您可以在下面看到我的配置。
预期行为:加载页面,而不是使用#search_form定义的查询从控制器加载标记。 最后将标记添加到#map并将它们聚类。
我的js.coffee
$ -> handler = Gmaps.build('Google'); handler.buildMap { provider: {center: new google.maps.LatLng(49.639177, 9.536133),zoom: 6}, internal: {id: 'map'}}, ()-> handler._createClusterer() markers = [] placeMarkers = (data, textStatus, jqXHR) -> handler.removeMarkers(markers); markers = handler.addMarkers(data) # <<--- here is the error $("#search_form").submit (e) -> valuesToSubmit = $(this).serialize $.ajax({ url: $(this).attr("action"), data: valuesToSubmit, success: placeMarkers, dataType: 'json' }) e.preventDefault $("#search_form").submit()和控制器:
def index respond_to do |format| format.html {} format.json { seminars = Seminar.search(params[:search]) @markers = Gmaps4rails.build_markers(seminars) do |seminar, marker| return if seminar.location.is_a? String marker.lat seminar.location.lat marker.lng seminar.location.lon marker.infowindow render_to_string(:partial => 'seminars/seminar.html.haml', :locals => {seminar: seminar}) marker.title seminar.course.title marker.json({ :id => seminar.id }) end render json: @markers } end end以及控制器的响应:
[{"lat":52.517,"lng":13.3889,"marker_title":"Title1","id":1}, {"lat":51.5114,"lng":7.46517,"marker_title":"Title2","id":3}]这是堆栈跟踪
Gmaps.Objects.Handler.Handler.addMarker (application.js:22417) (anonymous function) (application.js:22409) _.map._.collect (application.js:21094) Gmaps.Objects.Handler.Handler.addMarkers (application.js:22408) placeMarkers (application.js:23263) jQuery.Callbacks.fire (application.js:3049) jQuery.Callbacks.self.fireWith (application.js:3161) done (application.js:8236) jQuery.ajaxTransport.send.callback (application.js:8779)版本gmaps4rails-2.1.1
I receive the error this.clusterer is undefined. You can see my configuration below.
Expected behaviour: Load the Page, than AJAX-load the Markers from the controller using the query defined in #search_form. Finally add the markers to the #map and cluster them.
my js.coffee
$ -> handler = Gmaps.build('Google'); handler.buildMap { provider: {center: new google.maps.LatLng(49.639177, 9.536133),zoom: 6}, internal: {id: 'map'}}, ()-> handler._createClusterer() markers = [] placeMarkers = (data, textStatus, jqXHR) -> handler.removeMarkers(markers); markers = handler.addMarkers(data) # <<--- here is the error $("#search_form").submit (e) -> valuesToSubmit = $(this).serialize $.ajax({ url: $(this).attr("action"), data: valuesToSubmit, success: placeMarkers, dataType: 'json' }) e.preventDefault $("#search_form").submit()And the Controller:
def index respond_to do |format| format.html {} format.json { seminars = Seminar.search(params[:search]) @markers = Gmaps4rails.build_markers(seminars) do |seminar, marker| return if seminar.location.is_a? String marker.lat seminar.location.lat marker.lng seminar.location.lon marker.infowindow render_to_string(:partial => 'seminars/seminar.html.haml', :locals => {seminar: seminar}) marker.title seminar.course.title marker.json({ :id => seminar.id }) end render json: @markers } end endAnd the response from the controller:
[{"lat":52.517,"lng":13.3889,"marker_title":"Title1","id":1}, {"lat":51.5114,"lng":7.46517,"marker_title":"Title2","id":3}]Here is the stacktrace
Gmaps.Objects.Handler.Handler.addMarker (application.js:22417) (anonymous function) (application.js:22409) _.map._.collect (application.js:21094) Gmaps.Objects.Handler.Handler.addMarkers (application.js:22408) placeMarkers (application.js:23263) jQuery.Callbacks.fire (application.js:3049) jQuery.Callbacks.self.fireWith (application.js:3161) done (application.js:8236) jQuery.ajaxTransport.send.callback (application.js:8779)Version gmaps4rails-2.1.1
最满意答案
很奇怪,我再也无法重现了......
我认为更改是将$("#search_form").submit()到onMapLoad处理程序。
$ -> handler = Gmaps.build('Google'); handler.buildMap { provider: {center: new google.maps.LatLng(49.639177, 9.536133),zoom: 6}, internal: {id: 'map'}}, ()-> $("#search_form").submit()谢谢您的提示@apneadiving
Very strange, I can't reproduce it any more...
I think the change was to add the $("#search_form").submit() to the onMapLoad handler.
$ -> handler = Gmaps.build('Google'); handler.buildMap { provider: {center: new google.maps.LatLng(49.639177, 9.536133),zoom: 6}, internal: {id: 'map'}}, ()-> $("#search_form").submit()Thank you for your hint @apneadiving
更多推荐
发布评论