【发布时间】:2013-11-19 23:04:51
【问题描述】:
我有以下代码,用于获取用户行进的距离,并在两个标签中返回他们的速度。
.h 文件
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MobileCoreServices/MobileCoreServices.h>
@interface pointToPointViewController : UIViewController<CLLocationManagerDelegate, UINavigationControllerDelegate>
{
CLLocationManager *locManager;
CLLocationSpeed speed;
NSTimer *timer;
UILabel *speedText;
UILabel *locationText;
UILabel * distanceText;
CLLocationSpeed currentSpeed;
float fltDistanceTravelled;
}
@property (strong, nonatomic) IBOutlet UILabel *locationText;
@property (strong, nonatomic) IBOutlet UILabel *distanceText;
@property (nonatomic, retain) NSTimer *timer;
-(float)getDistanceInKm:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation;
@property (strong, nonatomic) IBOutlet UILabel *speedText;
.m 文件
import "pointToPointViewController.h"
#define kRequiredAccuracy 500.0 //meters
#define kMaxAge 10.0 //seconds
#define M_PI 3.14159265358979323846264338327950288 /* pi */
@interface pointToPointViewController ()
@end
@implementation pointToPointViewController
@synthesize timer;
@synthesize speedText;
@synthesize distanceText;
@synthesize locationText;
-(void)startReadingLocation{
[locManager startUpdatingLocation];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
CLLocationManager *locationManager=[[CLLocationManager alloc] init];
locationManager.delegate=self;
locationManager.desiredAccuracy=kCLLocationAccuracyBestForNavigation;
[locationManager startUpdatingLocation];
speedText = [[UILabel alloc]init];
NSString *speedString = [NSString stringWithFormat:@"speed is %.2f", currentSpeed];
speedText.text = speedString;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"new->%d old->%d",(newLocation==NULL),(oldLocation==NULL));
if(newLocation && oldLocation)
{
fltDistanceTravelled +=[self getDistanceInKm:newLocation fromLocation:oldLocation];
}
}
- (void)timeIntervalEnded:(NSTimer*)timer {
fltDistanceTravelled=0;
[self startReadingLocation];
}
-(float)getDistanceInKm:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
float lat1,lon1,lat2,lon2;
lat1 = newLocation.coordinate.latitude * M_PI / 180;
lon1 = newLocation.coordinate.longitude * M_PI / 180;
lat2 = oldLocation.coordinate.latitude * M_PI / 180;
lon2 = oldLocation.coordinate.longitude * M_PI / 180;
float R = 6371; // km
float dLat = lat2-lat1;
float dLon = lon2-lon1;
float a = sin(dLat/2) * sin(dLat/2) + cos(lat1) * cos(lat2) * sin(dLon/2) * sin(dLon/2);
float c = 2 * atan2(sqrt(a), sqrt(1-a));
float d = R * c;
NSLog(@"Kms-->%f",d);
return d;
NSString *distancestring = [NSString stringWithFormat:@"%f.2",d];
distanceText.text = distancestring;
}
当我使用“高速公路”位置选项运行应用程序时,没有任何文本字段正在更新,并且控制台没有提供任何信息(即行驶距离)
谁能确定我的代码中需要修复的地方?
提前致谢。
【问题讨论】:
标签: objective-c ios7 gps