iOS專案開發實戰——使用CoreLocation實現定位
CoreLocation是蘋果官方提供的一個框架,可以實現很多地理位置操作上的功能。比如地圖顯示,定位,地理位置編碼等。現在我們來實現定位功能。
(1)程式碼實現如下:
#import "ViewController.h"
#import <MapKit/MapKit.h>
@interface ViewController ()<CLLocationManagerDelegate>
@property (nonatomic,strong) MKMapView *mapView;
@property (nonatomic,strong) CLLocationManager *locationManager;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
[_mapView setShowsUserLocation:true];
[_mapView setMapType:MKMapTypeStandard];
[self.view addSubview:_mapView];
//監測定位功能是否開啟
if ([CLLocationManager locationServicesEnabled]) {
NSLog(@"已經開啟定位");
if (!_locationManager) {
_locationManager = [[CLLocationManager alloc] init];
if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[_locationManager requestWhenInUseAuthorization];
[_locationManager requestAlwaysAuthorization];
}
//設定代理;
[_locationManager setDelegate:self];
//設定定位精度;
[_locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
//設定距離篩選;
[_locationManager setDistanceFilter:100];
//開始定位;
[_locationManager startUpdatingLocation];
}
}else{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示"
message:@"您沒有開啟定位功能"
delegate:nil
cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
[alertView show];
}
}
//授權狀態發生改變的時候;
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
switch (status) {
case kCLAuthorizationStatusDenied:
{
NSLog(@"拒絕了授權");
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示"
message:@"您已經拒絕了授權"
delegate:nil cancelButtonTitle:@"確定"
otherButtonTitles:nil, nil];
[alertView show];
}
break;
default:
break;
}
}
//定位成功以後呼叫
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
CLLocation *location = locations.lastObject;
MKCoordinateRegion coordinateRegion = MKCoordinateRegionMake(CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude), MKCoordinateSpanMake(0.1, 0.1));
[_mapView setRegion:[_mapView regionThatFits:coordinateRegion] animated:true];
}
//定位失敗以後呼叫
-(void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
NSLog(@"error:%@",error);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
(2)在模擬器下執行,效果如下:本人在中國,卻給我定位到美國了,可見使用模擬器來測試非常的不靠譜。
。
下面是在真機中執行,定位較為準確:
。
github主頁:https://github.com/chenyufeng1991 。歡迎大家訪問!
相關文章
- iOS專案開發實戰——使用CoreLocation獲取當前位置資訊iOS
- iOS專案開發實戰——CoreLocation地理編碼和反地理編碼iOS
- iOS專案開發實戰——使用UICollectionView實現瀑布流iOSUIView
- iOS專案開發實戰——使用定時器實現迴圈操作iOS定時器
- iOS專案開發實戰——使用程式碼實現頁面跳轉iOS
- iOS專案開發實戰——實現檢視切換動畫iOS動畫
- iOS專案開發實戰——使用CALayer和定時器實現進度條iOS定時器
- iOS專案開發實戰——使用CALayer實現圖片的淡入淡出效果iOS
- iOS開發專案實戰——Swift實現ScrollView滾動條功能iOSSwiftView
- iOS專案開發實戰——UILabel與取色器的使用iOSUI
- iOS專案開發實戰——實現蘋果本地訊息通知推送服務iOS蘋果
- iOS開發專案實戰——Swift實現圖片輪播與瀏覽iOSSwift
- iOS專案開發實戰——理解frame,bounds,centeriOS
- iOS專案開發實戰——檢視動畫效果iOS動畫
- iOS專案開發實戰——配置自定義動畫iOS動畫
- iOS專案開發實戰——plist陣列解析iOS陣列
- flask 專案開發實戰Flask
- iOS專案開發實戰——Swift實現多個TableView的側滑與切換iOSSwiftView
- iOS專案開發實戰——使用SDWebImage庫進行圖片請求iOSWeb
- iOS專案開發實戰——使用程式碼獲取螢幕寬高iOS
- iOS專案開發實戰——UIImageView的使用與圖片顯示模式iOSUIView模式
- iOS專案開發實戰——使用ShareSDK進行QQ和微信分享iOS
- iOS專案開發實戰——學會使用TableView列表控制元件(一)iOSView控制元件
- iOS專案開發實戰——學會使用TableView列表控制元件(二)iOSView控制元件
- iOS專案開發實戰——使用三種方式實現頁面跳轉與引數傳遞(一)iOS
- iOS專案開發實戰——使用三種方式實現頁面跳轉與引數傳遞(二)iOS
- iOS專案開發實戰——使用三種方式實現頁面跳轉與引數傳遞(三)iOS
- iOS專案開發實戰——UIView的層級關係iOSUIView
- iOS專案開發實戰——如何進行延時操作iOS
- iOS專案開發實戰——獲取本機IP地址iOS
- iOS專案開發實戰——使用手勢識別判斷使用者操作iOS
- iOS專案開發實戰——如何使用Autoresizing進行螢幕適配iOS
- iOS專案開發實戰——使用AFNetworking進行Http Get請求iOSHTTP
- iOS專案開發實戰——使用同步請求獲取網頁原始碼iOS網頁原始碼
- iOS專案開發實戰——彈出提示對話方塊iOS
- django專案開發實戰——部落格Django
- 小程式雲開發專案實戰
- Python專案開發實戰1Python