Gathering detailed insights and metrics for google-maps-react-markers
Gathering detailed insights and metrics for google-maps-react-markers
Gathering detailed insights and metrics for google-maps-react-markers
Gathering detailed insights and metrics for google-maps-react-markers
Google Maps library that accepts markers as react components and works with React 18+.
npm install google-maps-react-markers
85.9
Supply Chain
93.3
Quality
79
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
147 Stars
392 Commits
19 Forks
3 Watching
11 Branches
5 Contributors
Updated on 17 Oct 2024
TypeScript (92.1%)
JavaScript (6.66%)
Shell (1.24%)
Cumulative downloads
Total Downloads
Last day
9.4%
4,460
Compared to previous day
Last week
3.5%
23,171
Compared to previous week
Last month
32.7%
96,137
Compared to previous month
Last year
327.4%
649,268
Compared to previous year
39
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 ❤️
Released TypeScript version of the library!
Feedbacks are welcome in this discussion.
See it in action here (API KEY not provided).
Demo source code is available here.
1pnpm add google-maps-react-markers
or
1yarn add google-maps-react-markers
or
1npm install --save google-maps-react-markers
1import GoogleMap from 'google-maps-react-markers' 2 3const App = () => { 4 const mapRef = useRef(null) 5 const [mapReady, setMapReady] = useState(false) 6 7 /** 8 * @description This function is called when the map is ready 9 * @param {Object} map - reference to the map instance 10 * @param {Object} maps - reference to the maps library 11 */ 12 const onGoogleApiLoaded = ({ map, maps }) => { 13 mapRef.current = map 14 setMapReady(true) 15 } 16 17 const onMarkerClick = (e, { markerId, lat, lng }) => { 18 console.log('This is ->', markerId) 19 20 // inside the map instance you can call any google maps method 21 mapRef.current.setCenter({ lat, lng }) 22 // ref. https://developers.google.com/maps/documentation/javascript/reference?hl=it 23 } 24 25 return ( 26 <> 27 {mapReady && <div>Map is ready. See for logs in developer console.</div>} 28 <GoogleMap 29 apiKey="" 30 defaultCenter={{ lat: 45.4046987, lng: 12.2472504 }} 31 defaultZoom={5} 32 options={mapOptions} 33 mapMinHeight="100vh" 34 onGoogleApiLoaded={onGoogleApiLoaded} 35 onChange={(map) => console.log('Map moved', map)} 36 > 37 {coordinates.map(({ lat, lng, name }, index) => ( 38 <Marker 39 key={index} 40 lat={lat} 41 lng={lng} 42 markerId={name} 43 onClick={onMarkerClick} // you need to manage this prop on your Marker component! 44 // draggable={true} 45 // onDragStart={(e, { latLng }) => {}} 46 // onDrag={(e, { latLng }) => {}} 47 // onDragEnd={(e, { latLng }) => {}} 48 /> 49 ))} 50 </GoogleMap> 51 </> 52 ) 53} 54 55export default App
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'} |
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. |
For clustering, follow this guide using useSupercluster Hook, but use bounds in this way:
1const [mapBounds, setMapBounds] = useState({ 2 bounds: [0, 0, 0, 0], 3 zoom: 0, 4}) 5const onMapChange = ({ bounds, zoom }) => { 6 const ne = bounds.getNorthEast() 7 const sw = bounds.getSouthWest() 8 /** 9 * useSupercluster accepts bounds in the form of [westLng, southLat, eastLng, northLat] 10 * const { clusters, supercluster } = useSupercluster({ 11 * points: points, 12 * bounds: mapBounds.bounds, 13 * zoom: mapBounds.zoom, 14 * }) 15 */ 16 setMapBounds({ ...mapBounds, bounds: [sw.lng(), sw.lat(), ne.lng(), ne.lat()], zoom }) 17}
To run the project locally, clone the repo and run:
1# in the root directory 2yarn --frozen-install 3yarn dev
1# in another tab (with NextJS and SSR) 2cd docs 3yarn install 4yarn 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.
MIT © giorgiabosello
Developed with ❤️ in Italy 🇮🇹
No vulnerabilities found.
No security vulnerabilities found.