Adjust Zoom Gmaps4Rails - ruby-on-rails

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);
};

Related

Rails: gmaps4rails - Start the map location atlocation within the database

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.

Customize mapTypeControlOptions with gmaps4rails gem

Is it possible to customize how the map options are displayed on the google map?
It seems this is an option in the API using mapTypeControlOptions wiht an array of mapTypeIDs.
Thanks to apneadiving for developing this awesome gem!!!
You can add any option google map enables. From the doc here:
If you lack some options for maps and markers, you can simply pass
what you need in :raw:
<%= gmaps(:markers => {:data => #json, :options => { :raw => '{ animation: google.maps.Animation.BOUNCE }' } },
:map_options => { :raw => '{ disableDefaultUI: true, scrollwheel: false }' }) %>

How to add different type of markers in Gmaps4Rails

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.

Trouble displaying a custom marker using gmaps4rails

This gem is awesome for displaying a dynamic map. I am a beginner programmer, and I cannot seem to get a custom marker to display on the map.
I have the following in my model:
def gmaps4rails_marker_picture
{
"picture" => 'images/logo_avatar.png',
"width" => '16',
"height" => '16'
}
end
I have tried many combinations of the info provided in the github README, but to no avail. My map works perfectly until the above code gets plugged into the model....then all the markers go away. Is the sidebar code required in order to display a custom marker?
Here is one version of what I have in the controller:
#markers = Property.where(:id => propertyids).to_gmaps4rails
and the view:
<%= gmaps4rails(#markers) %>
I have tried passing in the image as an option, but could not get it to work.
Any help would be much appreciated!
At the controller level, do:
#json = Property.where(:id => propertyids).to_gmaps4rails do |property, marker|
marker.picture({
"picture" => path_to_image('/images/logo_avatar.png'),
"width" => '16',
"height" => '16'
})
end
It lets you use the assets tag helper.
Hey tbone I think I was having a similar issue and found the solution,
all I did was change it to this
marker.picture({
'picture' => view_context.image_path("green-dot.png"),
'width' => 32,
'height' => 32
})
the following code works for me, try it..
#json = Property.where(:id => propertyids).to_gmaps4rails do |property, marker|
marker.picture({
"picture" => 'assets/logo_avatar.png',
"width" => '16',
"height" => '16'
})
end

Google Maps, Ruby on Rails, Zoom level with one marker

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 }
})
%>

Resources