I am adding google maps support with apneadiving / Google-Maps-for-Rails (thanks awesome gem)
I am finding one slight glitch, however, which very likely is my fault.
auto_zoom works great when there are multiple markers. However, when there is only one marker it is zoomed in to the max level which is not pretty.
"zoom" will only work when auto_zoom is false, so that's not what I want.
So therefore you could use "maxZoom" but now users cannot zoom in manually beyond that point which is not what I want.
Is there a way around this? Is my explanation making sense? Is this a limitation of Google Maps API?
Thanks...
This behavior is due to the auto_zoom built-in function in the google maps api.
One work around to this is to set it to false in the gmaps method:
<%= gmaps({
"map_options" => { "auto_zoom" => false},
"markers" => { "data" => #json }
})
%>
And then use the gmaps4rails_callback to fit your needs (be sure to have at least version 0.7.9)
<script type="text/javascript" charset="utf-8">
function gmaps4rails_callback() {
if (Gmaps4Rails.markers.length == 1) {
//only one marker, choose the zoom level you expect
Gmaps4Rails.map.setZoom(2);
}
else{
//more than one marker, let's auto_zoom
Gmaps4Rails.map_options.auto_zoom = true;
Gmaps4Rails.adjust_map_to_bounds();
}
}
</script>
I achieved this in a slightly different way as I know that I'll only ever have one marker on my map. I'm relatively new to rails, but this method feels a bit "cleaner" than using JS in your view.
I've got lat and lng stored in my model (encoded by geokit at time of creation), so did the following in my view:
<%= gmaps({
"map_options" => {"auto_zoom" => false, "zoom" => 15, "center_latitude" => #listing.lat, "center_longitude" => #listing.lng },
"markers" => {"data" => #markers }
})
%>
#markers is my JSON created by blah.to_gmaps4rails, and "listing" is my model.
thanks this helped me...
{"auto_zoom" => false, "zoom" => 15, "center_latitude" => #listing.lat, "center_longitude" => #listing.lng },
"markers" => {"data" => #markers }
})
%>
Related
I dont think there is need for me to post specific code from my application.
In simple terms I have a db table with name,address,longitude,latitude,gmaps.
Each record stores a location. Okay. When I load the page, the google maps just starts off in the middle of the atlantic ocean, I was wondering is there a way for it to start off on one of my locations that I already have in my table?
Hey when you pass a record to the view you can use it to center the map.
<%= gmaps({
"map_options" => { "auto_adjust" => false, "center_longitude" => #record.long, "center_latitude" => #record.lat, "auto_zoom" => false, "zoom" => 5 },
"markers" => {"data" => #json }
})
%>
When you try this, make sure you have set auto_adjust to false.
I need to show two different markers in my Google map. I am using rails 3 and Gmaps4rails gem.
I my controller I have
#marker1 = User.find(1)
#marker2 = User.find(2)
#json = [#marker1,#marker2].to_gmaps4rails
In view file
<%= gmaps({
"map_options" => { "zoom" => 12, "auto_adjust" => false, "center_latitude" => #marker1.lat, "center_longitude" => #marker1.lng},
"markers" => { "data" => #json }
})
%>
<%= yield :scripts %>
The map view I get is
I need to add different marker images for the each of them. How can this be done. please help.
I have simply one answer: it's explained in the wiki, within the Customize each marker section.
There is also some alternative to add the styles in a block from your controller instead of model level.
I am unable to get a Javascript callback to work with gmaps4_rails when the map has loaded or is in the 'idle' state.
Based on the wiki and this other SO question Dynamically load Google Maps Markers with gmaps4rails I thought this would fire all of the alerts in this view:
<%= gmaps(:map_options => {:detect_location => true,
:center_on_user => true,
:auto_zoom => true,
:zoom => 12,
:auto_adjust => false},
:markers => {:data => #json,
:options => {:processsing => "#json",
:list_container => "markers_list",
:do_clustering => true
} }) %>
<ul id="markers_list"> </ul>
<script type="text/javascript" charset="utf-8">
alert("About to register callback");
Gmaps.map.callback = function() {
alert("Map Loaded Callback Reached");
}
function gmaps4rails_callback() {
alert("gmaps4_rails callback");
}
</script>
<%= yield :scripts %>
Instead I only get the "about to register callback" alert. The other functions never seem to be called. I have a feeling I'm misunderstanding something about the namespace but any insight would be appreciated- I'm trying to get things setup to refresh from a JSON endpoint as the map moved but I'm not having a lot of luck getting past this first step of making sure I can trigger a callback.
I should note the map appears normally and my initial data is all there.
As per documentation here, you must wrap the js code in a content_for :scripts
Why?
because gmaps4rails js code should be loaded first.
i've got confused trying to change the map controls like said here But don`t know how to implement using gmaps4rails gem.
I just wanted to leave only zoom and pan control and remove all the others. But js console says for:
>> Gmaps.map.map.mapTypeControl
>> false
But it is still present on a map.
You can use a raw parameter to send any map option you need. See doc.
I guess, you should do:
<%= gmaps(:map_options => { :raw => '{ panControl: true,
zoomControl: true,
mapTypeControl: false,
scaleControl: false,
streetViewControl: false,
overviewMapControl: false}'
},
:markers => { :data => #json }
)%>
If you are initialising with javascript (not with <%= gmaps %>), use
Gmaps.search_map.map_options.raw.streetViewControl = false;
More options here
I'm using the Gmaps4Rails gem and cannot figure out how to adjust the default zoom settings. In my view I have the following code:
<%= gmaps({
"map_options" => {"auto_adjust" => false, "auto_zoom" => false, "zoom" => 15 },
"markers" => {"data" => #json }
})
%>
I know this has been asked and answered many times, but maybe I'm just not seeing something...any advice? Am i missing something completely obvious? I apologize for reposting this question.
Thanks, Kevin
EDIT
I failed to properly comment out the default instance of gmaps and it overrode my customized settings. Thanks for all the help #apneadiving!
You're simply missing that seeing the whole map means providing a very small number, not a big one!
<%= gmaps({
"map_options" => {"auto_adjust" => false, "zoom" => 0 },
"markers" => {"data" => #json }
})
%>
I'm on gmaps4rails 1.3.2 and these are the best settings I could find:
<%= gmaps({
"map_options" => {"auto_zoom" => false, "zoom" => 10 },
"markers" => {"data" => #json }
})
%>
I did not set auto_adjust to false, because the map would not be centered on the marker anymore.
The 10 value for the zoom is nice to display approximatively a 50km-wide map.
what helped me in this case was this:
Gmaps4Rails.callback = function() {
setTimeout("Gmaps4Rails.map.setZoom(13);", 100);
};