const apiKey = process.env.NEXT_PUBLIC_GOOGLE_MAPS_API_KEY ?? "";
searchPlacesByLocationNew: publicProcedure
.input(z.object({ location: z.string() }))
.mutation(async ({ input }) => {
const apiUrl = `https://places.googleapis.com/v1/places:searchNearby`;
const body = JSON.stringify({
includedTypes: ["restaurant"],
maxResultCount: 10,
locationRestriction: {
circle: {
center: {
latitude: 51.5074,
longitude: -0.1278,
},
radius: 500.0,
},
},
});
try {
const options = {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Goog-Api-Key": apiKey,
"X-Goog-FieldMask": "places.displayName",
},
body,
};
const response = await fetch(apiUrl, options);
console.log("This is response", response);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = (await response.json()) as NearbySearchResponse;
console.log("This is data", data);
return data;
} catch (error) {
console.error("Error fetching data from Google Places API:", error);
return { error: "Failed to fetch data from Google" };
}
}),
const apiKey = process.env.NEXT_PUBLIC_GOOGLE_MAPS_API_KEY ?? "";
searchPlacesByLocationNew: publicProcedure
.input(z.object({ location: z.string() }))
.mutation(async ({ input }) => {
const apiUrl = `https://places.googleapis.com/v1/places:searchNearby`;
const body = JSON.stringify({
includedTypes: ["restaurant"],
maxResultCount: 10,
locationRestriction: {
circle: {
center: {
latitude: 51.5074,
longitude: -0.1278,
},
radius: 500.0,
},
},
});
try {
const options = {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Goog-Api-Key": apiKey,
"X-Goog-FieldMask": "places.displayName",
},
body,
};
const response = await fetch(apiUrl, options);
console.log("This is response", response);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = (await response.json()) as NearbySearchResponse;
console.log("This is data", data);
return data;
} catch (error) {
console.error("Error fetching data from Google Places API:", error);
return { error: "Failed to fetch data from Google" };
}
}),