算两个CLLocation之间的距离本来想用一个地球半径相关复杂公式算的,但是既然有现成的,还是省省吧……
用这个初始化CLLocationManager:
- (void)startLocationUpdates
{
// Create the location manager if this object does not
// already have one.
if (self.locationManager == nil) {
self.locationManager = [[CLLocationManager alloc] init];
}
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.activityType = CLActivityTypeFitness;
// Movement threshold for new events.
self.locationManager.distanceFilter = 10; // meters
[self.locationManager startUpdatingLocation];
}
用这段更新已经跑出的距离:
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
for (CLLocation *newLocation in locations) {
if (newLocation.horizontalAccuracy < 20) {
// update distance
if (self.locations.count > 0) {
self.distance += [newLocation distanceFromLocation:self.locations.lastObject];
}
[self.locations addObject:newLocation];
}
}
}
注意:在使用CLLocationManager时,需要在Supporting File 的 info.plist 中加这样两行,否则没有办法用(调了好久才发现,还没搞清楚为啥)。
<!-- for requestAlwaysAuthorization -->
<key>NSLocationAlwaysUsageDescription</key>
<string>Explain for what are you using the user location</string>
<!-- for requestWhenInUseAuthorization -->
<key>NSLocationWhenInUseUsageDescription</key>
<string>Explain for what are you using the user location</string>
代码链接:https://gist.github.com/1992chenlu/2ec71e9dc5d3d6588160
Reference: http://www.raywenderlich.com/73984/make-app-like-runkeeper-part-1
没有评论:
发表评论