title: Passing geolocation javascript variable to rails infinite reload
body: <p>Ok so I am trying to create a map that is on one page. The javascript is supposed to locate the user and then pass those variables to rails so it can find nearby Store model locations. I cannot seem to figure out what I need to do to get it to work. It is in an infinite loop thanks to the body onload = 'getLocation()' and the window.location function in the .js file. Also the url is /findme. </p>
  
  <p>controller</p>
  
  <pre><code>def show
      @nearby_stores = (params[:lat].present? &amp;&amp; params[:lon].present?) ? Storeuser.near([params[:lat], params[:lon]], 50) : []
      respond_to do |format|
        format.html
      end
    end
  </code></pre>
  
  <p>view</p>
  
  <pre><code>&lt;body onload = 'getLocation()'&gt;
  
      &lt;div class = 'map'&gt;
        &lt;div id="map" style='width: 800px; height: 400px;'&gt;&lt;/div&gt;
      &lt;/div&gt;
  
      &lt;div class= "test"&gt;&lt;/div&gt;
      &lt;h3&gt;Nearby locations&lt;/h3&gt;
      &lt;ul id="nearby_locations"&gt;
          &lt;% @nearby_stores.each do |storeuser| %&gt;
          &lt;li&gt;&lt;%= link_to storeuser.storename, profile_path(storeuser) %&gt;(&lt;%= storeuser.distance.round(2) %&gt; miles) &lt;/li&gt;
          &lt;% end %&gt;
      &lt;/ul&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>application.js</p>
  
  <pre><code>var x = document.getElementById("map");
          function getLocation()
          {
              if (navigator.geolocation)
              {
                  navigator.geolocation.getCurrentPosition(showPosition);
              }
              else{x.innerHTML = "Geolocation is not supported by this browser.";}
          }
          function showPosition(position)
          {
              handler = Gmaps.build('Google', { markers: { maxRandomDistance: null } });
              handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
                  markers = handler.addMarkers([
                      {
                          "lat": position.coords.latitude,
                          "lng": position.coords.longitude,
                          "picture": {
                              "url": "https://addons.cdn.mozilla.net/img/uploads/addon_icons/13/13028-64.png",
                              "width":  36,
                              "height": 36
                          },
                          "infowindow": "You!"
                      }
                  ]);
                  handler.bounds.extendWith(markers);
                  handler.fitMapToBounds();
              });
              var lat = position.coords.latitude
              var lon = position.coords.longitude
              var parsed_data = {location: {lat: lat, lon: lon}}
              window.location = ('/findme?lat=' + lat + '&amp;lon=' + lon);
          }
  </code></pre>