Ios開發之定位CLLocationManager

来源:http://www.cnblogs.com/jerehedu/archive/2016/02/18/5198011.html
-Advertisement-
Play Games

Ios中的定位功能是通過 Core Location框架實現的。它和地圖開發框架是相互獨立的。在Core Location中主要實現了定位和地理編碼的功能! 下麵我們就來介紹一下它的屬性,方法和代理方法! 屬性: desiredAccuracy:定位精度,是一個枚舉類型 //kCLLocationA


  Ios中的定位功能是通過 Core Location框架實現的。它和地圖開發框架是相互獨立的。在Core Location中主要實現了定位和地理編碼的功能!

  下麵我們就來介紹一下它的屬性,方法和代理方法!

  屬性:

desiredAccuracy:定位精度,是一個枚舉類型

//kCLLocationAccuracyBest:最精確定位

//kCLLocationAccuracyNearestTenMeters:十米誤差範圍

//kCLLocationAccuracyHundredMeters:百米誤差範圍

//kCLLocationAccuracyKilometer:千米誤差範圍

//kCLLocationAccuracyThreeKilometers:三千米誤差範圍

  distanceFilter: 位置信息更新最小距離,只有移動大於這個距離才更新位置信息,預設為kCLDistanceFilterNone:不進行距離限制

  對象方法:

startUpdatingLocation:開始定位追蹤

stopUpdatingLocation : 停止定位追蹤

startUpdatingHeading:開始方向追蹤

stopUpdatingHeading:停止方向追蹤

startMonitoringForRegion : 開始對某個區域進行追蹤

 stopMonitoringForRegion : 停止對某區域進行追蹤 

requestAlwaysAuthorization : 請求獲得應用一直使用定位服務授權,註意使用此方法前要在info.plist中配置NSLocationAlwaysUsageDescription

requestWhenInUseAuthorization : 請求獲得應用使用時的定位服務授權,註意使用此方法前在要在info.plist中配置NSLocationWhenInUseUsageDescription

  代理方法:

-(void)locationManager:(CLLocationManager*)manager didUpdateLocations:(NSArray *)locations; 位置發生改變後執行

       - (void)locationManager:(CLLocationManager *)manager

didUpdateHeading:(CLHeading *)newHeading; 導航方向發生變化後執行

- (void)locationManager:(CLLocationManager *)manager

    didEnterRegion:(CLRegion *)region;進入某個區域

- (void)locationManager:(CLLocationManager *)manager

    didExitRegion:(CLRegion *)region;走出某個區域之後執行

   開代碼:

//
//  ViewController.m
//  location
//
//  Created by jerehedu on 15/12/23.
//  Copyright (c) 2015年 jerehedu. All rights reserved.
//

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface ViewController ()<CLLocationManagerDelegate>{
    CLLocationManager *manager;
    CLGeocoder *geocoder;

}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 375, 122)];
    label.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:label];
    if ([CLLocationManager locationServicesEnabled]) {
        NSLog(@"定位服務已經打開");
    }
    //如果沒有授權則請求用戶授權
    if ([CLLocationManager authorizationStatus]==kCLAuthorizationStatusNotDetermined){
        [manager requestWhenInUseAuthorization];
    }else if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusAuthorizedWhenInUse){
        manager = [[CLLocationManager alloc]init];
        [manager requestAlwaysAuthorization ];
        [manager  requestWhenInUseAuthorization];
        manager.delegate = self;
        manager.desiredAccuracy = kCLLocationAccuracyBest;//精準度
        //    定位精度,枚舉類型:
        //    kCLLocationAccuracyBest:最精確定位
        //    CLLocationAccuracy kCLLocationAccuracyNearestTenMeters:十米誤差範圍
        //kCLLocationAccuracyHundredMeters:百米誤差範圍
        //kCLLocationAccuracyKilometer:千米誤差範圍
        //kCLLocationAccuracyThreeKilometers:三千米誤差範圍
        manager.distanceFilter = 1.0;//移動十米定位一次
        //    位置信息更新最小距離,只有移動大於這個距離才更新位置信息,預設為kCLDistanceFilterNone:不進行距離限制
        [manager startUpdatingLocation];
    }


    // Do any additional setup after loading the view, typically from a nib.
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    CLLocation *loctaion = [locations firstObject];
    NSLog(@"%f  %f",loctaion.coordinate.latitude,loctaion.coordinate.longitude );
    /*[geocoder reverseGeocodeLocation:loctaion completionHandler:^(NSArray *placemarks, NSError *error) {
     CLPlacemark *placemark=[placemarks firstObject];

     CLLocation *location=placemark.location;//位置
     CLRegion *region=placemark.region;//區域
     NSDictionary *addressDic= placemark.addressDictionary;//詳細地址信息字典,包含以下部分信息
     //        CLPlacemark *placemark = [placemarks firstObject];
     //        placemark.addressDictionary
     NSString *name=placemark.name;//地名
     NSString *thoroughfare=placemark.thoroughfare;//街道
     NSString *subThoroughfare=placemark.subThoroughfare; //街道相關信息,例如門牌等
     NSString *locality=placemark.locality; // 城市
     NSString *subLocality=placemark.subLocality; // 城市相關信息,例如標誌性建築
     NSString *administrativeArea=placemark.administrativeArea; // 州
     NSString *subAdministrativeArea=placemark.subAdministrativeArea; //其他行政區域信息
     NSString *postalCode=placemark.postalCode; //郵編
     NSString *ISOcountryCode=placemark.ISOcountryCode; //國家編碼
     NSString *country=placemark.country; //國家
     NSString *inlandWater=placemark.inlandWater; //水源、湖泊
     NSString *ocean=placemark.ocean; // 海洋
     NSArray *areasOfInterest=placemark.areasOfInterest; //關聯的或利益相關的地標
     }];*/
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

 

作者:傑瑞教育
出處:http://www.cnblogs.com/jerehedu/ 
版權聲明:本文版權歸傑瑞教育技有限公司和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。
技術咨詢:JRedu技術交流
 
您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 首先在plist 文件中 Supported interface orientations 選項 只留下一個 portrait 屏幕強制橫屏 使用以下代碼 self.navigationController.view.transform = CGAffineTransformMakeRotation
  • 前言:使用NSURLSessionDownloadTask滿足不這個需要離線斷點續傳的下載需求,所以這裡就需要使用NSURLSessionDataTask的代理方法來處理下載大文件,並且實現離線斷點續傳。 關於NSURLSessionDownloadTask的知識,可以請讀者閱讀本人的前篇:《iOS
  • <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" and
  • 要看一個應用是否支持橫豎屏,要看Xcode裡面的info.plist文件設置才清楚,每一個新建工程都會包含三個支持方式,即Supported interface orientations裡面的就是 Portrait (bottom home button)豎屏home鍵在底部時,即我們平常拿手機的樣
  • UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0, -480, ScreenWidth, 480)]; UIImageView *iconImage = [[UIImageView alloc]initWithFrame:CGRe
  • * What went wrong:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.> com.android.build.api.transform.TransformException: co
  • 是CApropertyAnimation的子類,跟CABasicAnimation的區別是:CABasicAnimation只能從一個數值(fromValue)變到另一個數值(toValue),而CAKeyframeAnimation會使用一個NSArray保存這些數值,就可以實現CALayer的某
  • 什麼是JRebel for Android? 一款Android studio插件——允許你修改正在運行中的應用程式,而且不必重新部署或重啟。支持所有運行Android 4.0及以上版本的手機和平板。由ZeroTurnaround開發。這是一款收費插件,每年49美元,免費試用21天。 通常在開發過程
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...