最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

IOS入門筆記之地理位置定位系統(tǒng)

 更新時(shí)間:2016年01月27日 09:25:13   作者:Good_HeYang  
關(guān)于地理位置及定位系統(tǒng),在iOS開發(fā)中也比較常見,接下來(lái)通過(guò)本文給大家介紹IOS入門筆記之地理位置定位系統(tǒng),對(duì)ios地理位置定位系統(tǒng)感興趣的朋友一起學(xué)習(xí)吧

前言:關(guān)于地理位置及定位系統(tǒng),在iOS開發(fā)中也比較常見,比如美團(tuán)外面的餐飲店鋪的搜索,它首先需要用戶當(dāng)前手機(jī)的位置,然后在這個(gè)位置附近搜索相關(guān)的餐飲店鋪的位置,并提供相關(guān)的餐飲信息,再比如最常見的就是地圖導(dǎo)航,地圖導(dǎo)航更需要定位服務(wù),然后根據(jù)用戶的目的地選出一條路線。其實(shí),作為手機(jī)用戶這么長(zhǎng)時(shí)間,或多或少會(huì)發(fā)現(xiàn)在有些app應(yīng)用首次在你的手機(jī)安裝成功后,首次啟動(dòng)可能就會(huì)提示"是否同意XXx(比如百度瀏覽器)獲取當(dāng)前位置"等這樣一類的信息。可見地理位置及定位系統(tǒng)是企業(yè)app開發(fā)必不可少的技能。

本章將提供Swift版本和Objective-C兩個(gè)版本的入門代碼,分別實(shí)現(xiàn)顯示當(dāng)前手機(jī)或者是模擬器的地理經(jīng)緯度坐標(biāo)。

寫在正式學(xué)習(xí)前的小貼士:

這是因?yàn)閤code升級(jí)造成的定位權(quán)限設(shè)置問(wèn)題。
升級(jí)xcode6、xcode7以后打開以前xcode5工程,程序不能定位。工程升級(jí)到xcode6或xcode7編譯時(shí)需要iOS8 要自己寫授權(quán),不然沒(méi)權(quán)限定位。

解決方法:

首先在 info.plist里加入對(duì)應(yīng)的缺省字段 ,值設(shè)置為YES(前臺(tái)定位寫上邊字段,前后臺(tái)定位寫下邊字段)
NSLocationWhenInUseUsageDescription //允許在前臺(tái)獲取GPS的描述
NSLocationAlwaysUsageDescription //允許在前、后臺(tái)獲取GPS的描述

設(shè)置的圖示:


好了,如果設(shè)置好了,那就正式進(jìn)入編碼學(xué)習(xí)吧,首先熟悉蘋果提供的關(guān)于定位服務(wù)相關(guān)的類,方法以及屬性:

1、定位服務(wù)和地圖應(yīng)用的介紹

定位服務(wù): 獲取用戶當(dāng)前的位置信息,針對(duì)用戶的位置信息做相關(guān)的數(shù)據(jù)處理。

地圖應(yīng)用: 根據(jù)實(shí)際需求展示地圖和周邊環(huán)境信息,基于用戶當(dāng)前位置展示用戶所關(guān)注的地圖位置信息、以及為用戶導(dǎo)航。

•定位服務(wù)要掌握的:

•主要操作的類:CLLocationManager

•所屬庫(kù):CoreLocation

•結(jié)構(gòu)體:CLLocationCoordinate2D(經(jīng)緯度)、CLCLocationCoorRegion(區(qū)域)

•地圖應(yīng)用需要掌握的:

•框架:MapKit

•操作類:MKMapView

2、定位服務(wù)

•屬性:

•desiredAccuracy設(shè)置定位精確度,這是一個(gè)常量屬性,一般用best
•distanceFilter 重新定位的最小變化距離

方法:

•設(shè)置什么時(shí)候開啟定位的狀態(tài) •requestAlwaysAuthorization() 始終開啟定位
•requestWhenInUseAuthorization() 當(dāng)app進(jìn)入前臺(tái)的時(shí)候開啟定位(iOS8的新方法)
•類方法locationServicesEnabled() 是否有定位服務(wù)功能(CLLocationManager)
•startUpdatingLocation() 開啟定位

代理:

•代理的協(xié)議:
•代理的方法:可以直接進(jìn)入這個(gè)庫(kù)的API查看,只要就是定位錯(cuò)誤調(diào)用的代理方法,定位成功調(diào)用的代理方法等等;

