Closed simkessy closed 8 years ago
not sure I understand why you have null
there.
You dont need to rely on data available only on page load, you can have:
handler = Gmaps.build('Google');
handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
someFunctionToGetMarkersJson().then(function(markersJSON){
markers = handler.addMarkers(markersJSON);
handler.bounds.extendWith(markers);
handler.fitMapToBounds();
handler.getMap().setZoom(12);
})
});
someFunctionToGetMarkersJson().then
could be, if you use jquery: $.getJSON(your params here).success
Seems very weird to have dynamic data in a js.erb: its rendered once, load your dynamic data with ajax
Wouldn't all this run on site load however, even with ajax? I don't see how I get away from @markers
not existing yet, until I reach the search controller. Unless, there's some sort of timer or a button to update the map on the results page.
The search is what determines what markers are going to be available. For example, in my Database I have Venues which are in London and others in New York. If the user searches for USA, the controller returns the geolocation of those Venues and creates the markers
This is my controller if that clears things up:
def index
@venues = Venue.near eval(params[:s]["geo"])
near_by_venues = @venues.to_a.map(&:id)
@search = Availability.joins{facility.venue.activities}
.where{
(facility.venue_id.in near_by_venues)
}
.order{start_time.desc}
@markers = Gmaps4rails.build_markers(@venues) do |venue, marker|
marker.lat venue.latitude
marker.lng venue.longitude
marker.infowindow venue.name
end
end
When I try to add the
@hash
to my javascript, it renders:I'm using maps with a search bar which is on every page. The search bar uses Google Places and sends the user to the Search Controller which is where I create the
@hash
. It seems like the JS in the ReadMe is created on site load, therefore the Markers are null.Right now I have all the JS in a Maps.js.erb file. Does the script need to be in the View so it's rendered at the same time as the partial or is there another workaround?