创新互联React教程:定位

你需要在 info.plist 中添加 NSLocationWhenInUseUsageDescription 键来定位,当你用 react-native init 来创建一个项目时,默认情况下定位是能够使用的。

创新互联主要从事做网站、网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务东至,十年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:028-86922220

定位遵循 MDN 规范:
https://developer.mozilla.org/en-US/docs/Web/API/Geolocation

方法

static getCurrentPosition(geo_success: Function, geo_error?: Function, geo_options?: Object)

static watchPosition(success: Function, error?: Function, options?: Object)

static clearWatch(watchID: number)

static stopObserving()

例子

Edit on GitHub

/* eslint no-console: 0 */'use strict';var React = require('react-native');var {
  StyleSheet,
  Text,
  View,
} = React;
exports.framework = 'React';
exports.title = 'Geolocation';
exports.description = 'Examples of using the Geolocation API.';
exports.examples = [
  {
    title: 'navigator.geolocation',
    render: function(): ReactElement {      return ;
    },
  }
];var GeolocationExample = React.createClass({
  watchID: (null: ?number),
  getInitialState: function() {    return {
      initialPosition: 'unknown',
      lastPosition: 'unknown',
    };
  },
  componentDidMount: function() {
    navigator.geolocation.getCurrentPosition(
      (initialPosition) => this.setState({initialPosition}),
      (error) => console.error(error)
    );    this.watchID = navigator.geolocation.watchPosition((lastPosition) => {      this.setState({lastPosition});
    });
  },
  componentWillUnmount: function() {
    navigator.geolocation.clearWatch(this.watchID);
  },
  render: function() {    return (      
        
          Initial position: 
          {JSON.stringify(this.state.initialPosition)}        
        
          Current position: 
          {JSON.stringify(this.state.lastPosition)}        
      
    );
  }
});var styles = StyleSheet.create({
  title: {
    fontWeight: '500',
  },
});

分享文章:创新互联React教程:定位
转载注明:http://www.hantingmc.com/qtweb/news45/531945.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联