apneadiving / Google-Maps-for-Rails

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

having trouble with partials & ujs #169

Closed syllogismus closed 12 years ago

syllogismus commented 12 years ago

im having trouble to get my map via partial right. my partial containing the map works just fine when i use it directly in the index view, but not when reloaded via ujs:

https://gist.github.com/2225389

what is my issue here?

tia

ksz2k commented 12 years ago

I had the same some time ago..

In your _resultsmap.html.erb put only gmaps({:last_map => false}) In index.js.erb (.erb !!) this JS code (it is the same code which is generated by gmaps4rails's content_for :scrpts, modify it to your needs):

Gmaps.map = new Gmaps4RailsGoogle();
Gmaps.load_map = function() {
    Gmaps.map.map_options.maxZoom = 15;
    Gmaps.map.initialize();
    Gmaps.map.markers = <%= @json.to_gmaps4rails %>;
    Gmaps.map.create_markers();
    Gmaps.map.adjustMapToBounds();
    Gmaps.map.callback();
};
  Gmaps.loadMaps();
apneadiving commented 12 years ago

seems clearly explained, thanks @ksz2k .

Maybe should it belong to the wiki as well?

ksz2k commented 12 years ago

Sure, if you think it's worth :) I can describe, just tell me where is ok - new page in JS / Ajax section would be ok?

apneadiving commented 12 years ago

Seems perfect :)

Sent from my iPhone

On 28 mars 2012, at 15:28, Krzysiek Szczukareply@reply.github.com wrote:

Sure, if you think it's worth :) I can describe, just tell me where is ok - new page in JS / Ajax section would be ok?


Reply to this email directly or view it on GitHub: https://github.com/apneadiving/Google-Maps-for-Rails/issues/169#issuecomment-4764532

syllogismus commented 12 years ago

if i add an js-alert prior to Gmaps.map.initialize(); it popsup, but not after. it seems not to initialize the map... what can i do? thx

ksz2k commented 12 years ago

Ah.. 'Hardly' add map's script inclusion tags in the <head> area before Rails standard <%= javascript_include_tag "application" %>.. Like this:

    <script type="text/javascript" src="//maps.google.com/maps/api/js?v=3.5&sensor=false&amp;libraries=geometry"></script>
    <script type="text/javascript" src="//google-maps-utility-library-v3.googlecode.com/svn/tags/infobox/1.1.9/src/infobox_packed.js"></script>
    <script type="text/javascript" src="//google-maps-utility-library-v3.googlecode.com/svn/tags/markerclustererplus/2.0.5/src/markerclusterer_packed.js"></script>
    <script type="text/javascript" src="//google-maps-utility-library-v3.googlecode.com/svn/trunk/richmarker/src/richmarker-compiled.js"></script>
syllogismus commented 12 years ago

ok, i got it working now by adding those js-fiels to the index file.

1) but why do i need these js-files additionally? if i call my partial directly from within the index view (without ujs) then it renders fine too 2) right now i have one marker like this (which is working): Gmaps.map.markers = [{"description": "This is a test.", "lng": "-74.006605", "lat": "40.714623"}];

, but this is not working: Gmaps.map.markers = <%= @json %>; where:

@json = @locations.to_gmaps4rails do |location, marker| marker.infowindow(render_to_string(:partial => "resultsmapinfowindow", :locals => { :object => location})).gsub(/\n/, '').gsub(/"/, '\"') marker.title "test" marker.sidebar "i'm the sidebar" marker.json "\"id\": #{location.id}" end

thx for all the help!!!

On Wed, Mar 28, 2012 at 3:46 PM, Krzysiek Szczuka < reply@reply.github.com

wrote:

Ah.. 'Hardly' add map's script inclusion tags in the <head> area.. Like this:

   <script type="text/javascript" src="//
maps.google.com/maps/api/js?v=3.5&sensor=false&amp;libraries=geometry
"></script>
   <script type="text/javascript" src="//
google-maps-utility-library-v3.googlecode.com/svn/tags/infobox/1.1.9/src/infobox_packed.js
"></script>
   <script type="text/javascript" src="//
google-maps-utility-library-v3.googlecode.com/svn/tags/markerclustererplus/2.0.5/src/markerclusterer_packed.js
"></script>
   <script type="text/javascript" src="//
google-maps-utility-library-v3.googlecode.com/svn/trunk/richmarker/src/richmarker-compiled.js
"></script>

Reply to this email directly or view it on GitHub:

https://github.com/apneadiving/Google-Maps-for-Rails/issues/169#issuecomment-4764992

apneadiving commented 12 years ago

1) It's because the script sources aren't loaded when you insert code with ajax. The js files should be added on the fly or added before the ajax call. I prefer the later.

2) did you try

<%=raw @json %>
apneadiving commented 12 years ago

Remark, in the latest versions, gsub isn't needed anymore

syllogismus commented 12 years ago

woha - raw works. thanks to all of you! appreciate it

On Wed, Mar 28, 2012 at 4:16 PM, Benjamin Roth < reply@reply.github.com

wrote:

Remark, in the latest versions, gsub isn't needed anymore


Reply to this email directly or view it on GitHub:

https://github.com/apneadiving/Google-Maps-for-Rails/issues/169#issuecomment-4765663

apneadiving commented 12 years ago

Please close the issue :)

Sent from my iPhone

On 28 mars 2012, at 16:21, tomreply@reply.github.com wrote:

woha - raw works. thanks to all of you! appreciate it

On Wed, Mar 28, 2012 at 4:16 PM, Benjamin Roth < reply@reply.github.com

wrote:

Remark, in the latest versions, gsub isn't needed anymore


Reply to this email directly or view it on GitHub:

https://github.com/apneadiving/Google-Maps-for-Rails/issues/169#issuecomment-4765663


Reply to this email directly or view it on GitHub: https://github.com/apneadiving/Google-Maps-for-Rails/issues/169#issuecomment-4765779

syllogismus commented 12 years ago

thanks again!