[英]Track location of iphone and notify if crosses certain kilometeres in ios Objective C
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. 我想跟踪我的ios设备的位置并在应用中通知它是否超过了一定的公里数。假设我想通知它是否与设备的当前位置相距1公里。 Please help I am new in iOS programming.
请帮助我是iOS编程的新手。
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];
}
self.currentLocation
must be an object of CLLocation
. self.currentLocation
必须是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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.