iOS系統自帶定位,用CLLocationManager就能夠輕鬆的實現定位的操做,得到的是一組經緯度,固然,也能夠根據給出的經緯度獲取相應的省份、城市、街道等信息,下面就看一個根據經緯度得到城市的demo:git
由於獲取經緯度須要CLLocationManager類,而這個類包含在CoreLocation框架中,獲取城市信息須要mapKit框架,因此須要首先在工程中導入這兩個框架:框架
導入框架的步驟:選擇1.target——2.Build Phases——3.Link Binary With Libraries ——4.點擊「+」號:如圖所示步驟:
oop
點擊加號以後在搜索框裏輸入相應的框架,便可搜索到,如圖所示:post
下面就該寫代碼了,首先在視圖控制器中導入:ui
1
2
|
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
|
兩個頭文件,而後.m中的具體代碼以下:atom
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
#import "ANNViewController.h"
@interface ANNViewController ()
@property (strong, nonatomic) IBOutlet UILabel *longitude;
@property (strong, nonatomic) IBOutlet UILabel *latitude;
@property (strong, nonatomic) IBOutlet UILabel *location;
@property (strong, nonatomic) CLLocationManager *locationManager;
@end
@implementation ANNViewController
- (
void
)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor whiteColor];
//建立CLLocationManager對象
self.locationManager = [[CLLocationManager alloc] init];
//設置代理爲本身
self.locationManager.delegate = self;
}
- (IBAction)locationButton:(UIButton *)sender {
[self.locationManager startUpdatingLocation];
}
- (
void
)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
//將經度顯示到label上
self.longitude.text = [NSString stringWithFormat:@
"%lf"
, newLocation.coordinate.longitude];
//將緯度現實到label上
self.latitude.text = [NSString stringWithFormat:@
"%lf"
, newLocation.coordinate.latitude];
// 獲取當前所在的城市名
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
//根據經緯度反向地理編譯出地址信息
[geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *array, NSError *error)
{
if
(array.count > 0)
{
CLPlacemark *placemark = [array objectAtIndex:0];
//將得到的全部信息顯示到label上
self.location.text = placemark.name;
//獲取城市
NSString *city = placemark.locality;
if
(!city) {
//四大直轄市的城市信息沒法經過locality得到,只能經過獲取省份的方法來得到(若是city爲空,則可知爲直轄市)
city = placemark.administrativeArea;
}
NSLog(@
"city = %@"
, city);
}
else
if
(error == nil && [array count] == 0)
{
NSLog(@
"No results were returned."
);
}
else
if
(error != nil)
{
NSLog(@
"An error occurred = %@"
, error);
}
}];
//系統會一直更新數據,直到選擇中止更新,由於咱們只須要得到一次經緯度便可,因此獲取以後就中止更新
[manager stopUpdatingLocation];
}
|
主要就是直轄市的城市得到須要拐個彎,iOS7添加了一個新的方法,代替了上面這個方法:spa
1
2
3
4
5
6
7
8
9
|
- (
void
)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
NSLog(@
"longitude = %f"
, ((CLLocation *)[locations
lastObject]).coordinate.longitude);
NSLog(@
"latitude = %f"
, ((CLLocation *)[locations lastObject]).coordinate.latitude);
[manager stopUpdatingLocation];
}
|
後面的處理和上面的方法同樣,你們能夠看一下。代理
另外還有一些CLGeocoder的屬性以下:rest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@property (nonatomic, readonly) NSDictionary *addressDictionary;
// address dictionary properties
@property (nonatomic, readonly) NSString *name;
// eg. Apple Inc.
@property (nonatomic, readonly) NSString *thoroughfare;
// street address, eg. 1 Infinite Loop
@property (nonatomic, readonly) NSString *subThoroughfare;
// eg. 1
@property (nonatomic, readonly) NSString *locality;
// city, eg. Cupertino
@property (nonatomic, readonly) NSString *subLocality;
// neighborhood, common name, eg. Mission District
@property (nonatomic, readonly) NSString *administrativeArea;
// state, eg. CA
@property (nonatomic, readonly) NSString *subAdministrativeArea;
// county, eg. Santa Clara
@property (nonatomic, readonly) NSString *postalCode;
// zip code, eg. 95014
@property (nonatomic, readonly) NSString *ISOcountryCode;
// eg. US
@property (nonatomic, readonly) NSString *country;
// eg. United States
@property (nonatomic, readonly) NSString *inlandWater;
// eg. Lake Tahoe
@property (nonatomic, readonly) NSString *ocean;
// eg. Pacific Ocean
@property (nonatomic, readonly) NSArray *areasOfInterest;
// eg. Golden Gate Park
|