2017-02-21 99 views
0

我需要从GPS访问我的位置。所以,我将地理位置导入到项目中。反应原生。我找不到我目前的位置(地理位置)IOS

首先,我创建下面这个样子(安装反应本地地图,已经链接)

,我只是已经在我的Xcode我在Xcode中添加NSLocationWhenInUseUsageDescription

也是,我刚已经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 
    } 
}); 
+0

在iOS模拟器中,它读取一个假的位置。在仿真器菜单中使用_Debug> Location> Custom Location ..._设置自定义位置。 – bendman

+0

但我需要从GPS获取当前位置。我该怎么办? –

+0

如果您在真实设备上运行它,您将获得真实位置。否则,您可以将您当前的位置放在我之前评论中提到的菜单中。您可以通过多种方式获得您的位置,包括:https://mylocation.org/ – bendman

回答

0

在iPhone模拟器它读取一个假位置。在模拟器菜单中使用调试>位置>自定义位置...设置自定义位置。

如果您在真实设备上运行它,您将获得真实位置。如果您继续使用模拟器,则可以使用自定义位置菜单来放置当前位置。您可以通过多种方式获取您的位置,其中包括:​​。