I followed the examples in the two locations:
but they both do not work. I get nothing back for either. I'm currently using the 1.5.5 version of gmaps4rails in RoR 3.2.8.
show_map.js.erb
$('#map_container').show();
$('#map_container').html('<%= escape_javascript( gmaps({:last_map => false}) ) %>');
Gmaps.map = new Gmaps4RailsGoogle();
Gmaps.load_map = function() {
Gmaps.map.map_options.maxZoom = 15;
Gmaps.map.initialize();
Gmaps.map.create_markers();
Gmaps.map.adjustMapToBounds();
Gmaps.map.markers = <%= @json %>;
Gmaps.map.callback();
};
Gmaps.loadMaps();
users_controller.rb
def show_map
@user = User.first
@json = @user.to_gmaps4rails
respond_to do |format|
format.js {}
end
end
show.html.haml
= link_to "Map", show_map_path, :remote => true
#map_container{:style => 'display:none;'}
Thanks for your help!
I have the same issue as @persistence
, and I prefer to report in this thread. I tried to follow both the wiki example first and then sticked to @apneadiving
comment and followed your gist, but still I'm missing something from the gist. I place this code in my controller:
events_controller
def index
@json = Event.all.to_gmaps4rails do |event, marker|
marker.title event.title
marker.infowindow event.description
marker.sidebar 'This is a side bar'
end
p @json
respond_with @json
end
And in my view I paste the exact code of the gist. When I push the load button I check in console that my json info is OK, but the map is not loading due to a Javascript error:
TypeError: Gmaps.map is undefined
I guess I'm missing some kind of Javascript initialization of the map?.
UPDATED 05/11/2012:
That was the problem indeed. Combining the javascript from both the gist and wiki example I made this work by putting the following code in the view:
index_view
<!-- create html + load js files but don't create map itself: will be done after ajax call -->
<%= gmaps({:last_map => false}) %>
<br/>
<!-- button to trigger ajax call -->
<button type="button" id="ajax">Load Map</button>
<script type="text/javascript" charset="utf-8">
$(function() {
//hide the empty container
$(".map_container").hide();
// Map initialization
Gmaps.map = new Gmaps4RailsGoogle();
Gmaps.load_map = function() {
Gmaps.map.map_options.maxZoom = 15;
Gmaps.map.initialize();
Gmaps.map.create_markers();
Gmaps.map.adjustMapToBounds();
Gmaps.map.callback();
};
$("#ajax").click(function(){
$.getJSON('/events', function(json){
$(".map_container").show();
Gmaps.loadMaps();
Gmaps.map.addMarkers(json);
})
})
});
</script>
I also left some simple examples of loading Gmaps4Rails maps in GitHub: https://github.com/daviddefco/Gmaps4RailsSamples.git
I'm a RoR and Javascript newbie, so comments or code improvements are welcome.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.