static getCurrentPosition(geo_success, geo_error?, geo_options?
該方法用於獲取當前的位置,其參數以下:react
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: 1, marginTop:25 }, item:{ margin:15, height:30, borderWidth:1, padding:6, borderColor:'#ddd', textAlign:'center' }, });
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: 1, marginTop:25 }, item:{ margin:15, height:30, borderWidth:1, padding:6, borderColor:'#ddd', textAlign:'center' }, }); AppRegistry.registerComponent('ReactDemo', () => App);