添加地理定位以创建 React Native App
Add geolocation to Create React Native App
我使用最新的 Expo CLI 创建了我的第一个 React Native 应用程序,使用了来自 React Native docs 的说明:
npm install -g expo-cli
npm expo init AwesomeProject
后来我想将 geolocation library 添加到此应用程序,但似乎整个自动 linking 都不想为我工作。我已经使用了提供的说明:
yarn add @react-native-community/geolocation
而且我还没有完成手册 link,因为我使用的是当前最新版本的 React Native(如库的自述文件中所述,高于 0.59)。添加示例代码后:
import Geolocation from '@react-native-community/geolocation';
Geolocation.getCurrentPosition(info => console.log(info));
我开始收到应该 linked 库的错误。在手动调用 link 命令后,不幸的是它仍然没有工作。
有时
Geolocation.getcurrentPosition 出现一些错误..
import React, {Component} from 'react';
import { Text, View, StyleSheet } from 'react-native';
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
latitude: 'lat',
longitude: 'long',
}
}
componentDidMount() {
navigator.geolocation = require('@react-native-community/geolocation');
navigator.geolocation.getCurrentPosition(
(position) => {
this.setState({
latitude: position.coords.latitude,
longitude: position.coords.longitude,
});
},
(error) => alert(error),
{ enableHighAccuracy: false, timeout: 200000, maximumAge: 1000 },
);
}
render(){
return (
<View style={styles.container}>
<Text style={styles.paragraph}>
{this.state.latitude}
</Text>
<Text style={styles.paragraph}>
{this.state.longitude}
</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
backgroundColor: '#ecf0f1',
padding: 8,
},
paragraph: {
margin: 24,
fontSize: 18,
fontWeight: 'bold',
textAlign: 'center',
},
});
这是一个简单的地理定位演示示例。试试看。并询问是否有任何疑问。
我实际上跳出这些库并就此进行了完整的 Expo(但我仍然不确定这是否是正确的方法):
import { useState, useEffect } from "react";
import * as Permissions from "expo-permissions";
import * as Location from "expo-location";
export const useLocation = () => {
const [location, setLocation] = useState();
useEffect(() => {
async function setLocationWithPerms() {
const permissions = await Permissions.askAsync(Permissions.LOCATION);
if (permissions.status === "granted") {
const currentLocation = await Location.getCurrentPositionAsync({});
setLocation(currentLocation);
}
}
setLocationWithPerms();
}, []);
return location;
};
我使用最新的 Expo CLI 创建了我的第一个 React Native 应用程序,使用了来自 React Native docs 的说明:
npm install -g expo-cli
npm expo init AwesomeProject
后来我想将 geolocation library 添加到此应用程序,但似乎整个自动 linking 都不想为我工作。我已经使用了提供的说明:
yarn add @react-native-community/geolocation
而且我还没有完成手册 link,因为我使用的是当前最新版本的 React Native(如库的自述文件中所述,高于 0.59)。添加示例代码后:
import Geolocation from '@react-native-community/geolocation';
Geolocation.getCurrentPosition(info => console.log(info));
我开始收到应该 linked 库的错误。在手动调用 link 命令后,不幸的是它仍然没有工作。
有时 Geolocation.getcurrentPosition 出现一些错误..
import React, {Component} from 'react';
import { Text, View, StyleSheet } from 'react-native';
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
latitude: 'lat',
longitude: 'long',
}
}
componentDidMount() {
navigator.geolocation = require('@react-native-community/geolocation');
navigator.geolocation.getCurrentPosition(
(position) => {
this.setState({
latitude: position.coords.latitude,
longitude: position.coords.longitude,
});
},
(error) => alert(error),
{ enableHighAccuracy: false, timeout: 200000, maximumAge: 1000 },
);
}
render(){
return (
<View style={styles.container}>
<Text style={styles.paragraph}>
{this.state.latitude}
</Text>
<Text style={styles.paragraph}>
{this.state.longitude}
</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
backgroundColor: '#ecf0f1',
padding: 8,
},
paragraph: {
margin: 24,
fontSize: 18,
fontWeight: 'bold',
textAlign: 'center',
},
});
这是一个简单的地理定位演示示例。试试看。并询问是否有任何疑问。
我实际上跳出这些库并就此进行了完整的 Expo(但我仍然不确定这是否是正确的方法):
import { useState, useEffect } from "react";
import * as Permissions from "expo-permissions";
import * as Location from "expo-location";
export const useLocation = () => {
const [location, setLocation] = useState();
useEffect(() => {
async function setLocationWithPerms() {
const permissions = await Permissions.askAsync(Permissions.LOCATION);
if (permissions.status === "granted") {
const currentLocation = await Location.getCurrentPositionAsync({});
setLocation(currentLocation);
}
}
setLocationWithPerms();
}, []);
return location;
};