简体   繁体   English

将地理位置JavaScript变量传递给Rails无限重载

[英]Passing geolocation javascript variable to rails infinite reload

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. 该javascript应该可以找到用户,然后将这些变量传递到rails,以便它可以找到附近的Store模型位置。 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. 由于主体onload ='getLocation()'和.js文件中的window.location函数,它处于无限循环中。 Also the url is /findme. 网址也是/ findme。

controller 控制者

def show
    @nearby_stores = (params[:lat].present? && params[:lon].present?) ? Storeuser.near([params[:lat], params[:lon]], 50) : []
    respond_to do |format|
      format.html
    end
  end

view 视图

<body onload = 'getLocation()'>

    <div class = 'map'>
      <div id="map" style='width: 800px; height: 400px;'></div>
    </div>

    <div class= "test"></div>
    <h3>Nearby locations</h3>
    <ul id="nearby_locations">
        <% @nearby_stores.each do |storeuser| %>
        <li><%= link_to storeuser.storename, profile_path(storeuser) %>(<%= storeuser.distance.round(2) %> miles) </li>
        <% end %>
    </ul>
</body>

application.js application.js

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 + '&lon=' + lon);
        }

ok stupid me 好吧,愚蠢的我

a coworker fixed it for me 一位同事为我修理了它

if (!window.location.search) {
                window.location = ('/findme?lat=' + lat + '&lon=' + lon);    
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM