我有以下代码,用于获取用户行进的距离,并以两个标签返回其速度。
.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;
}
当我运行应用程序时,使用“高速公路驱动器”位置选项,没有任何文本字段正在更新,并且控制台没有提供任何信息(即行进的距离)
有谁可以请确定我需要修复的代码在哪里?
提前致谢。
答案 0 :(得分:1)
在你的方法中:
- (float)getDistanceInKm:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
return语句在文本设置之前,因此永远不会执行该设置,以修复此操作将return语句移动到方法的末尾。