navigator.geolocation.getCurrentPosition没有调用submit事件

时间:2015-01-05 14:34:52

标签: jquery cordova jquery-mobile gps

我正在使用Cordova(最新版本),jQuery Mobile(最新版本)项目,我需要在表单提交事件上调用navigator.geolocation.getCurrentPosition()函数。

它完全适用于deviceready事件,虽然我在表单上按提交时没有调用它。

以下是我在以下正文部分的代码:

    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" src="js/index.js"></script>
    <script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript" src="js/JQM.js"></script>//JQuery mobile library
    <script type="text/javascript" src="js/jq_validate.js"></script>
    <script type="text/javascript" src="js/gps_gcm.js"></script>

    <script type="text/javascript">

    document.addEventListener("deviceready", onDeviceReady, false);//This working perfactly
    function onDeviceReady() {          
        navigator.geolocation.getCurrentPosition(onSuccess1, onError1);//callback function included in "js/gps_gcm.js" file
    }

    $("#gps_loc").click(function() {
        alert("test gps");//I got this alert
        navigator.geolocation.getCurrentPosition(onSuccess1, onError1);//**callback function included in "js/gps_gcm.js" file but seems like this function is not calling?**
        alert("test gps end");//I also got this alert
    });

    $('#form1').validate({// using jquery library for validation form "js/jq_validate.js" included above
        rules: {
            f_name: {
                required: true
            },
            mob_no: {
                required: true,
                number: true
            },
            email: {
                required: true,
                email: true
            },
            merchant_name: {
                //required: true,
                number: true
            }        
        },
        messages: {
            f_name: {
                required: "Please enter your first name."
            },
            mob_no: {
                required: "Please enter your last name."
            },
            email: {
                required: "Please enter your email."
            },
            merchant_name: {
              //  required: "Please enter your email."
            }
        },
        errorPlacement: function (error, element) {
            error.appendTo(element.parent().prev());
        },
        submitHandler: function (form) {
            var category_val="";
            $(':checkbox:checked').each(function(i){

              category_val += $(this).val()+",";
              //alert(category_val);
            });
            var categoryies = category_val.slice(0,-1);
            //start all function calling one by one 
            alert(categoryies);//I got this alert
            navigator.geolocation.getCurrentPosition(onSuccess1, onError1);//**callback function included in "js/gps_gcm.js" file but seems like this function is not calling?**
            alert("end navigation");//I also got this alert


            return false;
        }
    });
    </script>

请告知并随时提出更多信息

感谢您的时间和提前考虑

诚恳, -Niks

1 个答案:

答案 0 :(得分:0)

我认为你的实现失败了,因为getCurrentPosition是异步的,你期望它是同步的。您可以通过跟踪设备的位置来解决此问题,并在使用navigator.geolocator.watchPosition提交的表单中使用此功能。

请务必仅在deviceready之后调用此方法以避免常量权限弹出窗口

以下是MDN文档https://developer.mozilla.org/en-US/docs/Web/API/Geolocation.watchPosition

或者,您可以将提交代码放在您为getCurrentPosition提供的回调中,但这意味着您必须使用jQuery.post或类似的东西自己执行POST。跟踪位置更清晰。