npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

google-maps-react-markers

v2.0.11

Published

Google Maps library that accepts markers as react components, works with React 18+ and it is fully typed.

Downloads

91,685

Readme

Google Maps library that accepts markers as react components and works with React 18+.

It supports a small set of the props of Google Map React. Clustering also is possible. The library implements Google Maps Custom Overlays official library.

If you like this library, please consider supporting me ❤️

Buy me a Coffee PayPal

💬 Discussion

Released TypeScript version of the library!

Feedbacks are welcome in this discussion.

🚀 Demo

See it in action here (API KEY not provided).

Demo source code is available here.

🛠 Install

pnpm add google-maps-react-markers

or

yarn add google-maps-react-markers

or

npm install --save google-maps-react-markers

💻 Usage

import GoogleMap from 'google-maps-react-markers'

const App = () => {
  const mapRef = useRef(null)
  const [mapReady, setMapReady] = useState(false)

  /**
   * @description This function is called when the map is ready
   * @param {Object} map - reference to the map instance
   * @param {Object} maps - reference to the maps library
   */
  const onGoogleApiLoaded = ({ map, maps }) => {
    mapRef.current = map
    setMapReady(true)
  }

  const onMarkerClick = (e, { markerId, lat, lng }) => {
    console.log('This is ->', markerId)

    // inside the map instance you can call any google maps method
    mapRef.current.setCenter({ lat, lng })
    // ref. https://developers.google.com/maps/documentation/javascript/reference?hl=it
  }

  return (
    <>
      {mapReady && <div>Map is ready. See for logs in developer console.</div>}
      <GoogleMap
        apiKey=""
        defaultCenter={{ lat: 45.4046987, lng: 12.2472504 }}
        defaultZoom={5}
        options={mapOptions}
        mapMinHeight="100vh"
        onGoogleApiLoaded={onGoogleApiLoaded}
        onChange={(map) => console.log('Map moved', map)}
      >
        {coordinates.map(({ lat, lng, name }, index) => (
          <Marker
            key={index}
            lat={lat}
            lng={lng}
            markerId={name}
            onClick={onMarkerClick} // you need to manage this prop on your Marker component!
            // draggable={true}
            // onDragStart={(e, { latLng }) => {}}
            // onDrag={(e, { latLng }) => {}}
            // onDragEnd={(e, { latLng }) => {}}
          />
        ))}
      </GoogleMap>
    </>
  )
}

export default App

🧐 Props

GoogleMap component

| Prop | Type | Required | Default | Description | | -------------------- | -------- | -------- | --------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | apiKey | string | yes | '' | API Key to load Google Maps | | defaultCenter | object | yes | { lat: 0, lng: 0 } | Default center of the map | | defaultZoom | number | yes | 1-20 | Default zoom of the map | | libraries | array | no | ['places', 'geometry'] | Libraries to load | | options | object | no | {} | Options for the map | | onGoogleApiLoaded | function | no | () => {} | Callback when the map is loaded | | onChange | function | no | () => {} | Callback when the map has changed | | events | array | no | [] | Array of objects name/handler of DOM events to pass down to the div overlay. Example: events: [{ name: 'onClick', handler: () => {} }] | | children | node | no | null | Markers of the map | | loadScriptExternally | bool | no | false | Whether to load the Google Maps script externally.If true, the status prop is required and it will be used to control the loading of the script | | status | string | no | idle | The forced status of the Google Maps script. Depends on loadScriptExternally.It can be one of idle, loading, ready, error | | loadingContent | node | no | 'Google Maps is loading' | Content to show while the map is loading | | idleContent | node | no | 'Google Maps is on idle' | Content to show when the map is idle | | errorContent | node | no | 'Google Maps is on error' | Content to show when the map has an error | | mapMinHeight | string | no | 'unset' | Min height of the map | | containerProps | object | no | {} | Props for the div container of the map | | scriptCallback | function | no | () => {} | window global callback passed to the Google Script | | externalApiParams | object | no | undefined | Optional params to pass to the Google API script. Eg. {region: 'IT', language: 'it'} |

Markers

| Prop | Type | Required | Default | Description | | ----------- | ------ | -------- | ----------- | -------------------------------------------------------------------------------------------------------- | | lat | number | yes | undefined | Latitude of the marker | | lng | number | yes | undefined | Longitude of the marker | | draggable | bool | no | false | If true, the marker can be dragged | | onDragStart | func | no | () => {} | This event is fired when the user starts dragging the marker | | onDrag | func | no | () => {} | This event is repeatedly fired while the user drags the marker | | onDragEnd | func | no | () => {} | This event is fired when the user stops dragging the marker | | zIndex | number | no | 0 | The z-index of the marker. To bring the marker to the front, e.g. when it is active, set a higher value. |

📍 Clustering

For clustering, follow this guide using useSupercluster Hook, but use bounds in this way:

const [mapBounds, setMapBounds] = useState({
  bounds: [0, 0, 0, 0],
  zoom: 0,
})
const onMapChange = ({ bounds, zoom }) => {
  const ne = bounds.getNorthEast()
  const sw = bounds.getSouthWest()
  /**
   * useSupercluster accepts bounds in the form of [westLng, southLat, eastLng, northLat]
   * const { clusters, supercluster } = useSupercluster({
   *	points: points,
   *	bounds: mapBounds.bounds,
   *	zoom: mapBounds.zoom,
   * })
   */
  setMapBounds({ ...mapBounds, bounds: [sw.lng(), sw.lat(), ne.lng(), ne.lat()], zoom })
}

👥 Contributing

To run the project locally, clone the repo and run:

# in the root directory
yarn --frozen-install
yarn dev
# in another tab (with NextJS and SSR)
cd docs
yarn install
yarn dev

Do your changes to src/ or docs/src directory, commits all files in the root directory (yarn.lock and ones in dist too) and open a PR.

💻 Built with

🗒 License

MIT © giorgiabosello

🙏 Support

Buy me a Coffee PayPal