Ask Your Question
4

How can "useGoogleMap" be integrated within NextJS?

asked 2023-05-31 06:28:25 +0000

plato gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-05-31 06:56:02 +0000

bukephalos gravatar image

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:

  1. Install the dependencies by running the following command in the terminal:
npm install react-google-maps google-maps-react
  1. Next, create a file "GoogleMaps.js" inside the "pages" folder with the following code:
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>
  );
}
  1. Create a file "constants.js" inside the "config" folder and add the following code:
export const GOOGLE_MAPS_API_KEY = "YOUR_API_KEY_HERE";
  1. In the "next.config.js" file, add the following code to allow the use of environment variables:
module.exports = {
  env: {
    GOOGLE_MAPS_API_KEY: process.env.GOOGLE_MAPS_API_KEY
  }
}
  1. Add your Google Maps API key to the ".env.local" file:
GOOGLE_MAPS_API_KEY=YOUR_API_KEY_HERE
  1. Finally, in your "pages" folder, create a file "index.js" and add the following code:
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.

edit flag offensive delete link more

Your Answer

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

Add Answer


Question Tools

Stats

Asked: 2023-05-31 06:28:25 +0000

Seen: 3 times

Last updated: May 31 '23