Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Markers not working with google-map-react

I'm working with the 'google-map-react' library and I have tried all but the markers are not showing up. I pass the coords to the marker in many ways but none worked. Here's my code & repository:

https://github.com/jorginyu/ubica

import React, { Component } from 'react';
import GoogleMapReact from 'google-map-react';

const API_KEY = 'WTFULOOKINAT';

const contacts = [
  { name: 'Spiderman', lat: 41.529616, lng: 2.434130 },
  { name: 'Iron Man', lat: 41.528103, lng: 2.433834 },
  { name: 'Hulk', lat: 41.530192, lng: 2.422994 }
];

const MarkersC = (text ) => <div className="contact">{text}</div>;

export default class MapComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      center: {
        lat: 41.528452,
        lng: 2.434195
      },
      zoom: 18
    }
  }


  render() {
    return (
      // Important! Always set the container height explicitly
      <div className="mt-5" style={{ height: '80vh', width: '100%' }}>
        <GoogleMapReact
          bootstrapURLKeys={{ key: API_KEY }}
          defaultCenter={this.state.center}
          defaultZoom={this.state.zoom}
        >
            {contacts.map((contact,i) => {
            <MarkersC  position={{lat: contact.lat, lng: contact.lng}} text={contact.name} key={i} />
          })}

        </GoogleMapReact>
      </div>
    );
  }
}

What can I do? Thanks for your time :)

like image 649
Donutworry Avatar asked Feb 27 '26 15:02

Donutworry


1 Answers

There is a formatting problem. I deleted the spaces:

THEN:

          {
            contacts.map((contact, i) => 
              <MarkersC lat={contact.lat} lng={contact.lng} text={contact.name} key={i} />
            )
          }

NOW:

          {
            contacts.map((contact, i) => <MarkersC lat={contact.lat} lng={contact.lng} text={contact.name} key={i} /> )
          }
like image 59
Donutworry Avatar answered Mar 02 '26 05:03

Donutworry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!