Advertisement
Guest User

Untitled

a guest
Aug 4th, 2014
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  NV_ViewController.m
  3. //
  4.  
  5. #import "NV_ViewController.h"
  6. #import <GoogleMaps/GoogleMaps.h>
  7.  
  8. @interface NV_ViewController ()
  9.  
  10. @end
  11.  
  12. @implementation NV_ViewController {
  13.     GMSMapView *gmsMapView;
  14.     CLLocationManager *locationManagerMonitor;
  15. }
  16.  
  17. - (void)viewDidLoad
  18. {
  19.     [super viewDidLoad];
  20.     // Do any additional setup after loading the view, typically from a nib.
  21.    
  22.    
  23.     if ([self _checkIfLocationIsEnabled]) {
  24.    
  25.         CLLocation *location = [locationManagerMonitor location];
  26.    
  27.         GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:location.coordinate.latitude longitude:location.coordinate.longitude zoom:1];
  28.    
  29.         gmsMapView = [[GMSMapView alloc] initWithFrame:CGRectZero];
  30.         gmsMapView.delegate = self;
  31.         gmsMapView.myLocationEnabled = YES;
  32.         gmsMapView.settings.myLocationButton = YES;
  33.         self.view = gmsMapView;
  34.    
  35.         [gmsMapView setCamera:camera];
  36.         [gmsMapView animateToCameraPosition:[GMSCameraPosition cameraWithTarget:gmsMapView.camera.target zoom:16]];
  37.  
  38.         NSSet *monitoredRegions = [locationManagerMonitor monitoredRegions];
  39.         if (monitoredRegions) {
  40.             [monitoredRegions enumerateObjectsUsingBlock:^(CLRegion *region, BOOL *stop)
  41.              {
  42.                  NSString *identifier = region.identifier;
  43.                  NSLog(@"%@", identifier);
  44.                  
  45.                  [locationManagerMonitor stopMonitoringForRegion:region];
  46.              }];
  47.         }
  48.        
  49.         [locationManagerMonitor startMonitoringSignificantLocationChanges];
  50.     }
  51. }
  52.  
  53. - (void)didReceiveMemoryWarning
  54. {
  55.     [super didReceiveMemoryWarning];
  56.     // Dispose of any resources that can be recreated.
  57. }
  58.  
  59.  
  60.  
  61. #pragma mark -
  62. #pragma mark Custom Methods
  63. - (BOOL)_checkIfLocationIsEnabled
  64. {
  65.     if (![CLLocationManager locationServicesEnabled]) {
  66.         [self _showAlertWithMessage:@"location is not enabled"];
  67.         return NO;
  68.     }
  69.    
  70.     locationManagerMonitor = [[CLLocationManager alloc] init];
  71.     [locationManagerMonitor setDelegate:self];
  72.     [locationManagerMonitor setDesiredAccuracy:kCLLocationAccuracyBest];
  73.     [locationManagerMonitor setDistanceFilter:kCLDistanceFilterNone];
  74.    
  75.     return YES;
  76. }
  77.  
  78. - (void)_showAlertWithMessage:(NSString *)message
  79. {
  80.     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert" message:message delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
  81.     [alertView show];
  82. }
  83.  
  84.  
  85. //http://hayageek.com/ios-geofencing-api/
  86. - (NSNumber*)_calculateDistanceInMetersBetweenCoord:(CLLocationCoordinate2D)coord1 coord:(CLLocationCoordinate2D)coord2
  87. {
  88.     NSInteger nRadius = 6371; // Earth's radius in Kilometers
  89.     double latDiff = (coord2.latitude - coord1.latitude) * (M_PI/180);
  90.     double lonDiff = (coord2.longitude - coord1.longitude) * (M_PI/180);
  91.     double lat1InRadians = coord1.latitude * (M_PI/180);
  92.     double lat2InRadians = coord2.latitude * (M_PI/180);
  93.     double nA = pow ( sin(latDiff/2), 2 ) + cos(lat1InRadians) * cos(lat2InRadians) * pow ( sin(lonDiff/2), 2 );
  94.     double nC = 2 * atan2( sqrt(nA), sqrt( 1 - nA ));
  95.     double nD = nRadius * nC;
  96.     // convert to meters
  97.     return @(nD*1000);
  98. }
  99.  
  100.  
  101.  
  102. #pragma mark -
  103. #pragma mark GMSMapViewDelegates
  104. - (void)mapView:(GMSMapView *)mapView didLongPressAtCoordinate:(CLLocationCoordinate2D)coordinate
  105. {
  106.     NSLog(@"add marker");
  107.  
  108.     NSString *identifier = [[NSUUID UUID] UUIDString];
  109.    
  110.     GMSMarker *marker = [[GMSMarker alloc] init];
  111.     [marker setAppearAnimation:kGMSMarkerAnimationPop];
  112.     marker.position = coordinate;
  113.     marker.title = identifier;
  114.     marker.snippet = [[NSString alloc] initWithFormat:@"%f - %f", coordinate.latitude, coordinate.longitude];
  115.     marker.map = gmsMapView;
  116.    
  117.    
  118.     [locationManagerMonitor startUpdatingLocation];
  119.    
  120.     CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:coordinate radius:200 identifier:identifier];
  121.     region.notifyOnEntry = YES;
  122.     region.notifyOnExit = YES;
  123.     [locationManagerMonitor startMonitoringForRegion:region];
  124.    
  125.     [locationManagerMonitor stopUpdatingLocation];
  126.    
  127.     GMSCircle *regionCircle = [[GMSCircle alloc] init];
  128.     regionCircle.radius = region.radius;
  129.     regionCircle.position = coordinate;
  130.     regionCircle.fillColor = [UIColor colorWithWhite:0.7 alpha:0.5];
  131.     regionCircle.strokeWidth = 3;
  132.     regionCircle.strokeColor = [UIColor blueColor];
  133.     regionCircle.map = gmsMapView;
  134. }
  135.  
  136. - (void)mapView:(GMSMapView *)mapView didTapInfoWindowOfMarker:(GMSMarker *)marker
  137. {
  138.     CLLocationCoordinate2D userLocation = [[gmsMapView myLocation] coordinate];
  139.    
  140.     NSNumber *distance = [self _calculateDistanceInMetersBetweenCoord:marker.position coord:userLocation];
  141.    
  142.     CLCircularRegion *region = [regions objectAtIndex:0];
  143.     CLLocationCoordinate2D coordinate = [[gmsMapView myLocation] coordinate];
  144.    
  145.     //print distance
  146.     NSLog(@"%@", distance);
  147. }
  148.  
  149. - (BOOL)didTapMyLocationButtonForMapView:(GMSMapView *)mapView
  150. {
  151.     CLLocationCoordinate2D userLocation = [[gmsMapView myLocation] coordinate];
  152.     NSLog(@"Actual %f - %f", userLocation.latitude, userLocation.longitude);
  153.    
  154.     GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:userLocation.latitude longitude:userLocation.longitude zoom:16];
  155.     [gmsMapView setCamera:camera];
  156.    
  157.    
  158.     return YES;
  159. }
  160.  
  161.  
  162.  
  163. #pragma mark -
  164. #pragma mark CLLocationManagerDelegates
  165. - (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
  166. {
  167.     NSLog(@"enter: %@", region.identifier);
  168.     [self _showAlertWithMessage:@"ENTER !!!"];
  169. }
  170.  
  171. - (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
  172. {
  173.     NSLog(@"exit: %@", region.identifier);
  174. }
  175.  
  176.  
  177.  
  178. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement