javascript - Filter markers to display on google maps from search results -
i'd display on google maps markers after having search database elements. database made of restaurants latitude, longitude, address , restaurant's title. user searches restaurant based on city name, i'd display corresponding markers.
my problem can't figure out how link search results google markers. idea ?
hereafter code :
index.html.erb
<div id="map"></div> <% content_for(:after_js) %> <script> $(function() { var handler = gmaps.build('google'); handler.buildmap({ internal: { id: 'map' } }, function() { markers = handler.addmarkers(<%= raw @hash.to_json %>); handler.bounds.extendwith(markers); handler.fitmaptobounds(); if (markers.length == 0) { handler.getmap().setzoom(2); } else if (markers.length == 1) { handler.getmap().setzoom(14); } }); }); </script> <% end %>
places_controller.rb
class placescontroller < applicationcontroller def index @places = place.all if params[:search].nil? @places = place.where.not(latitude: nil, longitude: nil) else @search = params[:search] @places = place.search(@search).order("created_at desc") end @hash = gmaps4rails.build_markers(@places) |place, marker| marker.lat place.latitude marker.lng place.longitude marker.title place.title end end end
place.rb
class place < applicationrecord validates_presence_of :title, :address geocoded_by :address after_validation :geocode, if: ->(obj){ obj.address.present? , obj.address_changed? } def self.search(search) where("address ?", "%#{search}%") end end
Comments
Post a Comment