//
// ViewController.m
// XMLocation
//
// Created by Floating_SH on 15/12/29.
// Copyright © 2015年 SH. All rights reserved.
//
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>
// 定位管理類.
@property (nonatomic, strong) CLLocationManager *manager;
// 編碼和反編碼工具類
@property (nonatomic, strong) CLGeocoder *geo;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 判斷當前設備是否支持定位
if ([CLLocationManager locationServicesEnabled] == YES) {
NSLog(@"支持定位");
}else{
NSLog(@"不支持定位功能");
}
// 實例化定位管理對象
self.manager = [[CLLocationManager alloc] init];
self.manager.delegate = self;
// 初始化編碼類Geo
self.geo = [[CLGeocoder alloc]init];
// 向系統和用戶申請使用權限.
if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse) {
// 開始申請
[self.manager requestWhenInUseAuthorization];
}
// 開始爲定位做準備
// 表示多少米定位一次
self.manager.distanceFilter = 10;
// 定位的精度(效果)如何
self.manager.desiredAccuracy = kCLLocationAccuracyBest;
// 開啓定位
[self.manager startUpdatingLocation];
}
// 定位成功之後返回
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
CLLocation *location = [locations lastObject];
NSLog(@"經度:%f, 緯度:%f, 海拔:%f, 航向:%f, 行走速度:%f",
location.coordinate.longitude,
location.coordinate.latitude,
location.altitude,
location.course,
location.speed);
NSLog(@"定位成功");
// [self getCoorWithCityName:@"沂南縣"];
// [self getCityNameWithCoor:CLLocationCoordinate2DMake(35.547002, 118.455395)];
CLLocation *locat1 = [[CLLocation alloc]initWithLatitude:40 longitude:116];
CLLocation *locat2 = [[CLLocation alloc]initWithLatitude:41 longitude:116];
NSLog(@"一個緯度相差%f米",[locat1 distanceFromLocation:locat2]);
// 關閉定位
[self.manager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
NSLog(@"定位失敗");
}
// 編碼, 根據城市名稱返回城市的經緯度.
- (void)getCoorWithCityName:(NSString *)cityName{
[self.geo geocodeAddressString:cityName completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (error) {
NSLog(@"編碼失敗:%@",error);
}
// 返回最後一個編碼信息
CLPlacemark *placeMark = [placemarks lastObject];
NSLog(@"%@ 城市的經度是:%f, 緯度是:%f",
cityName,
placeMark.location.coordinate.longitude,
placeMark.location.coordinate.latitude);
}];
}
// 反編碼, 根據座標返回城市名稱.
- (void)getCityNameWithCoor:(CLLocationCoordinate2D)coor{
CLLocation *locat = [[CLLocation alloc]initWithLatitude:coor.latitude longitude:coor.longitude];
[self.geo reverseGeocodeLocation:locat completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (error) {
NSLog(@"反編碼失敗:%@",error);
}
CLPlacemark *placeMark = [placemarks lastObject];
[placeMark.addressDictionary enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
NSLog(@"%@:%@",key,obj);
}];
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
------------------------------- 下方爲plist文件中的內容 --------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSLocationWhenInUseUsageDescription</key>
<string>主人請讓我定位</string>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
</dict>
</plist>