Dynamically Adding Markers on google-map-react

前端 未结 2 1151
耶瑟儿~
耶瑟儿~ 2020-12-16 07:59

What I wan\'t to do is to show the location picked from some mobile devices on the Map. Data about the locations are there..

What I need here is to add Markers on t

相关标签:
2条回答
  • 2020-12-16 08:36

    Be careful. You said react-google-map but you are using google-map-react. Those are 2 different packages. Do not mix up their documentation.

    0 讨论(0)
  • 2020-12-16 08:37

    You may try:

    import React, { Component } from 'react';
    import GoogleMapReact from 'google-map-react';
    
    const AnyReactComponent = ({  img_src }) => <div><img src={img_src} className="YOUR-CLASS-NAME" style={{}} /></div>;
    
    class MyClass extends Component {
      constructor(props){
        super(props);
        this.state = {
            markers: [],
        }
      }
    
      componentDidMount(){
        // or you can set markers list somewhere else
        // please also set your correct lat & lng
        // you may only use 1 image for all markers, if then, remove the img_src attribute ^^
        this.setState({
          markers: [{lat: xxxx, lng: xxxx, img_src: 'YOUR-IMG-SRC'},{lat: xxxx, lng: xxxx, img_src: 'YOUR-IMG-SRC' },{lat: xxxx, lng: xxxx,  img_src: 'YOUR-IMG-SRC'}],
        });
      }
    
      render() {
        return (
          <GoogleMapReact
            defaultCenter={this.props.center}
            defaultZoom={this.props.zoom}
            style={{height: '300px'}}
          >
                {this.state.markers.map((marker, i) =>{
                  return(
                    <AnyReactComponent
                      lat={marker.lat}
                      lng={marker.lng}
                      img_src={marker.img_src}
                    />
    
                  )
                })}      
          </GoogleMapReact>
        );
      }
    }
    MyClass.defaultProps = {
      center: {lat: 59.95, lng: 30.33},
      zoom: 11
    };
    

    If this has error, please show here too, then we can fix it later

    ===========

    ADDED EXAMPLE FOR CLICK-EVENT ON MARKERS

     markerClicked(marker) {
       console.log("The marker that was clicked is", marker);
       // you may do many things with the "marker" object, please see more on tutorial of the library's author:
      // https://github.com/istarkov/google-map-react/blob/master/API.md#onchildclick-func 
      // Look at their examples and you may have some ideas, you can also have the hover effect on markers, but it's a bit more complicated I think 
     }
    
     render() {
        return (
          <GoogleMapReact
            defaultCenter={this.props.center}
            defaultZoom={this.props.zoom}
            style={{height: '300px'}}
          >
                {this.state.markers.map((marker, i) =>{
                  return(
                    <AnyReactComponent
                      lat={marker.lat}
                      lng={marker.lng}
                      img_src={marker.img_src}
                      onChildClick={this.markerClicked.bind(this, marker)}
                    />
    
                  )
                })}      
    
          </GoogleMapReact>
        );
      }
    

    Once again, post here some errors if any ^^ !

    0 讨论(0)
提交回复
热议问题