---
title: "iOS Dev (35) CoreLocation 获取经纬度"
author: "prevention"
date: "2014-01-24T00:20:00Z"
tags: []
slug: "ios-dev-35-corelocation"
draft: false
lastmod: "2014-02-03T16:56:12Z"
id: 2010854
url: "https://prevention.iteye.com/blog/2010854"
---

# iOS Dev (35) CoreLocation 获取经纬度

- 作者：大锐哥
- 博客：[http://blog.csdn.net/prevention](http://blog.csdn.net/prevention)

## AppDelegate.h

```
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
```

## AppDelegate.m

```
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    LocationViewController *lvc = [[LocationViewController alloc] init];
    self.window.rootViewController = lvc;

    [self.window makeKeyAndVisible];
    return YES;
}
```

## LocationViewController.h

```
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface LocationViewController : UIViewController <CLLocationManagerDelegate>
@property (strong, nonatomic) CLLocationManager *mgr;
@end
```

## LocationViewController.m

```
#import "LocationViewController.h"
@interface LocationViewController ()
@end

@implementation LocationViewController

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *newLocation = [locations lastObject];
    CLLocation *oldLocation;
    if (locations.count > 1)
    {
        oldLocation = [locations objectAtIndex:locations.count-2];
    }
    else
    {
        oldLocation = nil;
    }
    NSLog(@"didUpdateToLocation %@ from %@", newLocation, oldLocation);
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"%@",error);
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.mgr = [[CLLocationManager alloc] init];
    self.mgr.delegate = self;
    self.mgr.desiredAccuracy = kCLLocationAccuracyBest;
    self.mgr.distanceFilter = 5.0;
    [self.mgr startUpdatingLocation];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
```

\-

转载请注明来自大锐哥的博客：[http://blog.csdn.net/prevention](http://blog.csdn.net/prevention)