google maps api react js 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
npm i -S @react-google-maps/api
Example 3: npm google map api react
import {Map, InfoWindow, Marker, GoogleApiWrapper} from 'google-maps-react';
export class MapContainer extends Component {
render() {
return (
<Map google={this.props.google} zoom={14}>
<Marker onClick={this.onMarkerClick}
name={'Current location'} />
<InfoWindow onClose={this.onInfoWindowClose}>
<div>
<h2>{this.state.selectedPlace.name}</h2>
</div>
</InfoWindow>
</Map>
);
}
}
export default GoogleApiWrapper({
apiKey: (YOUR_GOOGLE_API_KEY_GOES_HERE)
})(MapContainer)
Example 4: @react-google-maps/api npm
npm i -S @react-google-maps/api