【问题标题】:Phonegap GPS location plugin cordova-plugin-gpslocationPhonegap GPS定位插件cordova-plugin-gpslocation
【发布时间】:2020-05-26 09:44:28
【问题描述】:

我正在尝试获取 gps 坐标以在我的手机中移动谷歌地图。我的 xml 文件中添加了这样的插件:

<plugin name="cordova-plugin-gpslocation" spec="1" />

当我单击按钮获取坐标时,此代码运行:

 function locateMe(){
        alert("in function");

    function onSuccess(position) {
        alert(position.coords.latitude);
        alert(position.coords.longitude);

    function onError(error) {
        alert('code: '    + error.code    + '\n' +
              'message: ' + error.message + '\n');
        }
    //locateMe code
    var watchID = GPSLocation.getCurrentPosition(onSuccess, onError);


    //get gps coordinate


    //move map to location   

    }


}

我正在使用的gps插件:https://www.npmjs.com/package/cordova-plugin-gpslocation

我的应用似乎可以识别该插件已添加,因为当我将它安装在我的 Android 手机上时,它会警告我有关 gps 权限的信息。

当我运行上面的代码时,我只收到in fucntion 的第一个警报,我不确定我没有收到 gps 坐标的警报。

【问题讨论】:

    标签: android cordova gps phonegap-plugins phonegap-build


    【解决方案1】:

    尝试使用官方的 Cordova 插件 访问:https://cordova.apache.org/docs/en/latest/guide/cli/index.html

    安装: 这需要cordova 5.0+(当前稳定的1.0.0)

    cordova 插件添加cordova-plugin-geolocation

    旧版本的 Cordova 仍然可以通过已弃用的 id(陈旧的 0.3.12)安装

    cordova 插件添加 org.apache.cordova.geolocation

    也可以直接通过repo url安装(不稳定)

    cordova 插件添加https://github.com/apache/cordova-plugin-geolocation.git

    要在按钮点击时获取设备当前位置,您可以在按钮点击时调用/调用下面的示例代码

    function getWeatherLocation() {
       navigator.geolocation.getCurrentPosition(onWeatherSuccess, onWeatherError,{enableHighAccuracy: true });
    }
    
    // Success callback for get geo coordinates
    
    var onWeatherSuccess = function (position) {
    
        Latitude = position.coords.latitude;
        Longitude = position.coords.longitude;
    
        //Do something with coordinates
    }
    
    // Error callback
    
    function onWeatherError(error) {
        console.log('code: ' + error.code + '\n' +
            'message: ' + error.message + '\n');
    }
    

    【讨论】:

      猜你喜欢
      • 2016-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-20
      • 2018-08-04
      • 2014-04-08
      • 2017-06-22
      • 1970-01-01
      相关资源
      最近更新 更多