Search code examples
javascriptjquerygeolocation

geolocation.getCurrentPosition issue


I have an issue regarding geolocation.getCurrentPosition()

I have the following code:

$(document).on('click', '#find_pickupStoresNearMe_button', function (e) {
    e.preventDefault();
    var formAction = $(this).parents('form').attr('action');
    $('#locationForSearch').val('');
    navigator.geolocation.getCurrentPosition(function (position) {
        console.log(position);
        ACC.pickupinstore.locationSearchSubmit('', formAction, position.coords.latitude, position.coords.longitude);
    }, function (error) {
        console.log("An error occurred... The error code and message are: " + error.code + "/" + error.message);
    });
});

The problem is that the functions jumps straight into the function(error) because the position is undefined.I have the same code in a different folder for a different store and the html structure is the same and it works fine retrieving the City I request in a search input.

HTML code:

<form id="command" name="pickupInStoreForm" class="searchPOSForm clearfix" action="/store/store-pickup/pointOfServices"
    method="POST">
    <table>
        <tbody>
            <tr>
                <td>
                    <div class="control-group left">
                        <div class="controls">
                            <input type="text" name="locationQuery" id="locationForSearch" class="left" placeholder="Adauga Oras">
                        </div>
                    </div>
                </td>
                <td>
                    <button type="button" class="" id="pickupstore_search_button">Gaseste magazin</button>
                </td>
                <td>
                    <button type="button" class="" id="find_pickupStoresNearMe_button">Localizeaza pozitia pe harta</button>
                </td>
            </tr>
        </tbody>
    </table>
    <div>

I am new to javascript and jQuery, sorry if the question is poorly stated or if extra info is needed, let me know. Any help on where whould I look for the problem would be appreciated.


Solution

  • Check if these works.

    $(function($) {
        $('#pickupstore_search_button').on('click', (e) => {
          e.preventDefault();
          navigator.geolocation.getCurrentPosition((pos) => {
            console.log(pos)
          }, (err) => {
             console.error(err)
          });
        });
    });
    

    Otherwise, click on lock symbol in before URL( before https), go to the site settings and set Location to ask default or allow.

    enter image description here

    Maybe

    enter image description here