apneadiving / Google-Maps-for-Rails

Enables easy Google map + overlays creation in Ruby apps
https://apneadiving.github.io/
MIT License
2.27k stars 382 forks source link

rails asset-pipeline relative url for marker.picture in assets/images folder #471

Closed genlighten closed 9 years ago

genlighten commented 9 years ago

I'm trying to use a custom marker icon by setting marker.picture({ :url => "<%= asset_path('filename.png') %>" and I can't seem to get it to work (either in development or production).

Is it possible to use images from my app's /assets/images folder as custom marker icons? If not, can you help me understand how to access my images as a full url (https://appname.com/assets/filename.png doesn't seem to work for me in production.)

Thanks,

Dean

apneadiving commented 9 years ago

Did you try with height and width keys?

genlighten commented 9 years ago

Yup, here's my call to Gmaps4rails.build_markers:

        repositories_hash = Gmaps4rails.build_markers(repositories) do |repository, marker|
            marker.infowindow repository.name
            marker.lat repository.latitude
            marker.lng repository.longitude
            marker.picture({
                            :url => "https://addons.cdn.mozilla.net/img/uploads/addon_icons/13/13028-64.png",
                            :width => 24,
                            :height => 24
                            })

When I use the mozilla icon url given in the gmaps4rails example site, they appear no problem. When I try to switch to an image hosted on my development or production server using asset_path, no luck.

Thanks for your help...

--Dean

apneadiving commented 9 years ago

I cant tell Are you sure you pass: a valid picture + valid dimensions (beware if the picture is too big, it would take only a bit of it maybe transparent)

genlighten commented 9 years ago

Here's the actual code that fails:

        repositories_hash = Gmaps4rails.build_markers(repositories) do |repository, marker|
            marker.infowindow repository.name
            marker.lat repository.latitude
            marker.lng repository.longitude
            marker.picture({
                            :url => '<%= asset_path "repository_icon_for_map.png" %>',
                            :width => 48,
                            :height => 48
                            })

The file "repository_icon_for_map.png" is 48x48px and located in app/assets/images on my development box. On my staging server (accessible to you at https://new.genlighten.com/genealogists/chicago-illinois) the same file is in /public/assets. I pasted the image in this message and it's hopefully accessible at github as indicated below.

Thanks for your help!

--Dean

image

genlighten commented 9 years ago

Good news! This approach I found at StackOverflow seems to work well:

        repositories_hash = Gmaps4rails.build_markers(repositories) do |repository, marker|
            marker.infowindow repository.name
            marker.lat repository.latitude
            marker.lng repository.longitude
            marker.picture({
                            :url => ActionController::Base.helpers.asset_path("repository_icon_for_map.png"),
                            :width => 36,
                            :height => 36
                            })

The issue was, since I'm in a service object (.rb) instead of a view, I need to explicitly pull in the asset path helper. Doh!

--Dean

apneadiving commented 9 years ago

you were in a service object! ... :)

JonKernPA commented 9 years ago

Thanks for the tip!

ActionController::Base.helpers.asset_path