【发布时间】:2017-02-21 20:26:37
【问题描述】:
我需要通过 GPS 访问我的位置。所以,我将地理位置导入到项目中。
首先,我像这样创建以下内容(安装反应原生地图,已链接)
我刚刚在我的 Xcode 中添加了 NSLocationWhenInUseUsageDescription
另外,我的 Xcode 中已经有 libRCTGeolocation.a
然后,我刚刚在 Airbnb 的 mapview 中阅读了地图视图。告诉我设置 showUserLocation 将其设置为“true”。设置为 true 时将要求用户访问 位置。
参考:https://github.com/airbnb/react-native-maps/blob/master/docs/mapview.md
最后,那是不要求任何访问 地点。所以,我不知道为什么。我找不到我当前的位置。
这是我从一开始就逐步描述的图片 http://imgur.com/a/5TJZa
我会告诉你我的代码。
Route.js
import React, { Component, PropTypes } from 'react';
import {
StyleSheet,
View,
Text,
Image,
Dimensions,
} from 'react-native';
import MapView from 'react-native-maps';
var {height, width} = Dimensions.get('window');
export default class Route extends Component {
constructor(props){
super(props);
this.state = {
region: {
latitude: 37.78825,
longitude: -122.4324,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
}
};
this.onRegionChange = this.onRegionChange.bind(this);
}
onRegionChange(region) {
this.setState({ region });
}
render() {
return (
<View style={styles.container}>
<MapView style={styles.map}
mapType="standard"
showsUserLocation={true}
followsUserLocation={true}
showsCompass={false}
showsPointOfInterest={false}
region={this.state.region}
onRegionChange={this.onRegionChange}
/>
<View style={styles.container}>
<Text>
Latitude: {this.state.region.latitude}{'\n'}
Longitude: {this.state.region.longitude}{'\n'}
LatitudeDelta: {this.state.region.latitudeDelta}{'\n'}
LongitudeDelta: {this.state.region.longitudeDelta}
</Text>
</View>
</View>
);
}
}
Route.navigationOptions = { //this define from App.js option in SecondScreen
title: 'Route', //name top of StackNavigator
};
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
image: {
width: 200,
height: 200,
},
text: {
color: 'white',
fontWeight: 'bold',
backgroundColor: 'transparent',
marginTop: 20,
},
map: {
width: width,
height: height*2/3
}
});
【问题讨论】:
-
在 iOS 模拟器中,它读取了一个虚假的位置。使用模拟器菜单中的 Debug > Location > Custom Location... 设置自定义位置。
-
但我需要从 GPS 访问我当前的位置。我该怎么办?
-
如果您在真实设备上运行它,您将获得真实位置。否则,您可以将您当前的位置放在我在之前评论中提到的菜单中。您可以通过多种方式获取您的位置,包括:mylocation.org
-
哦,我真的非常感谢你。
标签: ios xcode react-native geolocation react-native-maps