W3Cschool
恭喜您成為首批注冊(cè)用戶(hù)
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
你需要在 info.plist 中添加 NSLocationWhenInUseUsageDescription
鍵來(lái)定位,當(dāng)你用 react-native init
來(lái)創(chuàng)建一個(gè)項(xiàng)目時(shí),默認(rèn)情況下定位是能夠使用的。
定位遵循 MDN 規(guī)范:
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()
/* 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 <GeolocationExample />; }, } ];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 ( <View> <Text> <Text style={styles.title}>Initial position: </Text> {JSON.stringify(this.state.initialPosition)} </Text> <Text> <Text style={styles.title}>Current position: </Text> {JSON.stringify(this.state.lastPosition)} </Text> </View> ); } });var styles = StyleSheet.create({ title: { fontWeight: '500', }, });
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話(huà):173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: