1,getCurrentPosition()方法介绍

static getCurrentPosition(geo_success, geo_error?, geo_options?

该方法用于获取当前的位置,其参数如下:

(1)geo_success:成功回调函数
(2)geo_error:失败回调函数
(3)geo_options:传递的参数。其支持的属性有:
  • timeout:指定获取地理位置的超时时间,默认不限时。单位为毫秒。
  • maximumAge:最长有效期,在重复获取地理位置时,此参数指定多久再次获取位置。默认为 0,表示浏览器需要立刻重新计算位置。
  • enableHighAccuracy:指示浏览器获取高精度的位置,默认为 false。当开启后,可能没有任何影响,也可能使浏览器花费更长的时间获取更精确的位置数据。

    样例代码,直接可以使用

    import React from 'react';
    import {
    AppRegistry,
    StyleSheet,
    Text,
    View
    } from 'react-native'; var Geolocation = require('Geolocation'); //默认应用的容器组件
    export default class Localtion extends React.Component { static navigationOptions = ({ navigation }) => {
    const { navigate } = navigation;
    return {
    title: '获取定位'
    };
    }; //渲染
    render() {
    return (
    <View style={styles.container}>
    <Text style={styles.item} onPress={this.getLocation.bind(this)}>获取位置</Text>
    </View>
    );
    } //获取位置
    getLocation() {
    Geolocation.getCurrentPosition(
    location => {
    var result = "速度:" + location.coords.speed +
    "\n经度:" + location.coords.longitude +
    "\n纬度:" + location.coords.latitude +
    "\n准确度:" + location.coords.accuracy +
    "\n行进方向:" + location.coords.heading +
    "\n海拔:" + location.coords.altitude +
    "\n海拔准确度:" + location.coords.altitudeAccuracy +
    "\n时间戳:" + location.timestamp;
    alert(result);
    },
    error => {
    alert("获取位置失败:"+ error)
    }
    );
    }
    } //样式定义
    const styles = StyleSheet.create({
    container:{
    flex: ,
    marginTop:
    },
    item:{
    margin:,
    height:,
    borderWidth:,
    padding:,
    borderColor:'#ddd',
    textAlign:'center'
    },
    });

    二、监视定位变化

    1,watchPosition()方法介绍

    如果我们需要设定一个回调函数来不断响应定位数据发生的变更(设备发生了移动,或获取到了更高精度的地理位置信息)。可以通过 watchPosition() 函数实现该功能。
    watchPosition() 与 getCurrentPosition() 接收的参数相同,但回调函数会被调用多次。
    我们可以直接调用 watchPosition() 函数,不需要先调用 getCurrentPosition() 函数。
     

    2,clearWatch()方法介绍

    根据传入的 watchid 来对应的位置监听活动。

    样例代码

  • import React, { Component } from 'react';
    import {
    AppRegistry,
    StyleSheet,
    Text,
    View
    } from 'react-native'; var Geolocation = require('Geolocation'); //监听定位的id
    var watchID = null //默认应用的容器组件
    class App extends Component {
    //渲染
    render() {
    return (
    <View style={styles.container}>
    <Text style={styles.item} onPress={this.beginWatch.bind(this)}>开始监听</Text>
    <Text style={styles.item} onPress={this.stopWatch.bind(this)}>停止监听</Text>
    </View>
    );
    } //开始监听位置变化
    beginWatch() {
    watchID = Geolocation.watchPosition(
    location => {
    var result = "速度:" + location.coords.speed +
    "\n经度:" + location.coords.longitude +
    "\n纬度:" + location.coords.latitude +
    "\n准确度:" + location.coords.accuracy +
    "\n行进方向:" + location.coords.heading +
    "\n海拔:" + location.coords.altitude +
    "\n海拔准确度:" + location.coords.altitudeAccuracy +
    "\n时间戳:" + location.timestamp;
    alert(result);
    },
    error => {
    alert("获取位置失败:"+ error)
    }
    );
    } //停止监听位置变化
    stopWatch() {
    Geolocation.clearWatch(watchID);
    }
    } //样式定义
    const styles = StyleSheet.create({
    container:{
    flex: ,
    marginTop:
    },
    item:{
    margin:,
    height:,
    borderWidth:,
    padding:,
    borderColor:'#ddd',
    textAlign:'center'
    },
    }); AppRegistry.registerComponent('ReactDemo', () => App);

React Native - 使用Geolocation进行定位(获取当前位置、监听位置变化)的更多相关文章

  1. html5定位获取当前位置并在百度地图上显示

    用html5的地理定位功能通过手机定位获取当前位置并在地图上居中显示出来,下面是百度地图API的使用过程,有需要的朋友可以参考下 在开发移动端 web 或者webapp时,使用百度地图 API 的过程 ...

  2. 后台自动运行,定期记录定位数据(Hbuilder监听 app由前台切换到后台、切换运行环境的 监听方法)

    http://ask.dcloud.net.cn/question/28090 https://blog.csdn.net/qq_37508970/article/details/86649703 各 ...

  3. Android 之 获取地理位置及监听

    第一步.添加权限 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> ...

  4. 获取运行端口监听的用户身份auth-owner

    获取运行端口监听的用户身份auth-owner   Windows系统提供工作在TCP 113端口的授权服务(Authentication Service),用来判断TCP连接的用户.Nmap的aut ...

  5. Spring-Security (学习记录五)--配置登录时,密码采用md5加密,以及获取登录信息属性监听同步自己想要的登录信息

    目录 1. PasswordEncoder 采用密码加密 2. 获取当前的用户信息 1. PasswordEncoder 采用密码加密 使用前面的例子.可以看出我们数据库密码是采用明文的,我们在登录的 ...

  6. react native 安卓生产包无法获取线上数据

    android:usesCleartextTraffic="true"

  7. ios 定位获取当前位置信息

    啊,倦怠的人生啊~~ 什么事情都没做一眨眼就2点半了啊!!赶紧爬起来写博客啊. 诸位看官会鄙视我么,表示我真心不是把这当技术文章写的啊. 啊,下午我们来第二篇.获取地理位置信息.嗯嗯,秘籍上说叫逆向地 ...

  8. React Native纯干货总结

    随着项目也渐渐到了尾声,之前的项目是mobile开发,采用的是React Native.为即将要开始做RN项目或者已经做过的小伙伴可以参考借鉴,也顺便自己做一下之前项目的总结. 文章比较长,可以选择自 ...

  9. [React Native]获取网络状态

    使用React Native,可以使用NetInfo API获取手机当前的各个网络状态. componentWillMount() { NetInfo.fetch().done((status)=&g ...

随机推荐

  1. python 执行环境

    一些函数 执行其它非python程序 1 一些函数 callable callable()是一个布尔函数,确定一个对象是否可以通过函数操作符(())来调用.如果函数可调用便返回True,否则便是Fal ...

  2. spring mvc dispatcherservlet处理request流程

    1.dispatcherservlet接收到请求后,doDispatch(request,response) 2.doDispatch()里进行handlermapping,handlermappin ...

  3. Hard模式题目

    先过一下Hard模式的题目吧.   # Title Editorial Acceptance Difficulty Frequency   . 65 Valid Number     12.6% Ha ...

  4. POJ2584_T-Shirt Gumbo(二分图多重最大匹配/最大流)

    解题报告 http://blog.csdn.net/juncoder/article/details/38239367 题目传送门 题意: X个參赛选手,每一个选手有衣服大小的范围,5种大小的队服,求 ...

  5. Unity Update 具体解释

    0x01:简单介绍 Unity的脚本继承了Monobehaviour类,在脚本中定义函数: void FixedUpdate(){} void Update(){} void LateUpdate() ...

  6. selenium获取弹窗提示

    1.点击保存给的提示是几秒钟,遮罩显示 2. 其他弹窗处理方法 http://blog.csdn.net/Real_Tino/article/details/59068827

  7. WCF学习笔记——不支持内容类型 text/xml; charset=utf-8

    我在使用WCF的时候,客户端运行报错: 不支持内容类型 text/xml; charset=utf-8 原因是WCF服务做了修改.刷新客户端的服务引用,问题消失 =================== ...

  8. JS基础之开篇

    JavaScript是解释型语言,无需编译就可以随时运行,这样哪怕语法有错误,没有语法错误的部分还是能正确运行. 1.JavaScript能做什么? 01, javaScript可以进行表单验证 如果 ...

  9. 最经典的SDK程序结构 HelloWin

    程序运行效果:在创建窗口的时候,播放一个声音.且在窗口的客户区中央画一句文字:Hello, Windows 98!,无论程序怎么移动.最大化,文字始终在程序的中央部位. 程序总共分为六个步骤:定义,注 ...

  10. bootstrap异步加载树后样式显示问题

    整个过程: 1.先加载整个页面 2.通过jquery异步请求后台返回数据 3.循环遍历数据,拼接需要的内容 4.把拼接好的数据加载到页面中. 问题: 把拼接好的内容加载到页面后,样式显示不正确.而如果 ...