To integrate "useGoogleMap" within NextJS, you first need to install the required dependencies including "react-google-maps" and "google-maps-react".
Here are the steps you can follow:
npm install react-google-maps google-maps-react
import React, { useState } from "react";
import { withGoogleMap, GoogleMap, Marker } from "react-google-maps";
import { GOOGLE_MAPS_API_KEY } from "../config/constants";
const Map = withGoogleMap(props => (
<GoogleMap defaultZoom={8} defaultCenter={{ lat: props.lat, lng: props.lng }}>
<Marker position={{ lat: props.lat, lng: props.lng }} />
</GoogleMap>
));
export default function GoogleMaps() {
const [position, setPosition] = useState({ lat: 40.7128, lng: -74.006 });
const handleGetCurrentPosition = () => {
navigator.geolocation.getCurrentPosition(
pos => {
setPosition({
lat: pos.coords.latitude,
lng: pos.coords.longitude
});
},
err => console.log(err)
);
}
return (
<div>
<button onClick={handleGetCurrentPosition}>Get Location</button>
<Map
lat={position.lat}
lng={position.lng}
containerElement={<div style={{ height: `400px` }} />}
mapElement={<div style={{ height: `100%` }} />}
googleMapURL={`https://maps.googleapis.com/maps/api/js?key=${GOOGLE_MAPS_API_KEY}`}
/>
</div>
);
}
export const GOOGLE_MAPS_API_KEY = "YOUR_API_KEY_HERE";
module.exports = {
env: {
GOOGLE_MAPS_API_KEY: process.env.GOOGLE_MAPS_API_KEY
}
}
GOOGLE_MAPS_API_KEY=YOUR_API_KEY_HERE
import GoogleMaps from "./GoogleMaps";
export default function Home() {
return (
<div>
<GoogleMaps />
</div>
);
}
This will allow you to use "useGoogleMap" within your NextJS application. Note that the "GoogleMaps.js" file includes the "useGoogleMap" hook which handles the initialization of the Google Map component. The "google-maps-react" library is used to provide the Google Map component.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-05-31 06:28:25 +0000
Seen: 3 times
Last updated: May 31 '23
How can I deal with Expression.Error related to a column in Power Query?
How can you implement pagination in Oracle for the LISTAGG() function?
What is the process for implementing a FutureBuilder on an OnTap function in Flutter?
How can we require users to be logged in before they can access the root folders in WordPress?
In SCSS, what is the method for grouping and reusing a set of classes and styles?
How can popen() be used to direct streaming data to TAR?
How does iOS retrieve information from a BLE device?
How can Django Admin accommodate a variety of formats and locales for its input fields?