MapViewController.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. /**
  3. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: GPL-3.0-or-later
  5. */
  6. #import "MapViewController.h"
  7. #import <CoreLocation/CoreLocation.h>
  8. #import "GeoLocationRichObject.h"
  9. #import "NCAppBranding.h"
  10. @interface MapViewController () <CLLocationManagerDelegate, MKMapViewDelegate>
  11. {
  12. CLLocationManager *_locationManager;
  13. MKPointAnnotation *_annotation;
  14. }
  15. @end
  16. @implementation MapViewController
  17. - (void)viewDidLoad
  18. {
  19. [super viewDidLoad];
  20. self.navigationItem.title = NSLocalizedString(@"Shared location", nil);
  21. [self.navigationController.navigationBar setTitleTextAttributes:
  22. @{NSForegroundColorAttributeName:[NCAppBranding themeTextColor]}];
  23. self.navigationController.navigationBar.tintColor = [NCAppBranding themeTextColor];
  24. self.navigationController.navigationBar.barTintColor = [NCAppBranding themeColor];
  25. self.navigationController.navigationBar.translucent = NO;
  26. UIColor *themeColor = [NCAppBranding themeColor];
  27. UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
  28. [appearance configureWithOpaqueBackground];
  29. appearance.backgroundColor = themeColor;
  30. appearance.titleTextAttributes = @{NSForegroundColorAttributeName:[NCAppBranding themeTextColor]};
  31. self.navigationItem.standardAppearance = appearance;
  32. self.navigationItem.compactAppearance = appearance;
  33. self.navigationItem.scrollEdgeAppearance = appearance;
  34. _locationManager = [[CLLocationManager alloc] init];
  35. _locationManager.delegate = self;
  36. [_locationManager requestWhenInUseAuthorization];
  37. _mapView.delegate = self;
  38. [self centerMapViewInSharedLocation];
  39. [_mapView addAnnotation:_annotation];
  40. UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
  41. target:self action:@selector(cancelButtonPressed)];
  42. self.navigationItem.leftBarButtonItem = cancelButton;
  43. }
  44. - (instancetype)initWithGeoLocationRichObject:(GeoLocationRichObject *)geoLocation
  45. {
  46. self = [super init];
  47. if (self) {
  48. _annotation = [[MKPointAnnotation alloc] init];
  49. _annotation.coordinate = CLLocationCoordinate2DMake([geoLocation.latitude doubleValue], [geoLocation.longitude doubleValue]);
  50. _annotation.title = geoLocation.name;
  51. }
  52. return self;
  53. }
  54. #pragma mark - Actions
  55. - (void)cancelButtonPressed
  56. {
  57. [self dismissViewControllerAnimated:YES completion:nil];
  58. }
  59. #pragma mark - Map view
  60. - (void)centerMapViewInSharedLocation
  61. {
  62. MKCoordinateRegion mapRegion;
  63. mapRegion.center = _annotation.coordinate;
  64. mapRegion.span = MKCoordinateSpanMake(0.005, 0.005);
  65. [self.mapView setRegion:mapRegion animated:YES];
  66. }
  67. #pragma mark - MKMapViewDelegate
  68. - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
  69. {
  70. // If the annotation is the user location, just return nil.
  71. if ([annotation isKindOfClass:[MKUserLocation class]]) {
  72. return nil;
  73. }
  74. if (annotation == _annotation) {
  75. MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"SharedLocationAnnotationView"];
  76. if (!pinView) {
  77. pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"SharedLocationAnnotationView"];
  78. pinView.pinTintColor = [NCAppBranding elementColor];
  79. pinView.animatesDrop = YES;
  80. pinView.canShowCallout = YES;
  81. } else {
  82. pinView.annotation = annotation;
  83. }
  84. return pinView;
  85. }
  86. return nil;
  87. }
  88. @end