google-maps api in react code example
Example 1: react-google-maps
import React from 'react';
import {GoogleMap} from "@react-google-maps/api";
import {useLoadScript} from "@react-google-maps/api";
const mapContainerStyle = {
width: '100vw',
height: '100vh',
}
const center = {
lat: 31.968599,
lng: -99.901810,
}
export default function GoogleMaps() {
const{isLoaded, loadError} = useLoadScript({
});
if (loadError) return "Error loading Maps";
if (!isLoaded) return "Loading Maps";
return(
<GoogleMap
mapContainerStyle={mapContainerStyle}
zoom={11}
center={center}
/>
)
}
Example 2: react google maps api
import React from 'react'
import { GoogleMap, LoadScript } from '@react-google-maps/api';
const containerStyle = {
width: '400px',
height: '400px'
};
const center = {
lat: -3.745,
lng: -38.523
};
function MyComponent() {
return (
<LoadScript
googleMapsApiKey="YOUR_API_KEY"
>
<GoogleMap
mapContainerStyle={containerStyle}
center={center}
zoom={10}
>
{ }
<></>
</GoogleMap>
</LoadScript>
)
}
export default React.memo(MyComponent)