涉及到的對(duì)象

•locations: CLLocation 該CLLocation對(duì)象的屬性: •coordinate •longitude/latitude

英語(yǔ)詞匯積累:

•accuracy 英 'ækjʊrəsɪ n. [數(shù)] 精確度,準(zhǔn)確性
•filter 英 'fɪltə 濾波器 過(guò)濾器;篩選;濾光器 過(guò)濾;滲透;用過(guò)濾法除去

下面提供的是Swift源碼:

//
// ViewController.swift
// LocationManager
//
// Created by HEYANG on //.
// Copyright © 年 HEYANG. All rights reserved.
//
import UIKit
// 需要導(dǎo)入CoreLocation框架
import CoreLocation
class ViewController: UIViewController,CLLocationManagerDelegate {
// 聲明一個(gè)全局變量
var locationManager:CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
// 設(shè)置定位的精確度
locationManager.desiredAccuracy = kCLLocationAccuracyBest
// 設(shè)置定位變化的最小距離 距離過(guò)濾器
locationManager.distanceFilter = 
// 設(shè)置請(qǐng)求定位的狀態(tài)
if #available(iOS ., *) {
locationManager.requestWhenInUseAuthorization()
} else {
// Fallback on earlier versions
print("hello")
}//這個(gè)是在ios之后才有的
// 設(shè)置代理為當(dāng)前對(duì)象
locationManager.delegate = self;
if CLLocationManager.locationServicesEnabled(){
// 開啟定位服務(wù)
locationManager.startUpdatingLocation()
}else{
print("沒(méi)有定位服務(wù)")
}
}
// 定位失敗調(diào)用的代理方法
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print(error)
}
// 定位更新地理信息調(diào)用的代理方法
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if locations.count > 
{
let locationInfo = locations.last!
let alert:UIAlertView = UIAlertView(title: "獲取的地理坐標(biāo)",
message: "經(jīng)度是:\(locationInfo.coordinate.longitude),維度是:\(locationInfo.coordinate.latitude)",
delegate: nil, cancelButtonTitle: "是的")
alert.show()
}
}
}

下面是Objective-C的源碼:

//
// ViewController.m
// LocationManager
//
// Created by HEYANG on //.
// Copyright © 年 HEYANG. All rights reserved.
//
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController () <CLLocationManagerDelegate>
/** 全局定位對(duì)象 */
@property (nonatomic,strong)CLLocationManager *locationManager;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
CLLocationManager* locationManager = [[CLLocationManager alloc] init];
// 設(shè)置定位精確度
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
// 設(shè)置定位變化最小距離
locationManager.distanceFilter = ;
// 設(shè)置定位服務(wù)的使用狀態(tài)
[locationManager requestWhenInUseAuthorization]; 
locationManager.delegate = self;
if ([CLLocationManager locationServicesEnabled]) {
[locationManager startUpdatingLocation];
}else{
NSLog(@"本機(jī)不支持定位服務(wù)功能");
}
self.locationManager = locationManager;
}
// 定位失敗調(diào)用的代理方法
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
NSLog(@"錯(cuò)誤信息:%@",error);
}
// 定位數(shù)據(jù)更新調(diào)用的代理方法
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
if (locations.count > ) {
CLLocation* location = locations.lastObject;
CLLocationCoordinateD coordinateD = location.coordinate;
NSString* message = [NSString stringWithFormat:@"經(jīng)度:%lf,維度是:%lf",coordinateD.longitude,coordinateD.latitude];
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"顯示當(dāng)前位置的經(jīng)緯度"                 message:message delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
[alertView show];
}
}
@end 

以上是小編給大家分享的IOS入門筆記之地理位置定位系統(tǒng),希望對(duì)大家有所幫助。

相關(guān)文章

最新評(píng)論

铁力市| 自治县| 山东| 台南市| 曲水县| 秦皇岛市| 瑞金市| 蓝田县| 进贤县| 简阳市| 石台县| 忻城县| 太湖县| 大城县| 富源县| 耒阳市| 民乐县| 龙里县| 桓仁| 徐汇区| 苗栗市| 永德县| 南汇区| 岳普湖县| 岳西县| 深泽县| 公安县| 土默特右旗| 吕梁市| 象山县| 宜都市| 周宁县| 庆城县| 清河县| 漳平市| 古蔺县| 扎赉特旗| 霍山县| 故城县| 读书| 那曲县|