Skip to content

Commit

Permalink
added geoloc
Browse files Browse the repository at this point in the history
  • Loading branch information
jermxt committed Oct 16, 2022
1 parent db26f11 commit 78975ee
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions backend/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
from sqlalchemy import exc
from database import Session
from model import User
import json
import random
import json, os, random, requests

users_bp = Blueprint('users', __name__)

try:
gmap_api_key = os.environ['GMAPS_API_KEY']
except:
gmap_api_key = None


# Adds a new user in the db, only populating the
# username and password fields. If there is already
# a user with the same username it returns { "success" : False}
Expand Down Expand Up @@ -98,8 +103,23 @@ def addFriends():
return jsonify(ret)

def geocode(address):
if gmap_api_key != None:
res = requests.get("https://maps.googleapis.com/maps/api/geocode/json" + \
"?address=" + address + "&key=" + gmap_api_key)
results = res.json()['results']
if results != []:
lat = results[0]['geometry']['location']['lat']
lng = results[0]['geometry']['location']['lng']
return lat, lng
return 37.86253507889059 + random.uniform(-1, 1), -122.26139173001866 + random.uniform(-1, 1)

@users_bp.route('/testGeo/', methods=['GET'])
def bop():
print(gmap_api_key)
geocode('2318 Parker Street, Berkeley, CA, 94704, US')
return jsonify({})


# get params: ?username=USERNAME
@users_bp.route('/getFriends/', methods=['GET'])
def getFriends():
Expand Down

0 comments on commit 78975ee

Please sign in to comment.