-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap's assesment.py
71 lines (58 loc) · 2.18 KB
/
map's assesment.py
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
71
import sys
import time
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QVBoxLayout
import requests
import webbrowser
class MapsInterface(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 300, 150)
self.setWindowTitle('Maps Interface')
# Create the widgets
self.label = QLabel('Enter a city name:')
self.input_field = QLineEdit()
self.button = QPushButton('Open in Maps')
self.button.clicked.connect(self.open_maps)
# vertical layout
layout = QVBoxLayout()
#add the widgets to the layout
layout.addWidget(self.label)
layout.addWidget(self.input_field)
layout.addWidget(self.button)
#layout of the main widget
self.setLayout(layout)
def open_maps(self):
# webbrowser.open("https://www.google.com/maps")
time.sleep(0)
city = self.input_field.text()
coordinates = self.get_coordinates(city)
self.open_city_on_maps(city,coordinates)
def get_coordinates(self, city):
api_key = "AIzaSyC2SaoSKUBDXYMB4gjVX15u2mMiRywmTqo"
endpoint = "https://maps.googleapis.com/maps/api/geocode/json"
params = {
"address": city,
"key": api_key
}
response = requests.get(endpoint, params=params)
data = response.json()
if data["status"] == "OK":
lat = data["results"][0]["geometry"]["location"]["lat"]
lng = data["results"][0]["geometry"]["location"]["lng"]
return (lat, lng)
else:
return None
def open_city_on_maps(self, city, coordinates):
if coordinates:
lat, lng = coordinates
maps_url = f"https://www.google.com/maps?q={coordinates}"
webbrowser.open(maps_url)
else:
self.label.setText(f"Sorry, we were unable to find the coordinates of {city}.")
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MapsInterface()
ex.show()
sys.exit(app.exec_())