简体   繁体   English

在IOS7中获取经度和纬度

[英]Getting latitude and longitude in IOS7

I am developing an iPhone app which uses location services and runs in background.This app displays latitude and longitude exactly when it runs on IOS6 .But if i run the same app on IOS7 , sometimes it shows latitude and longitude exactly, sometimes -1 and -1. 我正在开发一个使用定位服务并在后台运行的iPhone应用程序。此应用程序在IOS6上运行时会准确显示纬度和经度。但是如果我在IOS7上运行同一应用程序,则有时会显示纬度和经度,有时是-1和-1。 I'm not understanding this behaviour on IOS7 . 我不了解IOS7上的这种行为。 what may be the reason for this behaviour? 这种行为的原因可能是什么?

In order to simulate a location, select the iOS Simulator Debug -> Location menu option and select either one of the pre-defined locations or journeys (such as City Bicycle Ride), or Custom Location… to enter a specific latitude and longitude. 为了模拟位置,请选择iOS Simulator Debug-> Location菜单选项,然后选择预定义的位置或旅程之一(例如City Bicycle Ride)或Custom Location…,以输入特定的纬度和经度。

This code worked for me. 这段代码对我有用。

Creating the CLLocationManager Object 创建CLLocationManager对象

The next task is to implement the code to create an instance of the CLLocationManager class. 下一个任务是实现代码以创建CLLocationManager类的实例。 Since this needs to occur when the view loads, an ideal location is in the view controller's viewDidLoad method in the LocationViewController.m file: 由于这需要在加载视图时发生,因此理想的位置是LocationViewController.m文件中视图控制器的viewDidLoad方法中:

- (void)viewDidLoad {
    [super viewDidLoad];
    _locationManager = [[CLLocationManager alloc] init];
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    _locationManager.delegate = self;
    [_locationManager startUpdatingLocation];
    _startLocation = nil;
}

Implementing the Action Method 实施行动方法

-(void)resetDistance:(id)sender
{
        _startLocation = nil;
}

Implementing the CLLocationManager Method 实现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;
}

In the End Try this method 到底试试这个方法

Try this method to get continuous updated latitude and longitude 尝试此方法以获得连续更新的纬度和经度

(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{  
    CLLocation *location_updated = [locations lastObject];   
    NSLog(@"updated coordinate are %@",location_updated);
}

Use this method for getting location continuously use this method 使用此方法获取位置连续使用此方法

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

{
location_updated = [locations lastObject];    
NSLog(@"updated coordinate are %@",location_updated);

}

may be you are using this method 可能是您正在使用此方法

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

This method has been Deprecated in iOS 6.0 此方法已在iOS 6.0中弃用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM