2011-05-13 5 views

답변

6

당신의 수용률을 향상시킵니다. 다음 방법을 사용하여 거리를 킬로미터 또는 킬로미터 단위로 표시하십시오. 당신은 당신의 헤더 파일에 timer를 선언해야합니다.

//SpeedViewController.h 
#import <UIKit/UIKit.h> 
#import <CoreLocation/CoreLocation.h> 
#import <MobileCoreServices/UTCoreTypes.h> 

@interface SpeedViewController : UIViewController <CLLocationManagerDelegate,UINavigationControllerDelegate> 
{ 
    CLLocationManager *locManager; 
    CLLocationSpeed speed; 
    NSTimer *timer; 

    CLLocationSpeed currentSpeed; 
    float fltDistanceTravelled; 
} 

@property (nonatomic,retain) NSTimer *timer; 

-(float)getDistanceInKm:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation; 
-(float)getDistanceInMiles:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation; 

@end 

//SpeedViewController.h 
#import "SpeedViewController.h" 
#define kRequiredAccuracy 500.0 //meters 
#define kMaxAge 10.0 //seconds 
#define M_PI 3.14159265358979323846264338327950288 /* pi */ 


@implementation SpeedViewController 

@synthesize timer; 
- (void)startReadingLocation { 
    [locManager startUpdatingLocation]; 
} 
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 

    CLLocationManager *locationManager=[[CLLocationManager alloc] init]; 
    locationManager.delegate=self; 
    locationManager.desiredAccuracy=kCLLocationAccuracyBestForNavigation; 
    [locationManager startUpdatingLocation]; 
} 
- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc. that aren't in use. 
} 

- (void)viewDidUnload { 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


- (void)dealloc { 
    [super dealloc]; 
} 

- (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]; 
    } 
} 

//this is a wrapper method to fit the required selector signature 
- (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; 
} 

-(float)getDistanceInMiles:(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 = 3963; // 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(@"Miles-->%f",d); 

    return d; 
} 

@end 

추가 질문이있는 경우 의견을 남기십시오. 나는이 장치를 테스트하지 않았지만 논리적으로 작동해야합니다.

는 도움이되기를 바랍니다.

+0

startUpdatingLocation 메서드를 알고 있습니다 .is startReadingLocation 및 startUpdatingLocation same – Rani

+0

개발자 설명서에서 startReadingLocation 메서드를 검색했지만 stopupdatinglocation 및 startupdatinglocation 만 포함되어 있습니다. readinglocation 메소드는 표준 메소드 또는 사용자 정의 – Rani

+0

@ 라니 사과드립니다. 코드에 해당 메소드를 넣는 것을 잊어 버렸습니다. 코드를 업데이트했습니다. –

2

어떤 속도를 결정하기 위해, 당신은 내야 할 것입니다 :

거리을 :

그들 사이의 거리를 계산하기 위해 두 개의 최신 지리 좌표 측정 (각이 기록 된 시간 소요)

이 작업을 수행하려면 이론 here에 대해 읽거나 코드 here을 확인하십시오.

속도 : 최신 GPS 좌표 값과 이전 값 사이의 거리를 사용하여 속도 = 거리/지속 시간 공식으로 속도를 계산하십시오. 그래서, 두 지리적 좌표 사이에서 발견 한 거리를 초 또는 분 또는 초 단위의 길이로 나눈 값입니다. 그러면 속도는 x 마일이나 킬로미터입니다.

관련 문제