0

I want to track the location of my ios device and notify in the app if it crosses certain kilometres.Suppose I want to notify if it crosses 1 km from current location of device. Please help I am new in iOS programming.

I am able to fetch the current location. Please help.

Here what I am able to do.

    - (void)viewDidLoad 
{
        [super viewDidLoad];
        self.mapView.delegate=self;
        // Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7.
        self.locationManager = [[CLLocationManager alloc] init];
        self.locationManager.delegate = self;
        self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;
        self.locationManager.distanceFilter = kCLLocationAccuracyBestForNavigation;//constant update of device location
        [self.locationManager requestWhenInUseAuthorization];
        [self.locationManager requestAlwaysAuthorization];

        [self.locationManager startUpdatingLocation];
    }
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    self.userLocation = userLocation;
    MKCoordinateSpan coordinateSpan; coordinateSpan.latitudeDelta = 0.3f; coordinateSpan.longitudeDelta = 0.3f;
    MKCoordinateRegion regionToShow; regionToShow.center = userLocation.coordinate; regionToShow.span = coordinateSpan;
    [self.mapView setRegion:regionToShow animated:YES];

    MKPointAnnotation *point=[[MKPointAnnotation alloc]init];
    point.coordinate=userLocation.coordinate;
    point.title=@"where Am I";
    point.subtitle=@"YOU are Here";
    [self.mapView addAnnotation:point];
}

1 Answers1

0

self.currentLocation must be an object of CLLocation.

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    CLLocationDistance dist = [self.currentLocation distanceFromLocation:userLocation.location];

    if (dist == 1000.0) {

        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Title" message:[NSString stringWithFormat:@"Message : you moved %.2f", dist] preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            // your code on OK click
        }]];
        [self presentViewController:alert animated:TRUE completion:^{ }];
    }

    self.currentLocation = userLocation.location;
    self.userLocation = userLocation;
}
VRAwesome
  • 4,721
  • 5
  • 27
  • 52
  • [self.userLocation distanceFromLocation:userLocation.location]; Hie This userLocation.location is giving error. It is not taking MKUserLocation type. @VRAwesome – Kavya Saravagi Sep 29 '16 at 10:11
  • Please create two different objects for different classes : `@property (retain, nonatomic) CLLocation *currentLocation;` and `@property (retain, nonatomic) MKUserLocation *userLocation;` – VRAwesome Sep 29 '16 at 10:14