-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddressSearchMapViewController.swift
126 lines (88 loc) · 4.23 KB
/
AddressSearchMapViewController.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
//
// AddressSearchMapViewController.swift
// OnTheMap
//
// Created by Basma Ahmed Mohamed Mahmoud on 3/25/18.
// Copyright © 2018 Basma Ahmed Mohamed Mahmoud. All rights reserved.
//
import UIKit
import MapKit
import Foundation
import CoreLocation
class AddressSearchMapViewController: UIViewController,MKMapViewDelegate,CLLocationManagerDelegate {
@IBOutlet weak var newMapView: MKMapView!
var lati: CLLocationDegrees?
var long: CLLocationDegrees?
var newAddress: String?
var newWebsite: String?
override func viewDidLoad() {
super.viewDidLoad()
newMapView.delegate = self
Activity.shared.startActivityIndicator(view: view)
}
override func viewWillAppear(_ animated: Bool) {
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(newAddress!) {
(placemarks, error) in
guard error == nil else {
self.alertTheUserWithAction(title: "Couldnt find your location", message: "Please try another location")
return
}
let placemark = placemarks?.first
self.lati = placemark?.location?.coordinate.latitude
self.long = placemark?.location?.coordinate.longitude
let latt = CLLocationDegrees(self.lati!)
let longg = CLLocationDegrees(self.long!)
// zooming the map to my location
let Location = CLLocationCoordinate2D(latitude: self.lati!, longitude: self.long!);
let region = MKCoordinateRegion(center: Location, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01));
self.newMapView.setRegion(region, animated: true);
let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: latt, longitude: longg)
annotation.title = self.newAddress
annotation.subtitle = self.newWebsite
self.newMapView.addAnnotation(annotation)
Activity.shared.stopActivityIndicator()
}
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let reuseId = "pin"
var pin = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
if pin == nil {
pin = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pin!.canShowCallout = true
pin!.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
} else {
pin!.annotation = annotation
}
return pin
}
@IBAction func placeNewAddress(_ sender: Any) {
OnTheMapFunc.shared.updateCurrentUser(latitude: self.lati as! Double, Longitude: self.long as! Double,mediaURL: newWebsite!){ (error: String?) in
DispatchQueue.main.async {
guard error == nil else {
self.alertTheUser(title: "Connection Error", message: "Please check your connection")
return
}
self.dismiss(animated: true, completion: nil)
}
}
}
private func alertTheUser(title: String, message: String){
DispatchQueue.main.async {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
let ok = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(ok)
self.present(alert, animated: true, completion: nil)
}
}
func alertTheUserWithAction(title: String, message: String){
DispatchQueue.main.async {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "ok", style: UIAlertActionStyle.default, handler:{(action)in
self.dismiss(animated: true, completion: nil)
} ))
self.present(alert, animated: true, completion: nil)
}
}
}