【发布时间】:2016-09-29 06:27:31
【问题描述】:
我想跟踪我的 ios 设备的位置并在应用程序中通知它是否越过某些公里。假设我想通知它是否从设备的当前位置越过 1 公里。请帮助我是 iOS 编程新手。
我能够获取当前位置。 请帮忙。
这是我能做的。
- (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];
}
【问题讨论】:
-
使用 locationManager.distanceFilter = 1000; // 米
-
你需要计算当前位置到旧位置的距离。 CLLocationDistance dist = [loc1 distanceFromLocation:loc2]; 并在它给你 1 公里时通知。
-
您能否通过示例代码解释一下.. 怎么做.. @EktaMakadiya
标签: ios objective-c push-notification mapkit cllocationmanager