我正在研究本机react,首先我有两个文件,我运行geolocation
函数来获取坐标并将其分配给状态。现在我想在我的第二个文件中访问这个状态。我尝试使用,props
但在控制台日志时显示未定义。请帮助我是新手。
export default class MapScreen extends React.Component {
constructor(props) {
super(props);
this.state = {
mapInitialRegion: {
latitude: 10,
longitude: 7,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA
},
markers: []
};
this.itemsRef = firebase.database().ref("incidents");
}
componentDidMount() {
// Get Marker Information from the server
//this._getMarkerInformation();
this.listenForItems(this.itemsRef);
// Get user's location to render the map around the user
navigator.geolocation.getCurrentPosition(
position => {
lat = parseFloat(position.coords.latitude);
long = parseFloat(position.coords.longitude);
console.log(position);
var initalRegion = {
latitude: lat,
longitude: long,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA
};
this.setState({ mapInitialRegion: initalRegion });
},
//error => alert(error),
);
} .. ..
在 Hospitals.js 中
import React, { Component } from 'react';
import {
Text,
View,
FlatList,
ActivityIndicator,
AppRegistry
} from 'react-native';
import { Components } from "expo";
import { List, ListItem } from "react-native-elements";
var _ = require('lodash');
export default class Hospitals extends React.Component {
constructor() {
super();
this.state = {
loading: false,
data: [],
pageToken: '',
refreshing: false,
siteTitle: ''
};
}
componentDidMount() {
this.fetchData();
}
fetchData = (props) => {
var latitude = this.props.propsname;
var longitude = this.props.propsname2;
const { pageToken } = this.state;
const urlFirst = `https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=${latitude},${longitude}&radius=500&type=hospital&key=your_api_key`
const urlNext =
`https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=${latitude},${longitude}&radius=500&type=hospital&key=Your_api_key&pagetoken=${pageToken}`;
let url = pageToken === '' ? urlFirst : urlNext
console.log(url);
console.log(latitude);
console.log(longitude);