简体   繁体   中英

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. I'm not understanding this behaviour on 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.

This code worked for me.

Creating the CLLocationManager Object

The next task is to implement the code to create an instance of the CLLocationManager class. 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:

- (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

#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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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