1

I'm trying to implement the HTML5 Geolocation when I click on the submit_tag in the following form. But the prompt for permission does not popup. Any idea why this is happening??

search_controller.rb

<%= form_for search_path, method: :get do |f| %>
    <p>
      <%= f.label "Search for" %>
      <%= text_field_tag :search, params[:search] %>
          <%= submit_tag "search", name: nil , :onclick => "getLocation();" %>
       </p>
    <% end %>


<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>
    var x = document.getElementById("demo");

    function getLocation() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(showPosition);
        } else {
            x.innerHTML = "Geolocation is not supported by this browser.";
        }
    }

    function showPosition(position) {
        // Do something with the coordinates

        x.innerHTML = "Latitude: " + position.coords.latitude +
            "<br>Longitude: " + position.coords.longitude;
    }
</script>

</body>
</html>


 <h3> Results:</h3>
    <% @las.each do |la| %>
       <li><%= link_to la.title, item %>
        <%= number_to_currency la.price %>
        (<%= la.distance.round(1) %> km) </li>
<% end %>
Dev
  • 437
  • 6
  • 25
  • The [code](https://codepen.io/LagartoSeb/pen/EwVVJz?editors=1010) works. Try in incognito mode. – Sebastián Palma Sep 16 '17 at 13:38
  • Thanks for the reply @Sebastián Palma... What do you mean by "incognito mode"? – Dev Sep 16 '17 at 13:42
  • [This](https://support.google.com/chrome/answer/95464?co=GENIE.Platform%3DDesktop&hl=en), and/or cleaning your cache/configuration. – Sebastián Palma Sep 16 '17 at 13:48
  • Where are you testing the code? If it's on a non https server that could be the issue, as the browsers are restricting geolocation to https only (http://caniuse.com/#feat=geolocation) | Also: any errors in the console? – yuriy636 Sep 16 '17 at 13:53
  • I'm using safari, so I opened a private window(if thats what you mean) but still no permissions popup @Sebastián Palma.... I also cleared the history as well. – Dev Sep 16 '17 at 13:54
  • Thanks for the reply @yuriy636.. Im testing on `localhost:3000`... nope no errors at all. – Dev Sep 16 '17 at 13:55
  • 1
    i think it may be an issue with how you are attaching the event listener (to the button's click and not the form's submit), and also maybe that you aren't calling preventDefault from the handler. See https://stackoverflow.com/a/7410112/2981429 – max pleaner Sep 16 '17 at 17:25

0 Answers0