2011-05-13 23 views

回答

6

提高你的錄取率。使用以下方法獲取以英里或公里爲單位的距離。你必須在頭文件中聲明定時器來合成它。

//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相同 – Rani 2011-05-13 08:00:43

+0

我已經在開發人員文檔中搜索startReadingLocation方法,但它只包含stopupdatinglocation和startupdatinglocation。讀取位置方法是標準方法或用戶定義的 – Rani 2011-05-13 08:22:18

+0

@Rani我道歉,只是忘了把該方法放在那個代碼中。更新了代碼。 – 2011-05-13 08:30:49

2

爲了確定任何速度,你要弄清楚:

距離:以你的兩個最新的地理座標讀數(每個記錄的時間)來計算他們

之間的距離

要做到這一點,你可以閱讀有關理論here,或者你可以檢查出代碼here

速度:使用最新的GPS座標讀數和前一個之間的距離,通過公式speed = distance/duration計算速度。因此,您在兩個地理座標之間找到的距離除以持續時間(以秒或分鐘爲單位)或任何其他值。那麼你的答案將是速度是每英里x英里或公里。