【问题标题】:Get current position with jquery ui map (gmap)使用 jquery ui map (gmap) 获取当前位置
【发布时间】:2017-08-17 15:00:47
【问题描述】:

我正在升级旧的混合应用程序。
现在我有最新版本的 jQuery (1.7 -> 2.2.4)jQuery Mobile (1.1 -> 1.4.5),我也使用 Jquery迁移 (1.4.1)。
此应用包含一个页面,其中包含使用 jquery-ui-map 创建的地图,我正在使用 PhoneGap 测试该应用。
在此页面中,我有一个按钮,我想获取用户的当前位置。
我使用此代码:

    $('#button-getcurrentposition').click(function(){

        $('#map-canvas').gmap('getCurrentPosition', function(position, status) {

            alert("I'm here");

            if ( status === 'OK' ) {
                var clientPosition = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
                $('#map-canvas').gmap('option','center', clientPosition);   
            }
        });         

    });

但警报“我在这里”从未显示,我不知道为什么,所以我无法获得用户的当前位置。
我已经包含了 jquery.ui.map.extensions.jsjquery.ui.map.overlays.jsjquery.ui.map.services.js。
我的网络应用版本和apk或ipa有同样的问题。
有人有同样的问题吗?有谁知道为什么不显示警报“我在这里”?最新版本的 jquery 或 cordova 是否已弃用函数“getCurrentPosition”?
非常感谢

【问题讨论】:

    标签: javascript android jquery cordova jquery-ui-map


    【解决方案1】:
        $('#button-getcurrentposition').click(function(){
    
            $('#map-canvas').gmap('getCurrentPosition', function(status, position) {
    
                alert("I'm here");
    
                if ( status === 'OK' ) {
                    var clientPosition = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
                    $('#map-canvas').gmap('option','center', clientPosition);   
                }
            }, { timeout: 4000, enableHighAccuracy: true });        
    
        });
    

    我相信这些 after 部分是必需的,并且根据文档,您在函数中使用了错误的方式。

    【讨论】:

    • 天啊!作品!太感谢了!但现在我有另一个问题。状态为“对象位置错误”。你对这个错误有所了解吗?谢谢
    • @Giu 那个错误是哪一行?在上面的代码上下文中
    • 变量“status”包含“object positionError”。如果我打印警报(状态);返回“位置错误”。代码: if ( status === 'OK' )
    • 如果无法检索到位置,status 将成为 positionError 对象。在status === 'OK'之前需要先检查status是否不是类的实例
    • mmmh,没关系...我不确定您所说的“检查状态是否不是类的实例”是什么意思...但是为什么无法检索位置?原因是什么?是代码问题还是我需要在 android manifest 中包含一些权限?
    猜你喜欢
    • 2014-05-20
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-01
    • 2015-11-15
    相关资源
    最近更新 更多