开发者

Gmaps4Rails.clear_marker TypeError: Cannot read property 'serviceObject' of null

开发者 https://www.devze.com 2023-03-26 16:47 出处:网络
I have been updating my Gmaps4Rails dynamically by waiting until a user has stopped typing in their search form for 0.5s then sending an AJAX request. However, after 2 or 3 requests the map stops upda

I have been updating my Gmaps4Rails dynamically by waiting until a user has stopped typing in their search form for 0.5s then sending an AJAX request. However, after 2 or 3 requests the map stops updating after Gmaps4Rails.replaceMarkers with a Javascript error of

TypeError: Cannot read property 'serviceObject' of null

which I have tracked down to Gmaps4Rails.clear_marker:

Gmaps4Rails.clearMarker = function(marker) {
  if (marker!=null) { // <-- I've added this line to make it work
    marker.serviceObject.setMap(null);
  }                   // <-- I've added this line to make it work
};

Is this something that needs patching or am I doing something wrong elsewhere?

Here's the relevant section from my application.rb

$('.venue_find').live('keyup', function() {
    window.clearTimeout(window.timeOutId);
    window.timeOutId = window.setTimeout(function() {
    $.get('/venues/find?address='+$('#venue_address').val()+","+$('#venue_city').val()+","+$('#venue_state').val()+","+$('#venue_postal_code').val()+","+$('#venue_country').val());
    },500); 
});

The map is set up as:

<div id="venue_form">
  <div id="venue_map">
    <%= gmaps("map_options"=>{"centre_latitude"=>51.500181,"centre_longitude"=>-0.12619,"maxZoom" => 16, "auto_zoom"=>true},"markers"=>{"data"=>@json}) %>
  <div id="addresses">
  </div>
</div>

Here's the relevant stuff from */venues/_form.html.erb*

<%= form_for(@venue, :remote=>true) do |f| %>
    <div class="field">
        <%= f.label :address %><br />
        <%= f.text_field :address, :class=>"venue_find" %>
    </div>开发者_Python百科;
    <div class="field">
        <%= f.label :city %><br />
        <%= f.text_field :city, :class=>"venue_find" %>
    </div>
    <div class="field">
        <%= f.label :state %><br />
        <%= f.text_field :state, :class=>"venue_find" %>
    </div>
    <div class="field">
        <%= f.label :postal_code %><br />
        <%= f.text_field :postal_code, :class=>"venue_find" %>
    </div>
    <div class="field">
        <%= f.label :country %><br />
        <%= f.text_field :country, :class=>"venue_find" %>
    </div>
<% end %>

Here's venues_controller.rb

def find
  if !params[:address].nil? && params[:address]!=""
    begin
      addresses=Gmaps4rails.geocode(params[:address])
      @json = "[" + addresses.map {|a|  "{\"lng\": \"#{a[:lng]}\",\"lat\": \"#{a[:lat]}\"} " }.join(",")+"]"
      @matched_addresses = addresses.map{|a| a[:matched_address]}.join("<br/>")
    rescue Gmaps4rails::GeocodeStatus
      @json=[]
      @matched_addresses=[]
    end
  else
    @json=[]
  end
  respond_to do |format|
    format.js
  end
end

And finally here's find.js.erb

$('#addresses').empty();
$('#addresses').html('<%=pluralize(@matched_addresses.length,'result')%><br/><%=raw @matched_addresses %>');
Gmaps4Rails.replaceMarkers(<%= raw @json %>);


I can't reproduce your bug and I can't even understand why your fix is useful :)

Actually, in clearMarkers, there is a loop on all existing markers:

for (var i = 0; i < this.markers.length; ++i) {
  this.clearMarker(this.markers[i]);
}

So if there is no marker, there is no loop...

Could you provide more context?

I posted this long comment as an answer because I'm almost the only one to answer this tag

0

精彩评论

暂无评论...
验证码 换一张
取 消