2016-11-23 74 views
0

我遇到問題,但不知道它是否與此鏈接。警告:失敗的道具類型:必填道具'region.latitude'未指定

當我從Android Studio在Android模擬器上運行我的地圖時,出現此警告,我在Palo Alto上的Google plex上大地定位,而不是在我的實際地理位置上。是因爲我使用模擬器嗎?

這裏是我的代碼,如果你想查詢:

Map.js

import React, { Component } from 'react'; 
import Geolocation from '../geolocation/Geolocation'; 
import Shops from '../shops/Shop'; 
import { 
    Text, 
    View 
} from 'react-native'; 
import MapView from 'react-native-maps'; 
import styles from './Style'; 

class Map extends Geolocation { 

    render() { 
    return (
     <View> 
     <MapView style={styles.map} 
     region={{ 
      latitude: this.state.position.coords.latitude, 
      latitudeDelta: 0.001, 
      longitude: this.state.position.coords.longitude, 
      longitudeDelta: 0.001 
     }} /> 
     <Shops /> 
     </View> 
    ); 
    } 
} 

export default Map; 

Geolocation.js你的答案

import React, { Component } from 'react'; 
import styles from './Style'; 

class Geolocation extends Component { 

    constructor(props) { 
     super(props); 
     this.state = { 
      position : { 
       coords: {} 
      } 
     } 
    } 

    componentDidMount() { 
     navigator.geolocation.getCurrentPosition(
      (position) => { 
       this.setState({position}); 
      }, 
      (error) => alert(error.message), 
      {enableHighAccuracy: true, timeout: 20000} 
     ); 
     navigator.geolocation.watchPosition((position) => { 
      this.setState({position}); 
     }); 
    } 
} 

export default Geolocation; 

感謝,如果您有任何想法,我這個問題有點失落。

回答

1

我相信這是因爲你分配了從this.state.position.cords對象MapView座標,其初始渲染將未定義cords.latitudecords.longitude,從而投擲propTypes警告。

在構造函數中初始化它們以避免這種情況。

constructor(props) { 
    super(props); 
    this.state = { 
     position : { 
      coords: { 
       latitude: 0, 
       longitude: 0 
      } 
     } 
    } 
} 
+0

感謝您的答案,就是這樣! –

+0

太棒了!很高興它有幫助。 –

0

緯度和經度可以改變。

啓動從Android的工作室(工具 - > Android的 - > Android設備監視器)的Android設備監視器

切換到模擬器控制選項卡位置控制組更改。 enter image description here

+0

非常感謝你,在模擬器上你需要模擬地理位置,不知道! –

相關問題