2017-11-03 26 views
0

をマップで自分の位置を表示する私は、グーグルマップを作成しましたが、私は私のアプリを開いたとき、それは最初に私の場所が表示されますしたいです。どうすればいいですか?ここでは私が反応-ネイティブ初心者</p> <p>だ反応し、ネイティブ

私のコードです 輸出デフォルトのクラスGoogleMapApiは、コンポーネント{ 状態= { mapRegion拡張:ヌル、 lastLat:ヌル、 lastLong:ヌル、 }

componentDidMount() { 
    this.watchID = navigator.geolocation.watchPosition((position) => {  
     let region = { 
     latitude:  position.coords.latitude, 
     longitude:  position.coords.longitude, 
     latitudeDelta: 0.00922*1.5, 
     longitudeDelta: 0.00421*1.5 
     } 
     this.onRegionChange(region, region.latitude, region.longitude); 
    }); 
    } 

    onRegionChange(region, lastLat, lastLong) { 
    this.setState({ 
     mapRegion: region, 
     // If there are no new values set use the the current ones 
     lastLat: lastLat || this.state.lastLat, 
     lastLong: lastLong || this.state.lastLong 
    }); 
    } 

そして

componentWillUnmount() { 
     navigator.geolocation.clearWatch(this.watchID); 
     } 

     onMapPress(e) { 
     console.log(e.nativeEvent.coordinate.longitude); 
     let region = { 
      latitude:  e.nativeEvent.coordinate.latitude, 
      longitude:  e.nativeEvent.coordinate.longitude, 
      latitudeDelta: 0.00922*1.5, 
      longitudeDelta: 0.00421*1.5 
     } 
     this.onRegionChange(region, region.latitude, region.longitude); 
     } 

     render() { 
     return (
      <View style={{flex: 1}}> 
      <MapView 
       style={styles.map} 
       region={this.state.mapRegion} 
       showsUserLocation={true} 
       followUserLocation={true} 
       onRegionChange={this.onRegionChange.bind(this)} 
       onPress={this.onMapPress.bind(this)}> 
       <MapView.Marker 
       coordinate={{ 
        latitude: (this.state.lastLat + 0.00050) || -36.82339, 
        longitude: (this.state.lastLong + 0.00050) || -73.03569, 
       }}> 
       <View> 
        <Text style={{color: '#000'}}> 
        { this.state.lastLong }/{ this.state.lastLat } 
        </Text> 
       </View> 
       </MapView.Marker> 
      </MapView> 
      </View> 
     ); 
     } 
    } 

答えて

0

あなたの現在の場所を取得するには、反応ネイティブのGeoLocationライブラリを使用できます。そのドキュメントはです。

`static getCurrentPosition(geo_success, geo_error?, geo_options?)` 

docs for above method avaible here

現在の位置座標を取得し、それらの座標を使用して地図内の自分の位置を確認するには、この方法を使用する必要があります。

+0

tks u、それは私のために働く –

関連する問題