Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markers returning as Null #487

Closed
simkessy opened this issue Jul 28, 2015 · 2 comments
Closed

Markers returning as Null #487

simkessy opened this issue Jul 28, 2015 · 2 comments

Comments

@simkessy
Copy link

When I try to add the @hash to my javascript, it renders:

handler = Gmaps.build('Google');
handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
  markers = handler.addMarkers(null);
  handler.bounds.extendWith(markers);
  handler.fitMapToBounds();
  handler.getMap().setZoom(12);
});

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?

@apneadiving
Copy link
Owner

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

@simkessy
Copy link
Author

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

@simkessy simkessy closed this as completed Nov 5, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants