Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map API #3

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions amplify/backend/api/amplifyDatasource/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ type Restaurant @model {
id: ID!
name: String!
rating: Int
photo: String!
aemmadi marked this conversation as resolved.
Show resolved Hide resolved
}

type Room @model {
Expand All @@ -11,6 +12,9 @@ type Room @model {
users: [User]
size: Int
current: Restaurant
lat: Int
long: Int
cuisine: String
}

type User @model {
Expand Down
6 changes: 6 additions & 0 deletions maps/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"PLACES_API_KEY": "None",
"LAT": "None",
"LONG": "None",
"CUISINE": "None"
}
53 changes: 53 additions & 0 deletions maps/map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Library & config imports
const axios = require("axios");
const { DataStore } = require("@aws-amplify/datastore");
const config = require("./config.json");
const { Restaurant, Room } = require("../src/models");

// Config
const PLACES_API_KEY = config.PLACES_API_KEY;
getConfig();

async function getConfig() {
let LAT = await DataStore.query(Room.lat);
LAT = LAT.toString();
let LONG = await DataStore.query(Room.long);
LONG = LONG.toString();
const CUISINE = await DataStore.query(Room.cuisine);
}

let url = "";

if (CUISINE == "None")
url = `https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=${LAT},${LONG}&radius=5000&type=restaurant&key=${PLACES_API_KEY}`;
else
url = `https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=${LAT},${LONG}&radius=5000&type=restaurant&keyword=${CUISINE}&key=${PLACES_API_KEY}`;

axios.get(url).then((response) => {
let length = response.data.results.length;
for (let i = 0; i < length; i++) {
let restaurantId = response.data.results[i].id;
let restaurantName = response.data.results[i].name;
let restaurantRating = response.data.results[i].rating;
let restaurantPhotoRef = response.data.results[i].photos[0].photo_reference;
let restaurantPhoto = `https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference=${restaurantPhotoRef}&key=${PLACES_API_KEY}`;
data = {
id: restaurantId,
name: restaurantName,
rating: restaurantRating,
photo: restaurantPhoto,
};
pushData(data);
}
});

async function pushData(data) {
await DataStore.save(
new Restaurant({
id: data.id,
name: data.name,
rating: data.rating,
photo: data.photo,
})
);
}
Loading