Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // NV_ViewController.m
- //
- #import "NV_ViewController.h"
- #import <GoogleMaps/GoogleMaps.h>
- @interface NV_ViewController ()
- @end
- @implementation NV_ViewController {
- GMSMapView *gmsMapView;
- CLLocationManager *locationManagerMonitor;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- if ([self _checkIfLocationIsEnabled]) {
- CLLocation *location = [locationManagerMonitor location];
- GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:location.coordinate.latitude longitude:location.coordinate.longitude zoom:1];
- gmsMapView = [[GMSMapView alloc] initWithFrame:CGRectZero];
- gmsMapView.delegate = self;
- gmsMapView.myLocationEnabled = YES;
- gmsMapView.settings.myLocationButton = YES;
- self.view = gmsMapView;
- [gmsMapView setCamera:camera];
- [gmsMapView animateToCameraPosition:[GMSCameraPosition cameraWithTarget:gmsMapView.camera.target zoom:16]];
- NSSet *monitoredRegions = [locationManagerMonitor monitoredRegions];
- if (monitoredRegions) {
- [monitoredRegions enumerateObjectsUsingBlock:^(CLRegion *region, BOOL *stop)
- {
- NSString *identifier = region.identifier;
- NSLog(@"%@", identifier);
- [locationManagerMonitor stopMonitoringForRegion:region];
- }];
- }
- [locationManagerMonitor startMonitoringSignificantLocationChanges];
- }
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- #pragma mark -
- #pragma mark Custom Methods
- - (BOOL)_checkIfLocationIsEnabled
- {
- if (![CLLocationManager locationServicesEnabled]) {
- [self _showAlertWithMessage:@"location is not enabled"];
- return NO;
- }
- locationManagerMonitor = [[CLLocationManager alloc] init];
- [locationManagerMonitor setDelegate:self];
- [locationManagerMonitor setDesiredAccuracy:kCLLocationAccuracyBest];
- [locationManagerMonitor setDistanceFilter:kCLDistanceFilterNone];
- return YES;
- }
- - (void)_showAlertWithMessage:(NSString *)message
- {
- UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert" message:message delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
- [alertView show];
- }
- //http://hayageek.com/ios-geofencing-api/
- - (NSNumber*)_calculateDistanceInMetersBetweenCoord:(CLLocationCoordinate2D)coord1 coord:(CLLocationCoordinate2D)coord2
- {
- NSInteger nRadius = 6371; // Earth's radius in Kilometers
- double latDiff = (coord2.latitude - coord1.latitude) * (M_PI/180);
- double lonDiff = (coord2.longitude - coord1.longitude) * (M_PI/180);
- double lat1InRadians = coord1.latitude * (M_PI/180);
- double lat2InRadians = coord2.latitude * (M_PI/180);
- double nA = pow ( sin(latDiff/2), 2 ) + cos(lat1InRadians) * cos(lat2InRadians) * pow ( sin(lonDiff/2), 2 );
- double nC = 2 * atan2( sqrt(nA), sqrt( 1 - nA ));
- double nD = nRadius * nC;
- // convert to meters
- return @(nD*1000);
- }
- #pragma mark -
- #pragma mark GMSMapViewDelegates
- - (void)mapView:(GMSMapView *)mapView didLongPressAtCoordinate:(CLLocationCoordinate2D)coordinate
- {
- NSLog(@"add marker");
- NSString *identifier = [[NSUUID UUID] UUIDString];
- GMSMarker *marker = [[GMSMarker alloc] init];
- [marker setAppearAnimation:kGMSMarkerAnimationPop];
- marker.position = coordinate;
- marker.title = identifier;
- marker.snippet = [[NSString alloc] initWithFormat:@"%f - %f", coordinate.latitude, coordinate.longitude];
- marker.map = gmsMapView;
- [locationManagerMonitor startUpdatingLocation];
- CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:coordinate radius:200 identifier:identifier];
- region.notifyOnEntry = YES;
- region.notifyOnExit = YES;
- [locationManagerMonitor startMonitoringForRegion:region];
- [locationManagerMonitor stopUpdatingLocation];
- GMSCircle *regionCircle = [[GMSCircle alloc] init];
- regionCircle.radius = region.radius;
- regionCircle.position = coordinate;
- regionCircle.fillColor = [UIColor colorWithWhite:0.7 alpha:0.5];
- regionCircle.strokeWidth = 3;
- regionCircle.strokeColor = [UIColor blueColor];
- regionCircle.map = gmsMapView;
- }
- - (void)mapView:(GMSMapView *)mapView didTapInfoWindowOfMarker:(GMSMarker *)marker
- {
- CLLocationCoordinate2D userLocation = [[gmsMapView myLocation] coordinate];
- NSNumber *distance = [self _calculateDistanceInMetersBetweenCoord:marker.position coord:userLocation];
- CLCircularRegion *region = [regions objectAtIndex:0];
- CLLocationCoordinate2D coordinate = [[gmsMapView myLocation] coordinate];
- //print distance
- NSLog(@"%@", distance);
- }
- - (BOOL)didTapMyLocationButtonForMapView:(GMSMapView *)mapView
- {
- CLLocationCoordinate2D userLocation = [[gmsMapView myLocation] coordinate];
- NSLog(@"Actual %f - %f", userLocation.latitude, userLocation.longitude);
- GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:userLocation.latitude longitude:userLocation.longitude zoom:16];
- [gmsMapView setCamera:camera];
- return YES;
- }
- #pragma mark -
- #pragma mark CLLocationManagerDelegates
- - (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
- {
- NSLog(@"enter: %@", region.identifier);
- [self _showAlertWithMessage:@"ENTER !!!"];
- }
- - (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
- {
- NSLog(@"exit: %@", region.identifier);
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement