4
在我的應用程序中,我實現了一個模塊,得到我的當前位置latidude和經度,現在我想出口這個數據googlemap組件,所以默認位置將被我當前覆蓋一。發送我的當前位置谷歌地圖在反應
這裏是我的代碼結構: location.js
import React from 'react';
import {geolocated} from 'react-geolocated';
class Demo extends React.Component {
render() {
return !this.props.isGeolocationAvailable
? <div>Your browser does not support Geolocation</div>
: !this.props.isGeolocationEnabled
? <div>Geolocation is not enabled</div>
: this.props.coords
? <table>
<tbody>
<tr><td>latitude</td><td>{this.props.coords.latitude}</td></tr>
<tr><td>longitude</td><td>{this.props.coords.longitude}</td></tr>
</tbody>
</table>
: <div>Getting the location data… </div>;
}
}
export default geolocated({
positionOptions: {
enableHighAccuracy: false,
},
userDecisionTimeout: 5000,
})(Demo);
我的GoogleMap的文件:
import React from 'react';
export default class GoogleMap extends React.Component{
constructor(){
super();
this.state = {
zoom: 13,
maptype: 'roadmap',
lat: -33.8688,
lng: 141.2195
}
}
componentDidMount(){
let map = new window.google.maps.Map(document.getElementById('map'), {
center: {
lat: this.state.lat,
lng: this.state.lng},
zoom: 13,
mapTypeId: 'roadmap',
})
console.log('loaded')
}
render(){
return(
<div>
<div id='map' />
</div>
)
}
}
最後我app.js文件(重要位):
import GoogleMap from './components/map';
import Demo from './components/local'
class App extends Component {
render(){
return(
<div> <Demo /> <GoogleMap /> </div>)
我知道我必須以某種方式導出父母和孩子之間的狀態,但我非常困惑d上有更大的項目,並與第三方庫