10: Cordova Geolocation获取当前位置

本文介绍如何在Cordova项目中添加并使用Geolocation插件进行设备定位,包括插件安装步骤、百度定位插件替代方案及SDK申请流程,提供了一个简单的定位功能示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,添加Geolocation插件
首先我们要在“终端”中进入工程所在的目录,然后运行如下命令:

1

cordova plugin add cordova-plugin-geolocation

可以看到 geolocation 定位插件已经成功添加了:

原文:Cordova - Geolocation插件的使用(GPS地理定位,经度、纬度、海拔等)

如果测试中,发下定位有问题

请参考百度定位cordova插件

https://github.com/hewz/cordova-baidu-location 或者

https://github.com/mrwutong/cordova-qdc-baidu-location

https://www.jianshu.com/p/ca2846a72243( 使用百度最新的sdk)

申请百度sdk: 参考:http://lbsyun.baidu.com/index.php?title=androidsdk/guide/create-project/ak 

集成skd 参考http://lbsyun.baidu.com/index.php?title=androidsdk/guide/create-project/androidstudio

2,调用定位功能

下面样例代码,点击页面上的“获取位置信息”按钮后,会将获取到的GPS信息弹出显示。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

<!DOCTYPE html>

<html>

    <head>

        <title>Capture Photo</title>

        <meta http-equiv="Content-type" content="text/html; charset=utf-8">

        <script type="text/javascript" charset="utf-8" src="cordova.js"></script>

        <script type="text/javascript" charset="utf-8">

 

            document.addEventListener("deviceready",onDeviceReady,false);

 

            //Cordova加载完成会触发

            function onDeviceReady() {

            }

 

            function getCurrentPosition(){

                //定位数据获取成功响应

                var onSuccess = function(position) {

                    alert('纬度: '          + position.coords.latitude          + '\n' +

                          '经度: '         + position.coords.longitude         + '\n' +

                          '海拔: '          + position.coords.altitude          + '\n' +

                          '水平精度: '          + position.coords.accuracy          + '\n' +

                          '垂直精度: ' + position.coords.altitudeAccuracy  + '\n' +

                          '方向: '           + position.coords.heading           + '\n' +

                          '速度: '             + position.coords.speed             + '\n' +

                          '时间戳: '         + position.timestamp                + '\n');

                };

 

                //定位数据获取失败响应

                function onError(error) {

                    alert('code: '    + error.code    + '\n' +

                          'message: ' + error.message + '\n');

                }

 

                //开始获取定位数据

                navigator.geolocation.getCurrentPosition(onSuccess, onError);

            }

        </script>

    </head>

    <body style="padding-top:50px">

        <button style="font-size:23px;" onclick="getCurrentPosition();">获取位置信息</button>

    </body>

</html>


 

转载于:https://my.oschina.net/thinkive/blog/2878433

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值