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
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.
How do pass color parameter to google maps api using google-maps-for-rails? I would like to set color of each marker inside controller based on a value, so some would be red, some yellow and some green. I believe it is the icon property, in Symbol, looking at:
https://developers.google.com/maps/documentation/javascript/reference#MarkerOptions
Also, I would like to have a number from 1-99 inside the marker, is it possible? So far I have this.
#json = Device.all.to_gmaps4rails do |device, marker|
end
I have been struggling with this for days, any help would be appreciated.
You should simply leverage the google's chart api.
Example, the following is a marker with:
letter A
red color: FF0000
black text: 000000
http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=A|FF0000|000000
So you should customize your needs:
#json = Device.all.to_gmaps4rails do |device, marker|
marker.picture({
:picture => "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=A|FF0000|000000", # up to you to pass the proper parameters in the url, I guess with a method from device
:width => 32,
:height => 32
})
end
I implemented the above solution to no avail, but now figured out why this wasn't working. Perhaps this was an update to the gem, but "url" is the right key for the URL of the picture, not "picture". Here was my code in my controller:
#devices_hash= Gmaps4rails.build_markers(#devices) do |device, marker|
...
marker.picture({
:url => "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=A|007FFF|000000",
:width => 32,
:height => 32
})
end
Hope this helps some people struggling with the same thing.
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'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);
};
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 }
})
%>