Next.js Discord

Discord Forum

google places api not loading

Unanswered
Karlo posted this in #help-forum
Open in Discord
Guys I need help. Im working on a next.js and react project and im trying to use the google places api. Im usually not a react dev, more c# but I got this project and my friend bought this template so I'm trying to edit it with as little react experience I have. The current error I get is just the google places api not loading, this is the place picker component I have:
// PlacePicker.jsx
"use client";
import { useState, useEffect } from "react";
import { Autocomplete } from "@react-google-maps/api";

export default function PlacePicker() {
const [autocomplete, setAutocomplete] = useState(null);
const [isLoaded, setIsLoaded] = useState(false);

useEffect(() => {
const interval = setInterval(() => {
if (typeof window !== 'undefined') {
window.initMap = () => console.log('Google Maps API loaded successfully');
}
}, 100); // Check every 100 milliseconds

return () => clearInterval(interval);
}, []);

if (!isLoaded) {
return <div>Loading Google Maps...</div>;
}

const onLoad = (autocompleteInstance) => {
console.log("Autocomplete component loaded");
setAutocomplete(autocompleteInstance);
};

const onPlaceChanged = () => {
if (autocomplete) {
console.log(autocomplete.getPlace());
} else {
console.log("Autocomplete is not loaded yet!");
}
};

return (
<Autocomplete
onLoad={onLoad}
onPlaceChanged={onPlaceChanged}
restrictions={{ country: "hr" }}
>
<input
type="text"
placeholder="Enter location"
className="search-input dropdown-location"
/>
</Autocomplete>
);
}

1 Reply

and this is where im calling the api, in my home page which calls the hero component that then displays it:
import Footer2 from "@/components/footers/Footer2";
import Header2 from "@/components/headers/Header2";

import Facts from "@/components/homes/home-2/Facts";
import Feet from "@/components/homes/home-2/Feet";
import Hero from "@/components/homes/home-2/Hero";
import Head from 'next/head';
import Process from "@/components/common/process/Process2";
import Service from "@/components/homes/home-2/Service";
import Testimonials from "@/components/homes/home-2/Testimonials";
import MobailHeader1 from "@/components/headers/MobailHeader1";
export const metadata = {
title:
"Taxi Sukosan",
description:
"Luxurious taxi rides all across Zadar",
};


export default function page() {
return (
#Unknown Channel
<Head>

<script
src={https://maps.googleapis.com/maps/api/js?key=${process.env.NEXT_PUBLIC_GOOGLE_MAPS_API_KEY}&callback=initMap&libraries=places}
async
defer
/>
</Head>
<Header2 /> <MobailHeader1 />
<main className="main">
<Hero />
<Process />
<Service />
<Feet />
<Facts />
<Testimonials />
</main>
<Footer2 />
</>
);
}

also ive noticed that if I remove the if (!isLoaded) {
return <div>Loading Google Maps...</div>;
}
I just get the google not defined error
Also i belive the google api key is right becuase if i click it and follow it link it gives me a response in the browser