
Security News
/Research
Popular node-ipc npm Package Infected with Credential Stealer
Socket detected malicious node-ipc versions with obfuscated stealer/backdoor behavior in a developing npm supply chain attack.
@ttoss/google-maps
Advanced tools
@ttoss/google-maps provides a concise, opinionated way to use Google Maps in React apps. This guide covers setup, usage, and key API features so you can get started quickly.
@ttoss/google-maps provides a concise, opinionated way to use Google Maps in React apps. This guide covers setup, usage, and key API features so you can get started quickly.
Install the package:
pnpm add @ttoss/google-maps
For TypeScript support:
pnpm add -D @types/google.maps
Add this to a declaration file (e.g., typings.d.ts):
/// <reference types="google.maps" />
Set up GoogleMapsProvider at your app root to provide the Google Maps context. This enables all child components to access the Google Maps API.
import { GoogleMapsProvider } from '@ttoss/google-maps';
const App = ({ children }) => (
<GoogleMapsProvider apiKey={process.env.GOOGLE_MAPS_API_KEY}>
{children}
</GoogleMapsProvider>
);
Use the useMap hook to render a map in your component. Define height and width for the map container:
import { Box, Text } from '@ttoss/ui';
import { useMap } from '@ttoss/google-maps';
import * as React from 'react';
const height = 400;
const width = '100%';
const MyComponent = () => {
const { ref, map } = useMap();
React.useEffect(() => {
if (map) {
map.setOptions({
center: { lat: -34.397, lng: 150.644 },
zoom: 8,
});
}
}, [map]);
return (
<Box>
<Text>My Map</Text>
<Box ref={ref} sx={{ height, width }} />
</Box>
);
};
google.maps ObjectUse the useGoogleMaps hook to access the google.maps object for advanced API usage:
import { useGoogleMaps } from '@ttoss/google-maps';
const MyComponent = () => {
const { google } = useGoogleMaps();
return <Text>{google.maps.version}</Text>;
};
If you use Next.js, pass the Next.js Script component to GoogleMapsProvider:
import { GoogleMapsProvider } from '@ttoss/google-maps';
import Script from 'next/script';
const App = ({ children }) => (
<GoogleMapsProvider apiKey={process.env.GOOGLE_MAPS_API_KEY} Script={Script}>
{children}
</GoogleMapsProvider>
);
map ObjectUse MapProvider to share the map object between components:
import { MapProvider, useMap } from '@ttoss/google-maps';
const ChildComponent = () => {
const { map } = useMap();
React.useEffect(() => {
if (map) {
map.setOptions({
center: { lat: -34.397, lng: 150.644 },
zoom: 8,
});
}
}, [map]);
return null;
};
const ParentComponent = () => {
const { ref, map } = useMap();
const height = 400;
const width = '100%';
return (
<MapProvider map={map} ref={ref}>
<Box>
<Text>My Map</Text>
<Box ref={ref} sx={{ height, width }} />
<ChildComponent />
</Box>
</MapProvider>
);
};
To use markers, include the marker library in GoogleMapsProvider:
<GoogleMapsProvider
apiKey={process.env.GOOGLE_MAPS_API_KEY}
libraries={['marker']}
>
{children}
</GoogleMapsProvider>
Add a marker using google.maps.marker.AdvancedMarkerElement:
import { useMap, useGoogleMaps } from '@ttoss/google-maps';
import React from 'react';
const height = 400;
const width = '100%';
const MyMapWithMarker = ({ location }) => {
const { ref, map } = useMap();
const { google } = useGoogleMaps();
const markerRef = React.useRef(null);
React.useEffect(() => {
if (map) {
map.setOptions({
center: {
lat: location.latitude,
lng: location.longitude,
},
zoom: location.zoom || 13,
});
}
if (google?.maps && map && location) {
const marker = new google.maps.marker.AdvancedMarkerElement({
position: {
lat: location.latitude,
lng: location.longitude,
},
map,
title: location.name,
});
markerRef.current = marker;
}
}, [map, location, google]);
return <div ref={ref} style={{ height, width }} />;
};
You can handle script loading errors using the onError prop in GoogleMapsProvider:
<GoogleMapsProvider
apiKey={process.env.GOOGLE_MAPS_API_KEY}
onError={(error) => {
// Handle error
console.error(error);
}}
>
{children}
</GoogleMapsProvider>
GoogleMapsProviderapiKey: string - Google Maps API key.libraries: string[] - Libraries to load.language: string - Language.Script: React.ComponentType - Custom Script component to use.onError: (error: Error) => void - Callback to handle script loading errors.MapProvidermap: google.maps.Map | null - Google Maps object.children: React.ReactNode - Children to render.ref: React.RefObject<HTMLDivElement> - Reference to the map container.useMapReturns:
ref: React.RefObject<HTMLDivElement> - Reference to the map container.map: google.maps.Map | null - Google Maps object.useGoogleMapsReturns:
google: typeof google - google.maps object.status: 'idle' | 'error' | 'loading' | 'ready' - Status of the script loading.isReady: boolean - Whether the script is ready (status === 'ready').For more on product development principles that guide our approach, see Product Development Principles.
FAQs
@ttoss/google-maps provides a concise, opinionated way to use Google Maps in React apps. This guide covers setup, usage, and key API features so you can get started quickly.
The npm package @ttoss/google-maps receives a total of 342 weekly downloads. As such, @ttoss/google-maps popularity was classified as not popular.
We found that @ttoss/google-maps demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
/Research
Socket detected malicious node-ipc versions with obfuscated stealer/backdoor behavior in a developing npm supply chain attack.

Security News
TeamPCP and BreachForums are promoting a Shai-Hulud supply chain attack contest with a $1,000 prize for the biggest package compromise.

Security News
Packagist urges PHP projects to update Composer after a GitHub token format change exposed some GitHub Actions tokens in CI logs.