0

i am using google maps react.

I am trying to fetch data on latitude and longitude from my APIinto the Google Maps as markers.

Getting an error "TypeError: markers.map is not a function"

here is code snippet -

  const [markers,setmarkers] = useState([]);

  const GetCoordinates = (info) => {
       setmarkers(info);} 

  function GetMaps(props){
     const AnyComponent = ({text}) => <div>{text}</div>
     defaultmarkers = {center:{lat: xxxx, lng: xxxx}, zoom: 10}
   }

   return (<div style={{height:'100vh', width:'100vw' }}>
     <GoogleMapReact
       bootstrapURLKeys = xxxxxx
       defaultcenter= {defaultmarkers.center}
       defaultZoom= {defaultmarkers.zoom}>

       <AnyComponent>
          <ul>
           {markers.map(x => (   //////getting error on this line
            <li key={x.id}>
               lat={x.lat}  // pull from API data
               lng={x.lng} // pull info from API
               text={x.title}
              </li>))}</ul>
       </AnyComponent><GoogleMapReact>

       <GetMaps /> // function called in return tag

Getting an error "TypeError: markers.map is not a function"

oceans
  • 5
  • 1
  • 3
  • So I converted the markers into an array using Object.keys and now it gives me an error saying "Warning: Each child in a list should have a unique "Key" prop" I have added
  • – oceans Jul 10 '19 at 19:30
  • I have yet to use the google maps api, but, I was having the same issue with mapping my fetch returns, it turns out that I needed to use a async function, then you await the response. Hope that helps. – tim Jul 10 '19 at 21:27