GoogleMap ReactJS: How to pass the value of state into defaultcenter of googlemap
I currently working on Google Map ReactJS of https://tomchentw.github.io, i used this because it's recommended.
Problem:
I have certain question and problem.
Why i cannot pass the value of state of lat lng to the defaultcenter properties of google map. the props that i pass has no value that's why it turns to grey box.., how to pass the value of props to the defaultCenter?
Google Map Code:
const google = window.google
const MyMapComponent = compose(
withProps({
googleMapURL: "https://maps.googleapis.com/maps/api/js?&v=3.exp&libraries=geometry,drawing,places&key=AIzaSyDWCz4V5r29GxcGZKNtFzE9v4gOSnKVMYA",
loadingElement: <div style={{ height: `100%` }} />,
containerElement: <div style={{ height: `400px` }} />,
mapElement: <div style={{ height: `100%` }} />,
}),
withHandlers({
onMarkerClustererClick: () => (markerClusterer) => {
const clickedMarkers = markerClusterer.getMarkers()
console.log(`Current clicked markers length: ${clickedMarkers.length}`)
console.log(clickedMarkers)
},
}),
withScriptjs,
withGoogleMap
)(props =>
<GoogleMap
defaultZoom={8}
defaultCenter={{lat: parseFloat(props.lat_value_props), lng: parseFloat(props.lang_value_props) }}
//defaultCenter={{lat:53.569967, lng:-113.393261}}
>
{props.markers.map(marker => (
<Marker
key={marker.id}
position={{ lat: parseFloat(marker.store_lat), lng: parseFloat(marker.store_long) }}
/>
))}
</GoogleMap>
);
JSX:
export default class FindStore extends Component {
constructor(props) {
super(props);
this.state = {
text:'',
city:'',
store_details:,
searchTerm: '',
lat_value:'',
lng_value:'',
markers: ,
}
this.searchUpdated = this.searchUpdated.bind(this)
}
componentDidMount() {
const id = this.props.match.params.id;
axios.get('/api/find_store_location/' + id).then(response => {
this.setState({
markers: response.data,
lat:response.data[0].store_lat,
long:response.data[0].store_long,
})
console.log(this.state.lat_value);
console.log(this.state.lng_value);
}).catch(error => console.log(error));
axios.get('/api/store_details').then(response => {
this.setState({
store_details:response.data
})
}).catch(error => console.log(error.response));
}
//store details oveflow leftside
render() {
const filteredEmails = this.state.store_details.filter(createFilter(this.state.searchTerm, KEYS_TO_FILTERS))
return (
<div>
<div className="header">
<div className="jumbotron">
<h1>Store Directory</h1>
</div>
</div>
<br/><br/><br/>
<div className="container">
<div className="row">
<div className="col-md-5">
<form method="get" onSubmit={this.SubmitFilterStore}>
<div className="form-group">
<label htmlFor="exampleSelect1">Select City</label>
<select value={this.state.city} className="form-control" id="exampleSelect1">
<option readOnly={true} selected={true} >Choose City</option>
<option value="Winnipeg">Winnipeg</option>
<option value="Edmonton">Edmonton</option>
<option value="Calgary">Calgary</option>
</select>
</div>
<div className="input-group mb-3" id="text_search">
<SearchInput className="search-input" onChange={this.searchUpdated} />
<br/> <br/>
<div style={{overflowY:'scroll', height:'500px'}}>
{filteredEmails.map(store => {
if(store.image == "")
{
return (
<div className="store" key={store.id}>
<div className="card" style={{width: '18rem' }}>
<div className="container">
<br/>
<p>No Store Image Uploaded</p>
</div>
<div className="card-body">
<div id="store-description">
<label>{store.branch_name}</label><br/>
<label>{store.store_type}</label><br/>
<label>{store.store_contactnumber}</label><br/>
<label>{store.store_businesshour}</label>
<br/><br/>
<Link to={'/find-store-location/'+store.id}><button className="btn">Direction <i className="fas fa-directions"></i> </button></Link>
</div>
</div>
</div>
<br/>
</div>
)
}
else
{
return (
<div className="store" key={store.id}>
<div className="card" style={{width: '18rem' }}>
<div className="container">
<br/>
<img src={"/storage/"+store.image}
height="120"
className="img-responsive"></img>
</div>
<div className="card-body">
<div id="store-description">
<label>{store.branch_name}</label><br/>
<label>{store.store_type}</label><br/>
<label>{store.store_contactnumber}</label><br/>
<label>{store.store_businesshour}</label>
<br/><br/>
<Link to={'/find-store-location/'+store.id}><button className="btn">Direction <i className="fas fa-directions"></i> </button></Link>
</div>
</div>
</div>
<br/>
</div>
)
}
})}
</div>
</div>
</form>
</div>
<div className="col-md-7">
<MyMapComponent
markers={this.state.markers}
lat_value_props={this.state.lat_value}
lang_value_props={this.state.lng_value}
/>
</div>
</div>
</div>
</div>
)
}
searchUpdated (term) {
this.setState({searchTerm: term})
}
}
reactjs google-maps
|
show 2 more comments
I currently working on Google Map ReactJS of https://tomchentw.github.io, i used this because it's recommended.
Problem:
I have certain question and problem.
Why i cannot pass the value of state of lat lng to the defaultcenter properties of google map. the props that i pass has no value that's why it turns to grey box.., how to pass the value of props to the defaultCenter?
Google Map Code:
const google = window.google
const MyMapComponent = compose(
withProps({
googleMapURL: "https://maps.googleapis.com/maps/api/js?&v=3.exp&libraries=geometry,drawing,places&key=AIzaSyDWCz4V5r29GxcGZKNtFzE9v4gOSnKVMYA",
loadingElement: <div style={{ height: `100%` }} />,
containerElement: <div style={{ height: `400px` }} />,
mapElement: <div style={{ height: `100%` }} />,
}),
withHandlers({
onMarkerClustererClick: () => (markerClusterer) => {
const clickedMarkers = markerClusterer.getMarkers()
console.log(`Current clicked markers length: ${clickedMarkers.length}`)
console.log(clickedMarkers)
},
}),
withScriptjs,
withGoogleMap
)(props =>
<GoogleMap
defaultZoom={8}
defaultCenter={{lat: parseFloat(props.lat_value_props), lng: parseFloat(props.lang_value_props) }}
//defaultCenter={{lat:53.569967, lng:-113.393261}}
>
{props.markers.map(marker => (
<Marker
key={marker.id}
position={{ lat: parseFloat(marker.store_lat), lng: parseFloat(marker.store_long) }}
/>
))}
</GoogleMap>
);
JSX:
export default class FindStore extends Component {
constructor(props) {
super(props);
this.state = {
text:'',
city:'',
store_details:,
searchTerm: '',
lat_value:'',
lng_value:'',
markers: ,
}
this.searchUpdated = this.searchUpdated.bind(this)
}
componentDidMount() {
const id = this.props.match.params.id;
axios.get('/api/find_store_location/' + id).then(response => {
this.setState({
markers: response.data,
lat:response.data[0].store_lat,
long:response.data[0].store_long,
})
console.log(this.state.lat_value);
console.log(this.state.lng_value);
}).catch(error => console.log(error));
axios.get('/api/store_details').then(response => {
this.setState({
store_details:response.data
})
}).catch(error => console.log(error.response));
}
//store details oveflow leftside
render() {
const filteredEmails = this.state.store_details.filter(createFilter(this.state.searchTerm, KEYS_TO_FILTERS))
return (
<div>
<div className="header">
<div className="jumbotron">
<h1>Store Directory</h1>
</div>
</div>
<br/><br/><br/>
<div className="container">
<div className="row">
<div className="col-md-5">
<form method="get" onSubmit={this.SubmitFilterStore}>
<div className="form-group">
<label htmlFor="exampleSelect1">Select City</label>
<select value={this.state.city} className="form-control" id="exampleSelect1">
<option readOnly={true} selected={true} >Choose City</option>
<option value="Winnipeg">Winnipeg</option>
<option value="Edmonton">Edmonton</option>
<option value="Calgary">Calgary</option>
</select>
</div>
<div className="input-group mb-3" id="text_search">
<SearchInput className="search-input" onChange={this.searchUpdated} />
<br/> <br/>
<div style={{overflowY:'scroll', height:'500px'}}>
{filteredEmails.map(store => {
if(store.image == "")
{
return (
<div className="store" key={store.id}>
<div className="card" style={{width: '18rem' }}>
<div className="container">
<br/>
<p>No Store Image Uploaded</p>
</div>
<div className="card-body">
<div id="store-description">
<label>{store.branch_name}</label><br/>
<label>{store.store_type}</label><br/>
<label>{store.store_contactnumber}</label><br/>
<label>{store.store_businesshour}</label>
<br/><br/>
<Link to={'/find-store-location/'+store.id}><button className="btn">Direction <i className="fas fa-directions"></i> </button></Link>
</div>
</div>
</div>
<br/>
</div>
)
}
else
{
return (
<div className="store" key={store.id}>
<div className="card" style={{width: '18rem' }}>
<div className="container">
<br/>
<img src={"/storage/"+store.image}
height="120"
className="img-responsive"></img>
</div>
<div className="card-body">
<div id="store-description">
<label>{store.branch_name}</label><br/>
<label>{store.store_type}</label><br/>
<label>{store.store_contactnumber}</label><br/>
<label>{store.store_businesshour}</label>
<br/><br/>
<Link to={'/find-store-location/'+store.id}><button className="btn">Direction <i className="fas fa-directions"></i> </button></Link>
</div>
</div>
</div>
<br/>
</div>
)
}
})}
</div>
</div>
</form>
</div>
<div className="col-md-7">
<MyMapComponent
markers={this.state.markers}
lat_value_props={this.state.lat_value}
lang_value_props={this.state.lng_value}
/>
</div>
</div>
</div>
</div>
)
}
searchUpdated (term) {
this.setState({searchTerm: term})
}
}
reactjs google-maps
console.log(this.state.latlng); it gives me right position.
– DevGe
Nov 21 '18 at 7:51
Is your <GoogleMap> in the render() function?
– Shawn Andrews
Nov 21 '18 at 7:51
@ShawnAndrews nope in the top of class
– DevGe
Nov 21 '18 at 7:52
it look like this . <GoogleMap defaultZoom={8} defaultCenter={this.state.latlng} > {props.markers.map(marker => ( <Marker key={marker.id} position={{ lat: parseFloat(marker.store_lat), lng: parseFloat(marker.store_long) }} /> ))} </GoogleMap> ); export default class FindStore extends Component {
– DevGe
Nov 21 '18 at 7:52
can you edit your post to include the full class?
– Shawn Andrews
Nov 21 '18 at 7:54
|
show 2 more comments
I currently working on Google Map ReactJS of https://tomchentw.github.io, i used this because it's recommended.
Problem:
I have certain question and problem.
Why i cannot pass the value of state of lat lng to the defaultcenter properties of google map. the props that i pass has no value that's why it turns to grey box.., how to pass the value of props to the defaultCenter?
Google Map Code:
const google = window.google
const MyMapComponent = compose(
withProps({
googleMapURL: "https://maps.googleapis.com/maps/api/js?&v=3.exp&libraries=geometry,drawing,places&key=AIzaSyDWCz4V5r29GxcGZKNtFzE9v4gOSnKVMYA",
loadingElement: <div style={{ height: `100%` }} />,
containerElement: <div style={{ height: `400px` }} />,
mapElement: <div style={{ height: `100%` }} />,
}),
withHandlers({
onMarkerClustererClick: () => (markerClusterer) => {
const clickedMarkers = markerClusterer.getMarkers()
console.log(`Current clicked markers length: ${clickedMarkers.length}`)
console.log(clickedMarkers)
},
}),
withScriptjs,
withGoogleMap
)(props =>
<GoogleMap
defaultZoom={8}
defaultCenter={{lat: parseFloat(props.lat_value_props), lng: parseFloat(props.lang_value_props) }}
//defaultCenter={{lat:53.569967, lng:-113.393261}}
>
{props.markers.map(marker => (
<Marker
key={marker.id}
position={{ lat: parseFloat(marker.store_lat), lng: parseFloat(marker.store_long) }}
/>
))}
</GoogleMap>
);
JSX:
export default class FindStore extends Component {
constructor(props) {
super(props);
this.state = {
text:'',
city:'',
store_details:,
searchTerm: '',
lat_value:'',
lng_value:'',
markers: ,
}
this.searchUpdated = this.searchUpdated.bind(this)
}
componentDidMount() {
const id = this.props.match.params.id;
axios.get('/api/find_store_location/' + id).then(response => {
this.setState({
markers: response.data,
lat:response.data[0].store_lat,
long:response.data[0].store_long,
})
console.log(this.state.lat_value);
console.log(this.state.lng_value);
}).catch(error => console.log(error));
axios.get('/api/store_details').then(response => {
this.setState({
store_details:response.data
})
}).catch(error => console.log(error.response));
}
//store details oveflow leftside
render() {
const filteredEmails = this.state.store_details.filter(createFilter(this.state.searchTerm, KEYS_TO_FILTERS))
return (
<div>
<div className="header">
<div className="jumbotron">
<h1>Store Directory</h1>
</div>
</div>
<br/><br/><br/>
<div className="container">
<div className="row">
<div className="col-md-5">
<form method="get" onSubmit={this.SubmitFilterStore}>
<div className="form-group">
<label htmlFor="exampleSelect1">Select City</label>
<select value={this.state.city} className="form-control" id="exampleSelect1">
<option readOnly={true} selected={true} >Choose City</option>
<option value="Winnipeg">Winnipeg</option>
<option value="Edmonton">Edmonton</option>
<option value="Calgary">Calgary</option>
</select>
</div>
<div className="input-group mb-3" id="text_search">
<SearchInput className="search-input" onChange={this.searchUpdated} />
<br/> <br/>
<div style={{overflowY:'scroll', height:'500px'}}>
{filteredEmails.map(store => {
if(store.image == "")
{
return (
<div className="store" key={store.id}>
<div className="card" style={{width: '18rem' }}>
<div className="container">
<br/>
<p>No Store Image Uploaded</p>
</div>
<div className="card-body">
<div id="store-description">
<label>{store.branch_name}</label><br/>
<label>{store.store_type}</label><br/>
<label>{store.store_contactnumber}</label><br/>
<label>{store.store_businesshour}</label>
<br/><br/>
<Link to={'/find-store-location/'+store.id}><button className="btn">Direction <i className="fas fa-directions"></i> </button></Link>
</div>
</div>
</div>
<br/>
</div>
)
}
else
{
return (
<div className="store" key={store.id}>
<div className="card" style={{width: '18rem' }}>
<div className="container">
<br/>
<img src={"/storage/"+store.image}
height="120"
className="img-responsive"></img>
</div>
<div className="card-body">
<div id="store-description">
<label>{store.branch_name}</label><br/>
<label>{store.store_type}</label><br/>
<label>{store.store_contactnumber}</label><br/>
<label>{store.store_businesshour}</label>
<br/><br/>
<Link to={'/find-store-location/'+store.id}><button className="btn">Direction <i className="fas fa-directions"></i> </button></Link>
</div>
</div>
</div>
<br/>
</div>
)
}
})}
</div>
</div>
</form>
</div>
<div className="col-md-7">
<MyMapComponent
markers={this.state.markers}
lat_value_props={this.state.lat_value}
lang_value_props={this.state.lng_value}
/>
</div>
</div>
</div>
</div>
)
}
searchUpdated (term) {
this.setState({searchTerm: term})
}
}
reactjs google-maps
I currently working on Google Map ReactJS of https://tomchentw.github.io, i used this because it's recommended.
Problem:
I have certain question and problem.
Why i cannot pass the value of state of lat lng to the defaultcenter properties of google map. the props that i pass has no value that's why it turns to grey box.., how to pass the value of props to the defaultCenter?
Google Map Code:
const google = window.google
const MyMapComponent = compose(
withProps({
googleMapURL: "https://maps.googleapis.com/maps/api/js?&v=3.exp&libraries=geometry,drawing,places&key=AIzaSyDWCz4V5r29GxcGZKNtFzE9v4gOSnKVMYA",
loadingElement: <div style={{ height: `100%` }} />,
containerElement: <div style={{ height: `400px` }} />,
mapElement: <div style={{ height: `100%` }} />,
}),
withHandlers({
onMarkerClustererClick: () => (markerClusterer) => {
const clickedMarkers = markerClusterer.getMarkers()
console.log(`Current clicked markers length: ${clickedMarkers.length}`)
console.log(clickedMarkers)
},
}),
withScriptjs,
withGoogleMap
)(props =>
<GoogleMap
defaultZoom={8}
defaultCenter={{lat: parseFloat(props.lat_value_props), lng: parseFloat(props.lang_value_props) }}
//defaultCenter={{lat:53.569967, lng:-113.393261}}
>
{props.markers.map(marker => (
<Marker
key={marker.id}
position={{ lat: parseFloat(marker.store_lat), lng: parseFloat(marker.store_long) }}
/>
))}
</GoogleMap>
);
JSX:
export default class FindStore extends Component {
constructor(props) {
super(props);
this.state = {
text:'',
city:'',
store_details:,
searchTerm: '',
lat_value:'',
lng_value:'',
markers: ,
}
this.searchUpdated = this.searchUpdated.bind(this)
}
componentDidMount() {
const id = this.props.match.params.id;
axios.get('/api/find_store_location/' + id).then(response => {
this.setState({
markers: response.data,
lat:response.data[0].store_lat,
long:response.data[0].store_long,
})
console.log(this.state.lat_value);
console.log(this.state.lng_value);
}).catch(error => console.log(error));
axios.get('/api/store_details').then(response => {
this.setState({
store_details:response.data
})
}).catch(error => console.log(error.response));
}
//store details oveflow leftside
render() {
const filteredEmails = this.state.store_details.filter(createFilter(this.state.searchTerm, KEYS_TO_FILTERS))
return (
<div>
<div className="header">
<div className="jumbotron">
<h1>Store Directory</h1>
</div>
</div>
<br/><br/><br/>
<div className="container">
<div className="row">
<div className="col-md-5">
<form method="get" onSubmit={this.SubmitFilterStore}>
<div className="form-group">
<label htmlFor="exampleSelect1">Select City</label>
<select value={this.state.city} className="form-control" id="exampleSelect1">
<option readOnly={true} selected={true} >Choose City</option>
<option value="Winnipeg">Winnipeg</option>
<option value="Edmonton">Edmonton</option>
<option value="Calgary">Calgary</option>
</select>
</div>
<div className="input-group mb-3" id="text_search">
<SearchInput className="search-input" onChange={this.searchUpdated} />
<br/> <br/>
<div style={{overflowY:'scroll', height:'500px'}}>
{filteredEmails.map(store => {
if(store.image == "")
{
return (
<div className="store" key={store.id}>
<div className="card" style={{width: '18rem' }}>
<div className="container">
<br/>
<p>No Store Image Uploaded</p>
</div>
<div className="card-body">
<div id="store-description">
<label>{store.branch_name}</label><br/>
<label>{store.store_type}</label><br/>
<label>{store.store_contactnumber}</label><br/>
<label>{store.store_businesshour}</label>
<br/><br/>
<Link to={'/find-store-location/'+store.id}><button className="btn">Direction <i className="fas fa-directions"></i> </button></Link>
</div>
</div>
</div>
<br/>
</div>
)
}
else
{
return (
<div className="store" key={store.id}>
<div className="card" style={{width: '18rem' }}>
<div className="container">
<br/>
<img src={"/storage/"+store.image}
height="120"
className="img-responsive"></img>
</div>
<div className="card-body">
<div id="store-description">
<label>{store.branch_name}</label><br/>
<label>{store.store_type}</label><br/>
<label>{store.store_contactnumber}</label><br/>
<label>{store.store_businesshour}</label>
<br/><br/>
<Link to={'/find-store-location/'+store.id}><button className="btn">Direction <i className="fas fa-directions"></i> </button></Link>
</div>
</div>
</div>
<br/>
</div>
)
}
})}
</div>
</div>
</form>
</div>
<div className="col-md-7">
<MyMapComponent
markers={this.state.markers}
lat_value_props={this.state.lat_value}
lang_value_props={this.state.lng_value}
/>
</div>
</div>
</div>
</div>
)
}
searchUpdated (term) {
this.setState({searchTerm: term})
}
}
reactjs google-maps
reactjs google-maps
edited Nov 21 '18 at 8:58
DevGe
asked Nov 21 '18 at 7:44
DevGeDevGe
1018
1018
console.log(this.state.latlng); it gives me right position.
– DevGe
Nov 21 '18 at 7:51
Is your <GoogleMap> in the render() function?
– Shawn Andrews
Nov 21 '18 at 7:51
@ShawnAndrews nope in the top of class
– DevGe
Nov 21 '18 at 7:52
it look like this . <GoogleMap defaultZoom={8} defaultCenter={this.state.latlng} > {props.markers.map(marker => ( <Marker key={marker.id} position={{ lat: parseFloat(marker.store_lat), lng: parseFloat(marker.store_long) }} /> ))} </GoogleMap> ); export default class FindStore extends Component {
– DevGe
Nov 21 '18 at 7:52
can you edit your post to include the full class?
– Shawn Andrews
Nov 21 '18 at 7:54
|
show 2 more comments
console.log(this.state.latlng); it gives me right position.
– DevGe
Nov 21 '18 at 7:51
Is your <GoogleMap> in the render() function?
– Shawn Andrews
Nov 21 '18 at 7:51
@ShawnAndrews nope in the top of class
– DevGe
Nov 21 '18 at 7:52
it look like this . <GoogleMap defaultZoom={8} defaultCenter={this.state.latlng} > {props.markers.map(marker => ( <Marker key={marker.id} position={{ lat: parseFloat(marker.store_lat), lng: parseFloat(marker.store_long) }} /> ))} </GoogleMap> ); export default class FindStore extends Component {
– DevGe
Nov 21 '18 at 7:52
can you edit your post to include the full class?
– Shawn Andrews
Nov 21 '18 at 7:54
console.log(this.state.latlng); it gives me right position.
– DevGe
Nov 21 '18 at 7:51
console.log(this.state.latlng); it gives me right position.
– DevGe
Nov 21 '18 at 7:51
Is your <GoogleMap> in the render() function?
– Shawn Andrews
Nov 21 '18 at 7:51
Is your <GoogleMap> in the render() function?
– Shawn Andrews
Nov 21 '18 at 7:51
@ShawnAndrews nope in the top of class
– DevGe
Nov 21 '18 at 7:52
@ShawnAndrews nope in the top of class
– DevGe
Nov 21 '18 at 7:52
it look like this . <GoogleMap defaultZoom={8} defaultCenter={this.state.latlng} > {props.markers.map(marker => ( <Marker key={marker.id} position={{ lat: parseFloat(marker.store_lat), lng: parseFloat(marker.store_long) }} /> ))} </GoogleMap> ); export default class FindStore extends Component {
– DevGe
Nov 21 '18 at 7:52
it look like this . <GoogleMap defaultZoom={8} defaultCenter={this.state.latlng} > {props.markers.map(marker => ( <Marker key={marker.id} position={{ lat: parseFloat(marker.store_lat), lng: parseFloat(marker.store_long) }} /> ))} </GoogleMap> ); export default class FindStore extends Component {
– DevGe
Nov 21 '18 at 7:52
can you edit your post to include the full class?
– Shawn Andrews
Nov 21 '18 at 7:54
can you edit your post to include the full class?
– Shawn Andrews
Nov 21 '18 at 7:54
|
show 2 more comments
1 Answer
1
active
oldest
votes
The error is because MyMapComponent is not a React.Component and therefore this.state does not exist.
The solution is to send the latlng along with your markers data to the Map Component.
Change to
<MyMapComponent
markers={this.state.markers}
latlng={this.state.latlng}
/>
and
<GoogleMap
defaultZoom={8}
defaultCenter={props.latlng}
>
hi @Shawn Andres. the map is not shown..
– DevGe
Nov 21 '18 at 8:24
the map turn only on grey box.. so it means their is no lat and lng value..
– DevGe
Nov 21 '18 at 8:30
Your componentDidMount is called after the component is initially rendered, so you're rendering the map without waiting to get the map API data. I recommend deletingcomponentWillMount() { this.setState({ markers: }) }
and renaming componentDidMount to componentWillMount.
– Shawn Andrews
Nov 21 '18 at 8:40
view my new update codes.
– DevGe
Nov 21 '18 at 8:42
in your updated code you send the propslat_value_props
but in the component you useprops.lat_value
instead. Not sure if you intended that. Does your map still give a grey box with{{lat:53.569967, lng:-113.393261}}
?
– Shawn Andrews
Nov 21 '18 at 8:48
|
show 3 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407341%2fgooglemap-reactjs-how-to-pass-the-value-of-state-into-defaultcenter-of-googlema%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The error is because MyMapComponent is not a React.Component and therefore this.state does not exist.
The solution is to send the latlng along with your markers data to the Map Component.
Change to
<MyMapComponent
markers={this.state.markers}
latlng={this.state.latlng}
/>
and
<GoogleMap
defaultZoom={8}
defaultCenter={props.latlng}
>
hi @Shawn Andres. the map is not shown..
– DevGe
Nov 21 '18 at 8:24
the map turn only on grey box.. so it means their is no lat and lng value..
– DevGe
Nov 21 '18 at 8:30
Your componentDidMount is called after the component is initially rendered, so you're rendering the map without waiting to get the map API data. I recommend deletingcomponentWillMount() { this.setState({ markers: }) }
and renaming componentDidMount to componentWillMount.
– Shawn Andrews
Nov 21 '18 at 8:40
view my new update codes.
– DevGe
Nov 21 '18 at 8:42
in your updated code you send the propslat_value_props
but in the component you useprops.lat_value
instead. Not sure if you intended that. Does your map still give a grey box with{{lat:53.569967, lng:-113.393261}}
?
– Shawn Andrews
Nov 21 '18 at 8:48
|
show 3 more comments
The error is because MyMapComponent is not a React.Component and therefore this.state does not exist.
The solution is to send the latlng along with your markers data to the Map Component.
Change to
<MyMapComponent
markers={this.state.markers}
latlng={this.state.latlng}
/>
and
<GoogleMap
defaultZoom={8}
defaultCenter={props.latlng}
>
hi @Shawn Andres. the map is not shown..
– DevGe
Nov 21 '18 at 8:24
the map turn only on grey box.. so it means their is no lat and lng value..
– DevGe
Nov 21 '18 at 8:30
Your componentDidMount is called after the component is initially rendered, so you're rendering the map without waiting to get the map API data. I recommend deletingcomponentWillMount() { this.setState({ markers: }) }
and renaming componentDidMount to componentWillMount.
– Shawn Andrews
Nov 21 '18 at 8:40
view my new update codes.
– DevGe
Nov 21 '18 at 8:42
in your updated code you send the propslat_value_props
but in the component you useprops.lat_value
instead. Not sure if you intended that. Does your map still give a grey box with{{lat:53.569967, lng:-113.393261}}
?
– Shawn Andrews
Nov 21 '18 at 8:48
|
show 3 more comments
The error is because MyMapComponent is not a React.Component and therefore this.state does not exist.
The solution is to send the latlng along with your markers data to the Map Component.
Change to
<MyMapComponent
markers={this.state.markers}
latlng={this.state.latlng}
/>
and
<GoogleMap
defaultZoom={8}
defaultCenter={props.latlng}
>
The error is because MyMapComponent is not a React.Component and therefore this.state does not exist.
The solution is to send the latlng along with your markers data to the Map Component.
Change to
<MyMapComponent
markers={this.state.markers}
latlng={this.state.latlng}
/>
and
<GoogleMap
defaultZoom={8}
defaultCenter={props.latlng}
>
answered Nov 21 '18 at 8:14
Shawn AndrewsShawn Andrews
965718
965718
hi @Shawn Andres. the map is not shown..
– DevGe
Nov 21 '18 at 8:24
the map turn only on grey box.. so it means their is no lat and lng value..
– DevGe
Nov 21 '18 at 8:30
Your componentDidMount is called after the component is initially rendered, so you're rendering the map without waiting to get the map API data. I recommend deletingcomponentWillMount() { this.setState({ markers: }) }
and renaming componentDidMount to componentWillMount.
– Shawn Andrews
Nov 21 '18 at 8:40
view my new update codes.
– DevGe
Nov 21 '18 at 8:42
in your updated code you send the propslat_value_props
but in the component you useprops.lat_value
instead. Not sure if you intended that. Does your map still give a grey box with{{lat:53.569967, lng:-113.393261}}
?
– Shawn Andrews
Nov 21 '18 at 8:48
|
show 3 more comments
hi @Shawn Andres. the map is not shown..
– DevGe
Nov 21 '18 at 8:24
the map turn only on grey box.. so it means their is no lat and lng value..
– DevGe
Nov 21 '18 at 8:30
Your componentDidMount is called after the component is initially rendered, so you're rendering the map without waiting to get the map API data. I recommend deletingcomponentWillMount() { this.setState({ markers: }) }
and renaming componentDidMount to componentWillMount.
– Shawn Andrews
Nov 21 '18 at 8:40
view my new update codes.
– DevGe
Nov 21 '18 at 8:42
in your updated code you send the propslat_value_props
but in the component you useprops.lat_value
instead. Not sure if you intended that. Does your map still give a grey box with{{lat:53.569967, lng:-113.393261}}
?
– Shawn Andrews
Nov 21 '18 at 8:48
hi @Shawn Andres. the map is not shown..
– DevGe
Nov 21 '18 at 8:24
hi @Shawn Andres. the map is not shown..
– DevGe
Nov 21 '18 at 8:24
the map turn only on grey box.. so it means their is no lat and lng value..
– DevGe
Nov 21 '18 at 8:30
the map turn only on grey box.. so it means their is no lat and lng value..
– DevGe
Nov 21 '18 at 8:30
Your componentDidMount is called after the component is initially rendered, so you're rendering the map without waiting to get the map API data. I recommend deleting
componentWillMount() { this.setState({ markers: }) }
and renaming componentDidMount to componentWillMount.– Shawn Andrews
Nov 21 '18 at 8:40
Your componentDidMount is called after the component is initially rendered, so you're rendering the map without waiting to get the map API data. I recommend deleting
componentWillMount() { this.setState({ markers: }) }
and renaming componentDidMount to componentWillMount.– Shawn Andrews
Nov 21 '18 at 8:40
view my new update codes.
– DevGe
Nov 21 '18 at 8:42
view my new update codes.
– DevGe
Nov 21 '18 at 8:42
in your updated code you send the props
lat_value_props
but in the component you use props.lat_value
instead. Not sure if you intended that. Does your map still give a grey box with {{lat:53.569967, lng:-113.393261}}
?– Shawn Andrews
Nov 21 '18 at 8:48
in your updated code you send the props
lat_value_props
but in the component you use props.lat_value
instead. Not sure if you intended that. Does your map still give a grey box with {{lat:53.569967, lng:-113.393261}}
?– Shawn Andrews
Nov 21 '18 at 8:48
|
show 3 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407341%2fgooglemap-reactjs-how-to-pass-the-value-of-state-into-defaultcenter-of-googlema%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
console.log(this.state.latlng); it gives me right position.
– DevGe
Nov 21 '18 at 7:51
Is your <GoogleMap> in the render() function?
– Shawn Andrews
Nov 21 '18 at 7:51
@ShawnAndrews nope in the top of class
– DevGe
Nov 21 '18 at 7:52
it look like this . <GoogleMap defaultZoom={8} defaultCenter={this.state.latlng} > {props.markers.map(marker => ( <Marker key={marker.id} position={{ lat: parseFloat(marker.store_lat), lng: parseFloat(marker.store_long) }} /> ))} </GoogleMap> ); export default class FindStore extends Component {
– DevGe
Nov 21 '18 at 7:52
can you edit your post to include the full class?
– Shawn Andrews
Nov 21 '18 at 7:54