Guest User

Untitled

a guest
Jan 22nd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.37 KB | None | 0 0
  1. func didLongPressMap(sender: UILongPressGestureRecognizer) {
  2.  
  3. if sender.state == UIGestureRecognizerState.Began {
  4. let touchPoint = sender.locationInView(self.mapView)
  5. let touchCoordinate = self.mapView.convertPoint(touchPoint, toCoordinateFromView: self.mapView)
  6. var annotation = MKPointAnnotation()
  7. annotation.coordinate = touchCoordinate
  8. annotation.title = "Your position"
  9. self.mapView.addAnnotation(annotation) //drops the pin
  10. println("lat: (touchCoordinate.latitude)")
  11. var num = (touchCoordinate.latitude as NSNumber).floatValue
  12. var formatter = NSNumberFormatter()
  13. formatter.maximumFractionDigits = 4
  14. formatter.minimumFractionDigits = 4
  15. var str = formatter.stringFromNumber(num)
  16. println("long: (touchCoordinate.longitude)")
  17. var num1 = (touchCoordinate.longitude as NSNumber).floatValue
  18. var formatter1 = NSNumberFormatter()
  19. formatter1.maximumFractionDigits = 4
  20. formatter1.minimumFractionDigits = 4
  21. var str1 = formatter1.stringFromNumber(num1)
  22. self.adressLoLa.text = "(num),(num1)"
  23. }
  24. }
  25.  
  26. func didLongPressMap(sender: UILongPressGestureRecognizer) {
  27.  
  28. if sender.state == UIGestureRecognizerState.began {
  29. let touchPoint = sender.location(in: mapView)
  30. let touchCoordinate = mapView.convert(touchPoint, toCoordinateFrom: self.mapView)
  31. let annotation = MKPointAnnotation()
  32. annotation.coordinate = touchCoordinate
  33. annotation.title = "Your position"
  34. mapView.addAnnotation(annotation) //drops the pin
  35. print("lat: (touchCoordinate.latitude)")
  36. let num = touchCoordinate.latitude as NSNumber
  37. let formatter = NumberFormatter()
  38. formatter.maximumFractionDigits = 4
  39. formatter.minimumFractionDigits = 4
  40. let str = formatter.string(from: num)
  41. print("long: (touchCoordinate.longitude)")
  42. let num1 = touchCoordinate.longitude as NSNumber
  43. let formatter1 = NumberFormatter()
  44. formatter1.maximumFractionDigits = 4
  45. formatter1.minimumFractionDigits = 4
  46. let str1 = formatter1.string(from: num1)
  47. adressLoLa.text = "(num),(num1)"
  48.  
  49. // Add below code to get address for touch coordinates.
  50. let geoCoder = CLGeocoder()
  51. let location = CLLocation(latitude: touchCoordinate.latitude, longitude: touchCoordinate.longitude)
  52. geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
  53.  
  54. // Place details
  55. var placeMark: CLPlacemark!
  56. placeMark = placemarks?[0]
  57.  
  58. // Address dictionary
  59. print(placeMark.addressDictionary as Any)
  60.  
  61. // Location name
  62. if let locationName = placeMark.addressDictionary!["Name"] as? NSString {
  63. print(locationName)
  64. }
  65. // Street address
  66. if let street = placeMark.addressDictionary!["Thoroughfare"] as? NSString {
  67. print(street)
  68. }
  69. // City
  70. if let city = placeMark.addressDictionary!["City"] as? NSString {
  71. print(city)
  72. }
  73. // Zip code
  74. if let zip = placeMark.addressDictionary!["ZIP"] as? NSString {
  75. print(zip)
  76. }
  77. // Country
  78. if let country = placeMark.addressDictionary!["Country"] as? NSString {
  79. print(country)
  80. }
  81. })
  82. }
  83. }
  84.  
  85. import Foundation
  86. import MapKit
  87.  
  88. typealias JSONDictionary = [String:Any]
  89.  
  90. class LocationServices {
  91.  
  92. let shared = LocationServices()
  93. let locManager = CLLocationManager()
  94. var currentLocation: CLLocation!
  95.  
  96. let authStatus = CLLocationManager.authorizationStatus()
  97. let inUse = CLAuthorizationStatus.authorizedWhenInUse
  98. let always = CLAuthorizationStatus.authorizedAlways
  99.  
  100. func getAdress(completion: @escaping (_ address: JSONDictionary?, _ error: Error?) -> ()) {
  101.  
  102. self.locManager.requestWhenInUseAuthorization()
  103.  
  104. if self.authStatus == inUse || self.authStatus == always {
  105.  
  106. self.currentLocation = locManager.location
  107.  
  108. let geoCoder = CLGeocoder()
  109.  
  110. geoCoder.reverseGeocodeLocation(self.currentLocation) { placemarks, error in
  111.  
  112. if let e = error {
  113.  
  114. completion(nil, e)
  115.  
  116. } else {
  117.  
  118. let placeArray = placemarks as? [CLPlacemark]
  119.  
  120. var placeMark: CLPlacemark!
  121.  
  122. placeMark = placeArray?[0]
  123.  
  124. guard let address = placeMark.addressDictionary as? JSONDictionary else {
  125. return
  126. }
  127.  
  128. completion(address, nil)
  129.  
  130. }
  131.  
  132. }
  133.  
  134. }
  135.  
  136. }
  137.  
  138. }
  139.  
  140. import UIKit
  141.  
  142. class ViewController: UIViewController {
  143.  
  144. override func viewDidLoad() {
  145. super.viewDidLoad()
  146. LocationServices.shared.getAdress { address, error in
  147.  
  148. if let a = address, let city = a["City"] as? String {
  149. //
  150. }
  151.  
  152. }
  153.  
  154. }
  155.  
  156. }
  157.  
  158. func setUsersClosestCity()
  159. {
  160. let geoCoder = CLGeocoder()
  161. let location = CLLocation(latitude: _point1.coordinate.latitude, longitude: _point1.coordinate.longitude)
  162. geoCoder.reverseGeocodeLocation(location)
  163. {
  164. (placemarks, error) -> Void in
  165.  
  166. let placeArray = placemarks as [CLPlacemark]!
  167.  
  168. // Place details
  169. var placeMark: CLPlacemark!
  170. placeMark = placeArray?[0]
  171.  
  172. // Address dictionary
  173. print(placeMark.addressDictionary)
  174.  
  175. // Location name
  176. if let locationName = placeMark.addressDictionary?["Name"] as? NSString
  177. {
  178. print(locationName)
  179. }
  180.  
  181. // Street address
  182. if let street = placeMark.addressDictionary?["Thoroughfare"] as? NSString
  183. {
  184. print(street)
  185. }
  186.  
  187. // City
  188. if let city = placeMark.addressDictionary?["City"] as? NSString
  189. {
  190. print(city)
  191. }
  192.  
  193. // Zip code
  194. if let zip = placeMark.addressDictionary?["ZIP"] as? NSString
  195. {
  196. print(zip)
  197. }
  198.  
  199. // Country
  200. if let country = placeMark.addressDictionary?["Country"] as? NSString
  201. {
  202. print(country)
  203. }
  204. }
  205. }
  206.  
  207. let geoCoder = CLGeocoder()
  208. let location = CLLocation(latitude: touchCoordinate.latitude, longitude: touchCoordinate.longitude)
  209.  
  210. geoCoder.reverseGeocodeLocation(location, completionHandler: { placemarks, error in
  211. guard let addressDict = placemarks?[0].addressDictionary else {
  212. return
  213. }
  214.  
  215. // Print each key-value pair in a new row
  216. addressDict.forEach { print($0) }
  217.  
  218. // Print fully formatted address
  219. if let formattedAddress = addressDict["FormattedAddressLines"] as? [String] {
  220. print(formattedAddress.joined(separator: ", "))
  221. }
  222.  
  223. // Access each element manually
  224. if let locationName = addressDict["Name"] as? String {
  225. print(locationName)
  226. }
  227. if let street = addressDict["Thoroughfare"] as? String {
  228. print(street)
  229. }
  230. if let city = addressDict["City"] as? String {
  231. print(city)
  232. }
  233. if let zip = addressDict["ZIP"] as? String {
  234. print(zip)
  235. }
  236. if let country = addressDict["Country"] as? String {
  237. print(country)
  238. }
  239. })
  240.  
  241. var geocoder = CLGeocoder()
  242. var location = CLLocation(latitude: IC.coordinate!.latitude, longitude: IC.coordinate!.longitude)
  243. geocoder.reverseGeocodeLocation(location) {
  244. (placemarks, error) -> Void in
  245. if let placemarks = placemarks as? [CLPlacemark] where placemarks.count > 0 {
  246. var placemark = placemarks[0]
  247. println(placemark.addressDictionary)
  248. }
  249.  
  250. - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:
  251. (CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
  252. CLLocation *location = [locationManager location];
  253.  
  254.  
  255. CLLocationCoordinate2D coordinate = [location coordinate];
  256.  
  257.  
  258. latitude = [NSString stringWithFormat:@"%.12f", coordinate.latitude];
  259. longitude = [NSString stringWithFormat:@"%.12f", coordinate.longitude];
  260.  
  261. CLLocation *location1 = [[CLLocation alloc]
  262. initWithLatitude:latitude.floatValue
  263. longitude:longitude.floatValue];
  264.  
  265. self.myGeocoder = [[CLGeocoder alloc] init];
  266.  
  267. [self.myGeocoder
  268. reverseGeocodeLocation:location1
  269. completionHandler:^(NSArray *placemarks, NSError *error) {
  270. if (error == nil &&
  271. [placemarks count] > 0){
  272. placemark = [placemarks lastObject];
  273. NSString* vendorLocation=[NSString stringWithFormat:@"%@ %@",
  274. placemark.locality,
  275. placemark.subLocality];
  276. NSLog(@"%@",vendorLocation);
  277. }
  278. }];
  279. }
  280.  
  281. let geoCoder = CLGeocoder()
  282. let location = CLLocation(latitude: 37.769193, longitude: -122.426512)
  283. geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
  284.  
  285. // Place details
  286. var placeMark: CLPlacemark!
  287. placeMark = placemarks?[0]
  288.  
  289. // Complete address as PostalAddress
  290. print(placeMark.postalAddress as Any) // Import Contacts
  291.  
  292. // Location name
  293. if let locationName = placeMark.name {
  294. print(locationName)
  295. }
  296.  
  297. // Street address
  298. if let street = placeMark.thoroughfare {
  299. print(street)
  300. }
  301.  
  302. // Country
  303. if let country = placeMark.country {
  304. print(country)
  305. }
  306. })
Add Comment
Please, Sign In to add comment