Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
lalitlouis committed Sep 18, 2024
1 parent e07ca76 commit 7895ec1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
26 changes: 10 additions & 16 deletions netlify/functions/googlePlaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ exports.handler = async function(event, context) {
return { statusCode: 405, body: 'Method Not Allowed' };
}

const { action, zipCode, placeId } = queryStringParameters;
const { action, zipCode, lat, lng, placeId } = queryStringParameters;

try {
let url, params;

if (action === 'nearbySearch') {
// First, we need to convert ZIP code to coordinates
const geocodeUrl = `https://maps.googleapis.com/maps/api/geocode/json?address=${zipCode}&key=${API_KEY}`;
const geocodeResponse = await axios.get(geocodeUrl);
const { lat, lng } = geocodeResponse.data.results[0].geometry.location;

if (action === 'geocode') {
url = 'https://maps.googleapis.com/maps/api/geocode/json';
params = {
address: zipCode,
key: API_KEY
};
} else if (action === 'nearbySearch') {
url = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json';
params = {
location: `${lat},${lng}`,
Expand All @@ -33,15 +34,8 @@ exports.handler = async function(event, context) {
fields: 'name,rating,formatted_phone_number,formatted_address,website,reviews,user_ratings_total,price_level',
key: API_KEY
};
} else if (action === 'geocode') {
url = 'https://maps.googleapis.com/maps/api/geocode/json';
params = {
address: zipCode,
key: API_KEY
};
}
else {
return { statusCode: 400, body: 'Invalid action' };
} else {
return { statusCode: 400, body: 'Invalid action' };
}

const response = await axios.get(url, { params });
Expand Down
8 changes: 4 additions & 4 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ function getNodeColor(place) {
return "#FF5722";
}

function deg2rad(deg) {
return deg * (Math.PI/180);
}

function calculateDistance(lat1, lon1, lat2, lon2) {
const R = 6371; // Radius of the earth in km
const dLat = deg2rad(lat2 - lat1);
Expand Down Expand Up @@ -269,10 +273,6 @@ document.getElementById('searchButton').addEventListener('click', async () => {
} catch (error) {
console.error('Error fetching data:', error);
d3.select("#graph").html(`<p>Error fetching data: ${error.message}</p>`);
if (error.details) {
console.error('Error details:', error.details);
d3.select("#graph").append("p").text(`Error details: ${error.details}`);
}
}
});

Expand Down

0 comments on commit 7895ec1

Please sign in to comment.