Adding Marker to Google Maps in google-map-react

前端 未结 4 1478
耶瑟儿~
耶瑟儿~ 2020-12-14 15:53

I am using the google-map-react NPM package to create a Google Map and several markers.

Question: How can we add the default Google Maps markers to

相关标签:
4条回答
  • 2020-12-14 16:08

    This may not be entirely clear from the description in the Readme, but the maps argument is, in fact, the maps API object (and map is, of course, the current Google Map instance). Therefore, you should pass both to your method:

    onGoogleApiLoaded={({map, maps}) => this.renderMarkers(map, maps)}
    

    and use them:

    renderMarkers(map, maps) {
      let marker = new maps.Marker({
        position: myLatLng,
        map,
        title: 'Hello World!'
      });
    }
    
    0 讨论(0)
  • 2020-12-14 16:08
    import React from 'react';
    import GoogleMapReact from 'google-map-react';
    
    const GoogleMaps = ({ latitude, longitude }) => {
     const renderMarkers = (map, maps) => {
      let marker = new maps.Marker({
      position: { lat: latitude, lng: longitude },
      map,
      title: 'Hello World!'
      });
      return marker;
     };
    
     return (
       <div style={{ height: '50vh', width: '100%' }}>
        <GoogleMapReact
          bootstrapURLKeys={{ key: 'YOUR KEY' }}
          defaultCenter={{ lat: latitude, lng: longitude }}
          defaultZoom={16}
          yesIWantToUseGoogleMapApiInternals
          onGoogleApiLoaded={({ map, maps }) => renderMarkers(map, maps)}
        >
        </GoogleMapReact>
       </div>
     );
    };
    
    export default GoogleMaps;
    
    0 讨论(0)
  • 2020-12-14 16:19

    Adding a marker on your map isn't as easy as we would like to, mostly because the confusing docs but here you have a super easy example:

    const Map = props => {
      return (
        <GoogleMapReact
         bootstrapURLKeys={{ props.key }}
         defaultCenter={{lat: props.lat, lng: props.lng}}
         defaultZoom={props.zoom}>
    
           {/* This is the missing part in docs:
             *
             * Basically, you only need to add a Child Component that
             * takes 'lat' and 'lng' Props. And that Component should
             * returns a text, image, super-awesome-pin (aka, your marker).
             *
             */}
           <Marker lat={props.lat} lng={props.lng}} />
        </GoogleMapReact>
      )
    }
    
    const Marker = props => {
      return <div className="SuperAwesomePin"></div>
    }
    
    0 讨论(0)
  • 2020-12-14 16:23

    Based on the answer by MasterAM.

    If you using react hooks, this one could do the trick:

    import React, { useMemo, useEffect } from 'react';
    
    const createControlledPromise = () => {
      let resolver;
      let rejector;
      const promise = new Promise((resolve, reject) => {
        resolver = resolve;
        rejector = reject;
      });
      return { promise, resolver, rejector };
    };
    
    const useMarker = ({ lat, lng }) => {
      const { 
        promise: apiPromise, 
        resolver: handleGoogleApiLoaded 
      } = useMemo(
        createControlledPromise,
        []
      ).current;
    
      useEffect(
        () => {
          let marker;
          apiPromise.then(api => {
            const { map, maps } = api;
            marker = new maps.Marker({ position: { lat, lng }, map });
          });
          return () => {
            if (marker) {
              marker.setMap(null);
            }
          };
        },
        [lat, lon],
      );
      return { handleGoogleApiLoaded };
    };
    
    const Location = ({
      location: { locationName, lat, lng }
    }) => {
      const { handleGoogleApiLoaded } = useMarker({ lat, lng });
      return (
        <section>
          <h1>{locationName}</h1>
          <p>
            <GoogleMapReact
              bootstrapURLKeys={{ key: __GOOGLE_MAP_API_KEY__ }}
              center={{ lat, lng }}
              defaultZoom={11}
              onGoogleApiLoaded={handleGoogleApiLoaded}
            />
          </p>
        </section>
      );
    };
    
    0 讨论(0)
提交回复
热议问题