Working with react : "cannot read property 'lat' of undefined
Working with react : "cannot read property 'lat' of undefined
I would like to mention it that I am new to react,this is my first try.
For the course I am following I need to create a single-page-app which loads google maps api and shows markers from foursquare.
I have an issue when I try to load up the markers,there is an error "cannot read property 'lat' of undefined,it has been many hours since I am trying to solve this issue and simply I can not get the hack of it. I was wondering if anyone can help.
I read through a lot of questions here but none seem to offer a solution.
I have attached a screenshot of the error and my code.
class App extends Component {
state = {
locations: ,
venueInfo: {},
query: ''
}
componentDidMount() {
this.setState({ isLoading: true });
// Foursquare api request
fetch(`https://api.foursquare.com/v2/venues/explore?ll=34.9003,33.6232&client_id=${Credentials.client_id}&client_secret=${Credentials.client_secret}&v=${Credentials.version_date}`)
.then(repsonse => repsonse.json())
.then(data => {
const locations = data.response.groups[0].items.map(item => {
return {
position: { lat: item.venue.location.lat, lng: item.venue.location.lng },
title: item.venue.name,
id: item.venue.id,
category: item.venue.categories[0].name,
address: item.venue.location.address,
crossStreet: item.venue.location.crossStreet,
state: item.venue.location.state,
coordinates: item.venue.location.lat + ', ' + item.venue.location.lng,
postalCode: item.venue.location.postalCode
}
})
this.setState({ locations })
})
.catch(err => {
console.log('Failed to fetch foursquare data', err)
})
}
import { Map, InfoWindow, Marker, GoogleApiWrapper } from 'google-maps-react'
import React, { Component } from 'react'
import PropTypes from 'prop-types'
export class MapContainer extends Component {
state = {
activeMaker: {},
selectedPlace: {},
showingInfoWindow: false
}
render() {
const bound = new this.props.google.maps.LatLngBounds()
for (let i = 0; i < this.props.locations.length; i++) {
bound.extend(this.props.locations[i].position)
}
return (
<Map
onClick={this.onMapClicked}
google={this.props.google}
initialCenter={{
lat: 34.9003,
lng: 33.6232
}}
zoom={15}
bounds={bound}>
return (
<Marker
key={window.location.id}
position={{ lat: window.location.position.lat, lng: window.location.position.lng }}
title={window.location.title}
onClick={this.onMarkerClick}
animation={this.props.google.maps.Animation.Fo}
category={window.location.category}
address={window.location.address}
crossStreet={window.location.crossStreet}
state={window.location.state}
coordinates={window.location.coordinates}
postalCode={window.location.postalCode} />
)
{/* <InfoWindow onClose={this.onInfoWindowClose}>
<div>
<h1>{this.state.selectedPlace.name}</h1>
</div>
</InfoWindow>*/}
</Map>
);
}
}
export default GoogleApiWrapper({
apiKey: 'AIzaSyDX5cjWZkxQXkCByGJKqVYes-K2_gMn2yQ'
})(MapContainer)
window.location.position
position
lat
undefined
Hy,thank you for the reply.I did wrote location.position at first but than es-lint throw an error that location can not be used ,I found the solution here that by adding window.location the error will be solved , well the es-lint error did disappeared but instead came the above error, can you advice how to solve the issue with es lint than? thank you this is the error that comes up "Line 32: Unexpected use of 'location' no-restricted-globals"
– melinda
Jun 30 at 16:50
To me it looks like you would like to
map over all the locations in your state and create a Marker for each one.– Tholle
Jun 30 at 16:58
map
locations
Marker
Yes indeed I tried to map over the locations,I just realised that i copied my code wrong and it is missing the map function.However I am getting the same error with the location.Maybe you got any idea how else I could access the locations?
– melinda
Jul 1 at 10:34
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Are you sure you want to use
window.location.position, and not the locations sent in to the component as props? window.location does not have apositionproperty, so you are trying to readlatfromundefined.– Tholle
Jun 30 at 16:37