I am working on react native and I have two files in first I run geolocation
function to get the coordinates and assign it to states. Now I want to access this state in my 2nd file. I tried using props
but it shows undefined when I console log. Please help I am a newbie.
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),
);
} .. ..
inside 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);