[英]Getting latitude and longitude in IOS7
我正在开发一个使用定位服务并在后台运行的iPhone应用程序。此应用程序在IOS6上运行时会准确显示纬度和经度。但是如果我在IOS7上运行同一应用程序,则有时会显示纬度和经度,有时是-1和-1。 我不了解IOS7上的这种行为。 这种行为的原因可能是什么?
为了模拟位置,请选择iOS Simulator Debug-> Location菜单选项,然后选择预定义的位置或旅程之一(例如City Bicycle Ride)或Custom Location…,以输入特定的纬度和经度。
这段代码对我有用。
创建CLLocationManager对象
下一个任务是实现代码以创建CLLocationManager类的实例。 由于这需要在加载视图时发生,因此理想的位置是LocationViewController.m文件中视图控制器的viewDidLoad方法中:
- (void)viewDidLoad {
[super viewDidLoad];
_locationManager = [[CLLocationManager alloc] init];
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
_locationManager.delegate = self;
[_locationManager startUpdatingLocation];
_startLocation = nil;
}
实施行动方法
-(void)resetDistance:(id)sender
{
_startLocation = nil;
}
实现CLLocationManager方法
#pragma mark -
#pragma mark CLLocationManagerDelegate
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSString *currentLatitude = [[NSString alloc]
initWithFormat:@"%+.6f",
newLocation.coordinate.latitude];
_latitude.text = currentLatitude;
NSString *currentLongitude = [[NSString alloc]
initWithFormat:@"%+.6f",
newLocation.coordinate.longitude];
_longitude.text = currentLongitude;
NSString *currentHorizontalAccuracy =
[[NSString alloc]
initWithFormat:@"%+.6f",
newLocation.horizontalAccuracy];
_horizontalAccuracy.text = currentHorizontalAccuracy;
NSString *currentAltitude = [[NSString alloc]
initWithFormat:@"%+.6f",
newLocation.altitude];
_altitude.text = currentAltitude;
NSString *currentVerticalAccuracy =
[[NSString alloc]
initWithFormat:@"%+.6f",
newLocation.verticalAccuracy];
_verticalAccuracy.text = currentVerticalAccuracy;
if (_startLocation == nil)
_startLocation = newLocation;
CLLocationDistance distanceBetween = [newLocation
distanceFromLocation:_startLocation];
NSString *tripString = [[NSString alloc]
initWithFormat:@"%f",
distanceBetween];
_distance.text = tripString;
}
到底试试这个方法
尝试此方法以获得连续更新的纬度和经度
(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *location_updated = [locations lastObject];
NSLog(@"updated coordinate are %@",location_updated);
}
使用此方法获取位置连续使用此方法
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
location_updated = [locations lastObject];
NSLog(@"updated coordinate are %@",location_updated);
}
可能是您正在使用此方法
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
此方法已在iOS 6.0中弃用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.