forked from vishakha-lall/MapBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogleMapsApiModule.py
More file actions
70 lines (67 loc) · 3.03 KB
/
googleMapsApiModule.py
File metadata and controls
70 lines (67 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
def direction(origin,destination):
import googlemaps
import webbrowser
import config
gmaps = googlemaps.Client(config.key)
result = gmaps.directions(origin,destination) #other attributes
#print("Total distance: "+str(result[0]['legs'][0]['distance']['text']))
#print("Calculated duration: "+str(result[0]['legs'][0]['duration']['text']))
#print("Steps: ")
#for i in range(len(result[0]['legs'][0]['steps'])):
# print("Step "+str(i+1)+" "+str(result[0]['legs'][0]['steps'][i]['html_instructions']))
# print("Continue this step for "+str(result[0]['legs'][0]['steps'][i]['distance']['text'])+" and an approximate of "+str(result[0]['legs'][0]['steps'][i]['duration']['text']))
# print("")
address = "origin="+origin+"&"+"destination="+destination
address = address.lower()
address = address.replace(" ","+")
url = "https://www.google.com/maps/dir/?api=1&"
result_url = url+address
print(result_url)
webbrowser.open_new(result_url)
def add_to_maps_database(origin,destination):
import config
import mysql.connector
db = mysql.connector.connect(user=config.user,password=config.password,host=config.host,database=config.database)
cur = db.cursor()
cur = db.cursor(buffered=True)
if destination == "":
cur.execute("INSERT INTO directions_table(origin_location) VALUES (%s)",(origin,))
db.commit()
elif origin == "":
cur.execute("SELECT id FROM directions_table ORDER BY id DESC")
res = cur.fetchone()
last_id = res[0]
cur.execute('UPDATE directions_table SET destination_location=%s WHERE id=%s',(destination,last_id))
db.commit()
else:
cur.execute("INSERT INTO directions_table(origin_location,destination_location) VALUES (%s,%s)",(origin,destination))
db.commit()
def get_from_maps_database():
import config
import mysql.connector
db = mysql.connector.connect(user=config.user,password=config.password,host=config.host,database=config.database)
cur = db.cursor()
cur = db.cursor(buffered=True)
cur.execute("SELECT id FROM directions_table ORDER BY id DESC")
res = cur.fetchone()
last_id = res[0]
cur.execute('SELECT origin_location FROM directions_table WHERE id=%s', (last_id,))
res = cur.fetchone()
origin = res[0]
cur.execute('SELECT destination_location FROM directions_table WHERE id=%s', (last_id,))
res = cur.fetchone()
destination = res[0]
return origin,destination
def geocoding(search_location):
import googlemaps
import webbrowser
import config
gmaps = googlemaps.Client(config.key)
result = gmaps.geocode(search_location)
print("Formatted Address: "+result[0]['formatted_address'])
print("Latitude: "+str(result[0]['geometry']['location']['lat'])+" "+"Longitude: "+str(result[0]['geometry']['location']['lng']))
address = search_location.lower()
address = address.replace(" ","+")
url = "https://www.google.com/maps/search/?api=1&query="
result_url = url+address
webbrowser.open_new(result_url)