您可以使用 React-native 地理定位来做到这一点:
https://facebook.github.io/react-native/docs/geolocation.html https://facebook.github.io/react-native/docs/geolocation.html
那里已经有一个很好的例子来说明如何做到这一点:
componentDidMount: function() {
navigator.geolocation.getCurrentPosition(
(position) => {
var initialPosition = JSON.stringify(position);
this.setState({initialPosition});
},
(error) => alert(error.message),
{enableHighAccuracy: true, timeout: 20000, maximumAge: 1000}
);
this.watchID = navigator.geolocation.watchPosition((position) => {
var lastPosition = JSON.stringify(position);
this.setState({lastPosition});
});
}
您现在有了初始位置(纬度和经度),使用它代替硬编码值。每当位置发生变化时,watchPosition 就会调用成功回调。
我目前正在使用它在我们的应用程序中查找当地餐馆。
更新答案:
我正在使用 Genymotion 模拟器来运行它。如果您还打算在 Genymotion 上运行此代码,请确保您已经安装谷歌播放支持,否则react-native-maps将无法工作。如果您使用的是物理设备,则无需执行此步骤。
https://github.com/codepath/android_guides/wiki/Genymotion-2.0-Emulators-with-Google-Play-support https://github.com/codepath/android_guides/wiki/Genymotion-2.0-Emulators-with-Google-Play-support
关于地理定位,请确保您在AndroidManifest.xml中添加了ACCESS_FINE_LOCATION权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
否则,您将收到错误消息:“看起来该应用程序没有访问位置的权限。”
另请确保您的 GPS/Wifi/数据已启用,否则您的设备无法检索您的当前位置。
var React = require('react');
var ReactNative = require('react-native');
var {
StyleSheet,
PropTypes,
View,
Text,
Dimensions,
TouchableOpacity,
} = ReactNative;
var MapView = require('react-native-maps');
var { width, height } = Dimensions.get('window');
const ASPECT_RATIO = width / height;
// (Initial Static Location) Mumbai
const LATITUDE = 19.0760;
const LONGITUDE = 72.8777;
const LATITUDE_DELTA = 0.01;
const LONGITUDE_DELTA = LATITUDE_DELTA * ASPECT_RATIO;
var CustomMap = React.createClass({
getInitialState() {
return {
region: {
latitude: LATITUDE,
longitude: LONGITUDE,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA
},
};
},
componentDidMount: function() {
navigator.geolocation.getCurrentPosition(
(position) => {
this.setState({
region: {
latitude: position.coords.latitude,
longitude: position.coords.longitude,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA
}
});
},
(error) => alert(error.message),
{enableHighAccuracy: true, timeout: 20000, maximumAge: 1000}
);
this.watchID = navigator.geolocation.watchPosition((position) => {
const newRegion = {
latitude: position.coords.latitude,
longitude: position.coords.longitude,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA
}
this.onRegionChange(newRegion);
});
},
componentWillUnmount: function() {
navigator.geolocation.clearWatch(this.watchID);
},
onRegionChange(region) {
this.setState({ region });
},
render() {
return (
<View style={styles.container}>
<MapView
ref="map"
mapType="terrain"
style={styles.map}
region={this.state.region}
onRegionChange={this.onRegionChange}
>
</MapView>
<View style={styles.bubble}>
<Text style={{ textAlign: 'center'}}>
{`${this.state.region.latitude.toPrecision(7)}, ${this.state.region.longitude.toPrecision(7)}`}
</Text>
</View>
</View>
);
},
});
var styles = StyleSheet.create({
container: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
justifyContent: 'flex-end',
alignItems: 'center',
},
map: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
},
bubble: {
backgroundColor: 'rgba(255,255,255,0.7)',
paddingHorizontal: 18,
paddingVertical: 12,
borderRadius: 20,
},
});
module.exports = CustomMap;
运行上面的代码,您应该会发现,当调用 getCurrentPosition 时,您的原始静态位置(孟买)会通过 componentDidMount 被设备中的当前位置覆盖。
如果您需要跟踪位置变化,请使用观察位置结合区域更改时,否则如果您只需要获取初始位置即可删除观察位置 step.