NCViewerImageDetailView.swift 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // NCViewerImageDetailView.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 31/10/2020.
  6. // Copyright © 2020 Marino Faggiana. All rights reserved.
  7. //
  8. import Foundation
  9. import MapKit
  10. class NCViewerImageDetailView: UIView {
  11. @IBOutlet weak var mapView: MKMapView!
  12. @IBOutlet weak var locationButton: UIButton!
  13. var annotation = MKPointAnnotation()
  14. var latitude: Double = 0
  15. var longitude: Double = 0
  16. var location: String = ""
  17. override func awakeFromNib() {
  18. super.awakeFromNib()
  19. mapView.layer.cornerRadius = 6
  20. mapView.isZoomEnabled = false
  21. mapView.isScrollEnabled = false
  22. mapView.isUserInteractionEnabled = false
  23. }
  24. @IBAction func touchLocation(_ sender: Any) {
  25. if self.latitude > 0 && self.longitude > 0 {
  26. openMapForPlace()
  27. }
  28. }
  29. func openMapForPlace() {
  30. let latitude: CLLocationDegrees = self.latitude
  31. let longitude: CLLocationDegrees = self.longitude
  32. let regionDistance:CLLocationDistance = 10000
  33. let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
  34. let regionSpan = MKCoordinateRegion(center: coordinates, latitudinalMeters: regionDistance, longitudinalMeters: regionDistance)
  35. let options = [
  36. MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center),
  37. MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span)
  38. ]
  39. let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
  40. let mapItem = MKMapItem(placemark: placemark)
  41. mapItem.name = location
  42. mapItem.openInMaps(launchOptions: options)
  43. }
  44. func updateExifLocal(metadata: tableMetadata) {
  45. DispatchQueue.global().async {
  46. if metadata.typeFile == k_metadataTypeFile_image {
  47. CCExifGeo.sharedInstance()?.setExif(metadata)
  48. }
  49. if let localFile = NCManageDatabase.sharedInstance.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId)) {
  50. let latitudeString = localFile.exifLatitude
  51. let longitudeString = localFile.exifLongitude
  52. self.latitude = Double(localFile.exifLatitude) ?? 0
  53. self.longitude = Double(localFile.exifLongitude) ?? 0
  54. if let location = NCManageDatabase.sharedInstance.getLocationFromGeoLatitude(latitudeString, longitude: longitudeString) {
  55. self.location = location
  56. }
  57. DispatchQueue.main.async {
  58. if self.latitude > 0 && self.longitude > 0 {
  59. self.annotation.coordinate = CLLocationCoordinate2D(latitude: self.latitude, longitude: self.longitude)
  60. self.mapView.addAnnotation(self.annotation)
  61. self.mapView.setRegion(MKCoordinateRegion(center: self.annotation.coordinate, latitudinalMeters: 500, longitudinalMeters: 500), animated: false)
  62. self.locationButton.setTitle(self.location, for: .normal)
  63. }
  64. }
  65. }
  66. }
  67. }
  68. func hasData() -> Bool {
  69. if self.latitude > 0 && self.longitude > 0 {
  70. return true
  71. } else {
  72. return false
  73. }
  74. }
  75. }