我有一個地圖application.when一個人從當前位置到特定位置的移動,我想計算所覆蓋的距離的用戶的平均速度這個人,這個人的當前速度,人。可任何人的平均速度幫我解決這個problem.please提供我任何SLOUTION解決這個problem.Thanks如何計算當前速度和從當前位置到特定loaction在地圖上旅行的iphone
7
A
回答
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
如果您還有其他疑問,請留下評論。我沒有在設備上測試過,但從邏輯上說它應該可以工作。
希望它有幫助。
2
相關問題
- 1. 如何獲得當前位置在iphone上的地圖位置
- 2. 從我的當前位置計算特定位置的給定距離和角度的緯度長度
- 3. 在地圖上的當前位置android
- 4. 在地圖上顯示當前位置
- 5. 在地圖上獲取當前位置
- 6. 從當前位置到在地圖上的給定位置的路線android
- 7. 計算當前位置的經度和緯度
- 8. Android Scroller:當前速度計算
- 9. 谷歌地圖在距離計算器中的當前位置
- 10. 谷歌地圖上的當前位置
- 11. Arcgis地圖當前位置
- 12. 當前位置與地圖
- 13. 如何從當前位置指定緯度和長度值?
- 14. 如何在運行時計算gmake中當前的Makefile位置
- 15. 如何從iPhone中確定當前位置iphone
- 16. 地圖縮放到當前位置
- 17. 如何將iphone中的當前位置
- 18. 如何在百度地圖中顯示當前位置android
- 19. iphone應用上的Google地圖:當前位置
- 20. iPhone上的「當前位置」Maps.app
- 21. 如何在iphone中獲取當前位置在地圖上的座標值?
- 22. 如何確定我的當前位置(經度和緯度)?
- 23. iphone mapview當前位置
- 24. 如何計算在當前視圖
- 25. 計算當前位置留給
- 26. 如何重定向在谷歌地圖中的當前位置
- 27. 如何使用iphone中的谷歌地圖從當前位置獲取地標?
- 28. 在當前位置啓動iPhone本地地圖應用程序
- 29. 地理位置和計算從當前經緯度到另一經度緯度的距離
- 30. 如何在「@」當前位置
我知道方法startUpdatingLocation .is startReadingLocation和startUpdatingLocation相同 – Rani 2011-05-13 08:00:43
我已經在開發人員文檔中搜索startReadingLocation方法,但它只包含stopupdatinglocation和startupdatinglocation。讀取位置方法是標準方法或用戶定義的 – Rani 2011-05-13 08:22:18
@Rani我道歉,只是忘了把該方法放在那個代碼中。更新了代碼。 – 2011-05-13 08:30:49