#import <CoreLocation/CoreLocation.h>
CLLocationManager *locManager = [[CLLocationManager alloc] init]; locManager.delegate = self; [locManager startUpdatingLocation];
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *curLocation = [locations lastObject];
if(curLocation.horizontalAccuracy > 0)
{
NSLog(@"当前位置:%.0f,%.0f +/- %.0f meters",curLocation.coordinate.longitude,
curLocation.coordinate.latitude,
curLocation.horizontalAccuracy);
}
if(curLocation.verticalAccuracy > 0)
{
NSLog(@"当前海拔高度:%.0f +/- %.0f meters",curLocation.altitude,curLocation.verticalAccuracy);
}
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{ //此方法为定位失败的时候调用。并且由于会在失败以后重新定位,所以必须在末尾停止更新
if(error.code == kCLErrorLocationUnknown)
{
NSLog(@"Currently unable to retrieve location.");
}
else if(error.code == kCLErrorNetwork)
{
NSLog(@"Network used to retrieve location is unavailable.");
}
else if(error.code == kCLErrorDenied)
{
NSLog(@"Permission to retrieve location is denied.");
[manager stopUpdatingLocation];
}
}
extern const CLLocationAccuracy kCLLocationAccuracyBestForNavigation; extern const CLLocationAccuracy kCLLocationAccuracyBest; extern const CLLocationAccuracy kCLLocationAccuracyNearestTenMeters; extern const CLLocationAccuracy kCLLocationAccuracyHundredMeters; extern const CLLocationAccuracy kCLLocationAccuracyKilometer; extern const CLLocationAccuracy kCLLocationAccuracyThreeKilometers;
CLLocationManager *locManager = [[CLLocationManager alloc] init]; locManager.delegate = self; locManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;//定位精度百米以内 locManager.distanceFilter = 200;//水平或者垂直移动200米调用代理更新位置 [locManager startUpdatingLocation];//启动位置更新
- (BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)manager
{
return YES;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
}
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
if(newHeading.headingAccuracy >=0)
{
NSString *headingDesc = [NSString stringWithFormat:@"%.0f degrees (true), %.0f degrees (magnetic)",newHeading.trueHeading,newHeading.magneticHeading];
NSLog(@"%@",headingDesc);
}
}
#import "ViewController.h"
#define kDestLongitude 113.12 //精度
#define kDestLatitude 22.23 //纬度
#define kRad2Deg 57.2957795 // 180/π
#define kDeg2Rad 0.0174532925 // π/180
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UILabel *lblMessage;
@property (strong, nonatomic) IBOutlet UIImageView *imgView;
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) CLLocation *recentLocation;
-(double)headingToLocation:(CLLocationCoordinate2D)desired current:(CLLocationCoordinate2D)current;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
self.locationManager.distanceFilter = 1609; //1英里≈1609米
[self.locationManager startUpdatingLocation];
if([CLLocationManager headingAvailable])
{
self.locationManager.headingFilter = 10; //10°
[self.locationManager startUpdatingHeading];
}
}
/*
* According to Movable Type Scripts
* http://mathforum.org/library/drmath/view/55417.html
*
* Javascript:
*
* var y = Math.sin(dLon) * Math.cos(lat2);
* var x = Math.cos(lat1)*Math.sin(lat2) -
* Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon);
* var brng = Math.atan2(y, x).toDeg();
*/
-(double)headingToLocation:(CLLocationCoordinate2D)desired current:(CLLocationCoordinate2D)current
{
double lat1 = current.latitude*kDeg2Rad;
double lat2 = desired.latitude*kDeg2Rad;
double lon1 = current.longitude;
double lon2 = desired.longitude;
double dlon = (lon2-lon1)*kDeg2Rad;
double y = sin(dlon)*cos(lat2);
double x = cos(lat1)*sin(lat2) - sin(lat1)*cos(lat2)*cos(dlon);
double heading=atan2(y,x);
heading=heading*kRad2Deg;
heading=heading+360.0;
heading=fmod(heading,360.0);
return heading;
}
//处理航向
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
if(self.recentLocation!=nil && newHeading.headingAccuracy>=0)
{
CLLocation *destLocation = [[CLLocation alloc] initWithLatitude:kDestLatitude longitude:kDestLongitude];
double course = [self headingToLocation:destLocation.coordinate current:self.recentLocation.coordinate];
double delta = newHeading.trueHeading - course;
if (abs(delta) <= 10)
{
self.imgView.image = [UIImage imageNamed:@"up_arrow.png"];
}
else
{
if (delta > 180)
{
self.imgView.image = [UIImage imageNamed:@"right_arrow.png"];
}
else if (delta > 0)
{
self.imgView.image = [UIImage imageNamed:@"left_arrow.png"];
}
else if (delta > -180)
{
self.imgView.image = [UIImage imageNamed:@"right_arrow.png"];
}
else
{
self.imgView.image = [UIImage imageNamed:@"left_arrow.png"];
}
}
self.imgView.hidden = NO;
}
else
{
self.imgView.hidden = YES;
}
}
//处理定位成功
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *curLocation = [locations lastObject];
if(curLocation.horizontalAccuracy >= 0)
{
self.recentLocation = curLocation;
CLLocation *destLocation = [[CLLocation alloc] initWithLatitude:kDestLatitude longitude:kDestLongitude];
CLLocationDistance distance = [destLocation distanceFromLocation:curLocation];
if(distance<500)
{
[self.locationManager stopUpdatingLocation];
[self.locationManager stopUpdatingHeading];
self.lblMessage.text = @"您已经到达目的地!";
}
else
{
self.lblMessage.text = [NSString stringWithFormat:@"距离目的地还有%f米",distance];
}
}
}
//处理定位失败
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
if(error.code == kCLErrorLocationUnknown)
{
NSLog(@"Currently unable to retrieve location.");
}
else if(error.code == kCLErrorNetwork)
{
NSLog(@"Network used to retrieve location is unavailable.");
}
else if(error.code == kCLErrorDenied)
{
NSLog(@"Permission to retrieve location is denied.");
[self.locationManager stopUpdatingLocation];
self.locationManager = nil;
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有