-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewController.swift
284 lines (261 loc) · 9.68 KB
/
ViewController.swift
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
//
// ViewController.swift
// WeatherOnMap
//
// Created by Mustafa on 4/13/24.
//
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate {
private let map: MKMapView = {
let map = MKMapView()
return map
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(map)
map.delegate = self
//title = "Home"
LocationManager.shared.getUserLocation { [weak self] location in
DispatchQueue.main.async {
guard let strongSelf = self else {
return
}
strongSelf.addMapPin(with: location)
strongSelf.map.setRegion(MKCoordinateRegion(center: location.coordinate,
span: MKCoordinateSpan(
latitudeDelta: 0.7,
longitudeDelta: 0.7)),
animated: true)
}
}
for (_, (latitude, longitude)) in usCapitalsCoordinates {
addMapPin(with: CLLocation(latitude: latitude, longitude: longitude))
}
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
map.frame = view.bounds
}
func addMapPin(with location: CLLocation) {
let pin = MKPointAnnotation()
pin.coordinate = location.coordinate
LocationManager.shared.resolveLocationName(with: location) { locationName in
pin.title = locationName
}
map.addAnnotation(pin)
}
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
guard let annotation = view.annotation else { return }
requestWeatherForLocation(with: CLLocation(latitude: annotation.coordinate.latitude, longitude: annotation.coordinate.longitude)) { weatherSummary in
guard let degrees = Double(weatherSummary) else {
print("Invalid input. Unable to convert to double.")
return
}
var message = ""
if(degrees >= 80.0) {
message = weatherSummary + " Degrees Fahrenheit ☀️"
} else if(degrees < 80.0 && degrees > 60.0) {
message = weatherSummary + " Degrees Fahrenheit 💨"
} else {
message = weatherSummary + " Degrees Fahrenheit ❄️"
}
let alertController = UIAlertController(title: "The weather is...", message: message, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
DispatchQueue.main.async {
self.present(alertController, animated: true, completion: nil)
}
}
}
func requestWeatherForLocation(with location: CLLocation, completion: @escaping (String) -> Void) {
let long = location.coordinate.longitude
let lat = location.coordinate.latitude
let url = "https://api.pirateweather.net/forecast/5f4y5Dbf4HuNpYznV7zaUpEBDC4HFCf4/\(lat),\(long)"
URLSession.shared.dataTask(with: URL(string: url)!, completionHandler: {data, response, error in
// validation
guard let data = data, error == nil else {
print("oops")
return
}
// convert data to models/some object
var json: WeatherResponse?
do {
json = try JSONDecoder().decode(WeatherResponse.self, from: data)
}
catch {
print("error: \(error)")
}
guard let result = json else {
return
}
completion(String(result.currently.temperature))
}).resume()
}
let usCapitalsCoordinates: [(String, (Double, Double))] = [
("Montgomery", (32.361538, -86.279118)),
("Juneau", (58.301935, -134.419740)),
("Phoenix", (33.448457, -112.073844)),
("Little Rock", (34.736009, -92.331122)),
("Sacramento", (38.555605, -121.468926)),
("Denver", (39.7391667, -104.984167)),
("Hartford", (41.767, -72.677)),
("Dover", (39.161921, -75.526755)),
("Tallahassee", (30.4518, -84.27277)),
("Atlanta", (33.76, -84.39)),
("Honolulu", (21.30895, -157.826182)),
("Boise", (43.613739, -116.237651)),
("Springfield", (39.78325, -89.650373)),
("Indianapolis", (39.790942, -86.147685)),
("Des Moines", (41.590939, -93.620866)),
("Topeka", (39.04, -95.69)),
("Frankfort", (38.197274, -84.86311)),
("Baton Rouge", (30.45809, -91.140229)),
("Augusta", (44.323535, -69.765261)),
("Annapolis", (38.972945, -76.501157)),
("Boston", (42.2352, -71.0275)),
("Lansing", (42.7335, -84.5467)),
("Saint Paul", (44.95, -93.094)),
("Jackson", (32.32, -90.207)),
("Jefferson City", (38.572954, -92.189283)),
("Helena", (46.595805, -112.027031)),
("Lincoln", (40.809868, -96.675345)),
("Carson City", (39.160949, -119.753877)),
("Concord", (43.220093, -71.549127)),
("Trenton", (40.221741, -74.756138)),
("Santa Fe", (35.667231, -105.964575)),
("Albany", (42.659829, -73.781339)),
("Raleigh", (35.771, -78.638)),
("Bismarck", (48.813343, -100.779004)),
("Columbus", (39.962245, -83.000647)),
("Oklahoma City", (35.482309, -97.534994)),
("Salem", (44.931109, -123.029159)),
("Harrisburg", (40.269789, -76.875613)),
("Providence", (41.82355, -71.422132)),
("Columbia", (34.0, -81.035)),
("Pierre", (44.367966, -100.336378)),
("Nashville", (36.165, -86.784)),
("Austin", (30.266667, -97.75)),
("Salt Lake City", (40.7547, -111.892622)),
("Montpelier", (44.26639, -72.57194)),
("Richmond", (37.54, -77.46)),
("Olympia", (47.042418, -122.893077)),
("Charleston", (38.349497, -81.633294)),
("Madison", (43.074722, -89.384444)),
("Cheyenne", (41.145548, -104.802042))
]
struct WeatherResponse: Codable {
let latitude: Float
let longitude: Float
let timezone: String
let offset: Int
let elevation: Int
let currently: CurrentWeather
let minutely: MinutelyWeather
let hourly: HourlyWeather
let daily: DailyWeather
}
struct CurrentWeather: Codable {
let time: Int
let summary: String
let icon: String
let nearestStormDistance: Int
let nearestStormBearing: Int
let precipIntensity: Int
let precipProbability: Int
let temperature: Double
let apparentTemperature: Double
let dewPoint: Double
let humidity: Double
let pressure: Double
let windSpeed: Double
let windGust: Double
let windBearing: Int
let cloudCover: Double
let uvIndex: Double
let visibility: Double
let ozone: Double
}
struct MinutelyWeather: Codable {
let summary: String
let icon: String
let data: [MinutelyWeatherEntry]
}
struct MinutelyWeatherEntry: Codable {
let time: Int
let precipIntensity: Int
let precipProbability: Int
let precipIntensityError: Int
let precipType: String
}
struct HourlyWeather: Codable {
let summary: String
let icon: String
let data: [HourlyWeatherEntry]
}
struct HourlyWeatherEntry: Codable {
let time: Int
let summary: String
let icon: String
let precipIntensity: Float
let precipProbability: Double
let precipType: String?
let temperature: Double
let apparentTemperature: Double
let dewPoint: Double
let humidity: Double
let pressure: Double
let windSpeed: Double
let windGust: Double
let windBearing: Int
let cloudCover: Double
let uvIndex: Double
let visibility: Double
let ozone: Double
}
struct DailyWeather: Codable {
let summary: String
let icon: String
let data: [DailyWeatherEntry]
}
struct DailyWeatherEntry: Codable {
let time: Int
let summary: String
let icon: String
let sunriseTime: Int
let sunsetTime: Int
let moonPhase: Double
let precipIntensity: Float
let precipIntensityMax: Float
let precipIntensityMaxTime: Int
let precipProbability: Double
let precipType: String?
let temperatureHigh: Double
let temperatureHighTime: Int
let temperatureLow: Double
let temperatureLowTime: Int
let apparentTemperatureHigh: Double
let apparentTemperatureHighTime: Int
let apparentTemperatureLow: Double
let apparentTemperatureLowTime: Int
let dewPoint: Double
let humidity: Double
let pressure: Double
let windSpeed: Double
let windGust: Double
let windGustTime: Int
let windBearing: Int
let cloudCover: Double
let uvIndex: Double
let uvIndexTime: Int
let visibility: Double
let temperatureMin: Double
let temperatureMinTime: Int
let temperatureMax: Double
let temperatureMaxTime: Int
let apparentTemperatureMin: Double
let apparentTemperatureMinTime: Int
let apparentTemperatureMax: Double
let apparentTemperatureMaxTime: Int
}
}