成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

iPhone GPS定位系統(tǒng) 實(shí)例操作

移動(dòng)開(kāi)發(fā) iOS
本文介紹的是iPhone GPS定位系統(tǒng) 實(shí)例操作,GPS越來(lái)越普遍使用了,那么本文講述的主要是在iphone手機(jī)中的使用,先來(lái)看內(nèi)容。

iPhone GPS定位系統(tǒng) 實(shí)例操作是本文要介紹的內(nèi)容,先來(lái)看本文詳細(xì)內(nèi)容。如今,配備GPS功能的移動(dòng)設(shè)備越來(lái)越普遍了,使用GPS定位系統(tǒng),可以精確地定位你當(dāng)前所在的地理位置,但由于GPS接收機(jī)需要對(duì)準(zhǔn)天空才能工作,因此在室內(nèi)環(huán)境基本無(wú)用。

另一個(gè)找到自己所在位置的有效方法是使用手機(jī)基站,手機(jī)開(kāi)機(jī)時(shí),它會(huì)與周?chē)幕颈3致?lián)系,如果你知道這些基站的身份,就可以使用各種數(shù)據(jù)庫(kù) (包含基站的身份和它們的確切地理位置)計(jì)算出手機(jī)的物理位置。基站不需要衛(wèi)星,和GPS不同,它對(duì)室內(nèi)環(huán)境一樣管用。但它沒(méi)有GPS那樣精確,它的精度取決于基站的密度,它在基站密集型區(qū)域的準(zhǔn)確度最高。

提示:第一代iPhone并沒(méi)有配置GPS接收器,基站方式不能應(yīng)用到iPod Touch上,因?yàn)樗皇鞘謾C(jī)。

第三種方法是依賴(lài)Wi-Fi,使用這種方法時(shí),設(shè)備連接到Wi-Fi網(wǎng)絡(luò),通過(guò)檢查服務(wù)提供商的數(shù)據(jù)確定位置,它既不依賴(lài)衛(wèi)星,也不依賴(lài)基站,因此這個(gè)方法對(duì)于可以連接到Wi-Fi網(wǎng)絡(luò)的區(qū)域有效,但它的精確度也是這三個(gè)方法中最差的。

定位框架內(nèi)核

在iPhone上,蘋(píng)果提供了定位框架內(nèi)核,以幫助你確定你的物理位置,這個(gè)框架的美妙之處在于它使用了前面提到的所有三種方法,具體使用的是哪種方法對(duì)于開(kāi)發(fā)者來(lái)說(shuō)是透明的,開(kāi)發(fā)人員只需要指定所需要的精度,定位內(nèi)核將會(huì)以最佳方式確定定位結(jié)果。

你一定感到很吃驚吧?!本文其余部分將向你展示這是如何做到的。

獲取位置坐標(biāo)

使用Xcode,創(chuàng)建一個(gè)新的基于視圖的應(yīng)用程序項(xiàng)目,取名為L(zhǎng)BS,在新項(xiàng)目中,雙擊LBSViewController.xib文件,在界面設(shè)計(jì)工具中編輯它。使用下面的組件填充視圖窗口,如圖1所示。

 Label

TextField

iPhone GPS定位系統(tǒng) 實(shí)例操作

圖 1 位置視圖實(shí)例:用Label和TextFiled填充這個(gè)窗口

在Xcode中框架組上點(diǎn)擊右鍵,選擇“添加”*“現(xiàn)有框架”,選擇 “Framework/CoreLocation.framework”,向LBSViewController.h文件中添加以下粗體字顯示的代碼:

  1.   #import   
  2.   #import   
  3.   @interface LBSViewController : UIViewController  
  4.    {  
  5.   IBOutlet UITextField *latitudeTextField;  
  6.     
  7.   IBOutlet UITextField *longitudeTextField;  
  8.   IBOutlet UITextField *accuracyTextField;  
  9.   CLLocationManager *lm;  
  10.   }  
  11.   @property (retain, nonatomic) UITextField *latitudeTextField;  
  12.   @property (retain, nonatomic) UITextField *longitudeTextField;  
  13.   @property (retain, nonatomic) UITextField *accuracyTextField;  
  14.   @end 

若要使用CLLocationManager類(lèi),需要在你的視圖控制器類(lèi)中實(shí)現(xiàn)CLLocationManagerDelegate協(xié)議,還需要?jiǎng)?chuàng)建三個(gè)出口用于連接視圖窗口中的三個(gè)TextFiled視圖。

回到界面編輯器,單擊并拖動(dòng)文檔的所有者項(xiàng)目到三個(gè)TextField視圖,然后分別選擇latitudeTextField,longitudeTextField和accuracyTextField。

在 LBSViewController.m文件中查詢(xún)以下代碼中的粗體部分:

  1.   #import "LBSViewController.h"  
  2.   @implementation LBSViewController  
  3.   @synthesize latitudeTextField, longitudeTextField, accuracyTextField;  
  4.   - (void) viewDidLoad {  
  5.   lm = [[CLLocationManager alloc] init];  
  6.   if ([lm locationServicesEnabled]) {  
  7.   lm.delegate = self;  
  8.   lm.desiredAccuracy = kCLLocationAccuracyBest;  
  9.   lm.distanceFilter = 1000.0f;  
  10.   [lm startUpdatingLocation];  
  11.   }  
  12.   }  
  13.   - (void) locationManager: (CLLocationManager *) manager  
  14.   didUpdateToLocation: (CLLocation *) newLocation  
  15.   fromLocation: (CLLocation *) oldLocation{  
  16.   NSString *lat = [[NSString alloc] initWithFormat:@"%g",  
  17.   newLocation.coordinate.latitude];  
  18.   latlatitudeTextField.text = lat;  
  19.   NSString *lng = [[NSString alloc] initWithFormat:@"%g",  
  20.   newLocation.coordinate.longitude];  
  21.   longitudeTextField.text = lng;  
  22.   NSString *acc = [[NSString alloc] initWithFormat:@"%g",  
  23.   newLocation.horizontalAccuracy];  
  24.   accaccuracyTextField.text = acc;  
  25.   [acc release];  
  26.   [lat release];  
  27.   [lng release]   
  28.   }  
  29.   - (void) locationManager: (CLLocationManager *) manager  
  30.   didFailWithError: (NSError *) error {  
  31.   NSString *msg = [[NSString alloc]  
  32.   initWithString:@"Error obtaining location"];  
  33.   UIAlertView *alert = [[UIAlertView alloc]  
  34.   initWithTitle:@"Error"  
  35.   message:msg  
  36.   delegate:nil  
  37.   cancelButtonTitle: @"Done"  
  38.   otherButtonTitles:nil];  
  39.   [alert show];  
  40.   [msg release];  
  41.   [alert release];  
  42.   }  
  43.   (void) dealloc{  
  44.  [lm release];  
  45.   [latitudeTextField release];  
  46.   [longitudeTextField release];  
  47.   [accuracyTextField release];  
  48.   [super dealloc];  
  49.   } 

前面的代碼創(chuàng)建了 CLLocationManager類(lèi)的一個(gè)實(shí)例,在使用對(duì)象之前,你應(yīng)該檢查用戶(hù)是否開(kāi)啟了設(shè)備的定位服務(wù),如果開(kāi)啟了,你可以使用 desiredAccuracy屬性指定想要的精度,使用下面的常量指定想要的精度:

  1.  kCLLocationAccuracyBest  
  2. kCLLocationAccuracyNearestTenMeters  
  3.  kCLLocationAccuracyHundredMeters  
  4.  kCLLocationAccuracyKilometer  
  5. kCLLocationAccuracyThreeKilometers 

distanceFilter屬性讓你指定設(shè)備必須移動(dòng)多少距離位置信息才會(huì)更新,這個(gè)屬性的單位是米。如果你想得到所有移動(dòng)的通知,可以使用kCLDistanceFilterNone常量,最后,使用 startUpdatingLocation方法啟動(dòng)位置管理器。

要獲得位置信息,需處理下面兩個(gè)事件:

  1. locationManager:didUpdateToLocation:fromLocation:  
  2. locationManager:didFailWithError: 

當(dāng)獲得一個(gè)新的定位值時(shí),設(shè)備觸發(fā) locationManager:didUpdateToLocation:fromLocation:事件,如果位置管理器不能確定位置信息,就會(huì)觸發(fā) locationManager:didFailWithError:事件。

當(dāng)設(shè)備可以確定位置時(shí),你可能想顯示經(jīng)緯度值和精度,這時(shí)你可以使用CLLocation對(duì)象,它的horizontalAccuracy屬性可以指定精度范圍,單位是米。

按Command- r在iPhone模擬器上測(cè)試該程序,圖2顯示了模擬器顯示的位置經(jīng)緯度值,同時(shí)顯示了精度。

iPhone GPS定位系統(tǒng) 實(shí)例操作

圖 2 定位測(cè)試:當(dāng)你在iPhone模擬器上測(cè)試該示例程序時(shí),總會(huì)顯示這些固定的值

顯示地圖

如果能將位置坐標(biāo)定位到地圖上顯示將會(huì)更有趣,幸運(yùn)的是,iPhone 3.0 SDK包括了Map Kit API,它可以讓你在程序中顯示Google Map,下面以一個(gè)例子進(jìn)行說(shuō)明。

還是使用前面創(chuàng)建的項(xiàng)目,在 LBSViewController.xib文件中視圖窗口上增加一個(gè)按鈕,如圖3所示。

iPhone GPS定位系統(tǒng) 實(shí)例操作

圖 3 View Map按鈕:增加按鈕后的樣子
  
在Xcode中框架組上點(diǎn)擊右鍵,增加一個(gè)新的框架MapKit.framework。在 LBSViewController.h文件中添加下列代碼中的粗體部分:

  1.   #import   
  2.   #import   
  3.   #import   
  4.   @interface LBSViewController : UIViewController  
  5.    {  
  6.   IBOutlet UITextField *accuracyTextField;  
  7.   IBOutlet UITextField *latitudeTextField;  
  8.   IBOutlet UITextField *longitudeTextField;  
  9.   CLLocationManager *lm;  
  10.   MKMapView *mapView;  
  11.   }  
  12.   @property (retain, nonatomic) UITextField *accuracyTextField;  
  13.   @property (retain, nonatomic) UITextField *latitudeTextField;  
  14.   @property (retain, nonatomic) UITextField *longitudeTextField;  
  15.   -(IBAction) btnViewMap: (id) sender;  
  16.   @end 

回到界面編輯器,拖動(dòng)按鈕到文件的所有者項(xiàng)目上,然后選擇btnViewMap:。

在LBSViewController.m文件中,添加下列代碼中的粗體部分:

  1.   -(IBAction) btnViewMap: (id) sender {  
  2.   [self.view addSubview:mapView];  
  3.   }  
  4.   (void) viewDidLoad {  
  5.   lm = [[CLLocationManager alloc] init];  
  6.   lm.delegate = self;  
  7.   lm.desiredAccuracy = kCLLocationAccuracyBest;  
  8.     
  9.   lm.distanceFilter = 1000.0f;  
  10.   [lm startUpdatingLocation];  
  11.   mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];  
  12.   mapView.mapType = MKMapTypeHybrid;  
  13.   }  
  14.   - (void) locationManager: (CLLocationManager *) manager  
  15.   didUpdateToLocation: (CLLocation *) newLocation  
  16.   fromLocation: (CLLocation *) oldLocation{  
  17.   NSString *lat = [[NSString alloc] initWithFormat:@"%g",  
  18.   newLocation.coordinate.latitude];  
  19.   latlatitudeTextField.text = lat;  
  20.   NSString *lng = [[NSString alloc] initWithFormat:@"%g",  
  21.   newLocation.coordinate.longitude];  
  22.   longitudeTextField.text = lng;  
  23.   NSString *acc = [[NSString alloc] initWithFormat:@"%g",  
  24.   newLocation.horizontalAccuracy];  
  25.   accaccuracyTextField.text = acc;  
  26.   [acc release];  
  27.   [lat release];  
  28.   [lng release];  
  29.   MKCoordinateSpan span;  
  30.   span.latitudeDelta=.005;  
  31.   span.longitudeDelta=.005;  
  32.   MKCoordinateRegion region;  
  33.   region.center = newLocation.coordinate;  
  34.   region.span=span;  
  35.   [mapView setRegion:region animated:TRUE];  
  36.   }  
  37.   - (void) dealloc{  
  38.   [mapView release];  
  39.   [lm release];  
  40.   [latitudeTextField release];  
  41.   [longitudeTextField release];  
  42.   [accuracyTextField release];  
  43.   [super dealloc];  
  44.   } 

代碼解釋?zhuān)?/p>

當(dāng)視圖載入時(shí)創(chuàng)建一個(gè)MKMapView類(lèi)的實(shí)例,設(shè)置顯示的地圖類(lèi)型。

當(dāng)用戶(hù)點(diǎn)擊View Map按鈕時(shí),在當(dāng)前視圖上增加mapView對(duì)象。

當(dāng)位置信息得到更新時(shí),使用mapView對(duì)象的setRegion:方法放大地圖。

在iPhone模擬器中按Command-r測(cè)試該程序,點(diǎn)擊View Map按鈕將會(huì)顯示一個(gè)包含位置管理器返回位置的地圖。如圖4所示。

iPhone GPS定位系統(tǒng) 實(shí)例操作

圖 4 地圖位置:通過(guò)定位內(nèi)核框架顯示地圖位置

因?yàn)槟M器始終顯示的是相同的位置,如果你有一部iPhone手機(jī)也可以真實(shí)地感受一下,當(dāng)你移動(dòng)位置時(shí),你會(huì)看到地圖會(huì)自動(dòng)更新。將 distanceFilter屬性設(shè)置得小一點(diǎn),這樣可以增強(qiáng)跟蹤體驗(yàn)。

正如你所看到的,iPhone SDK的定位內(nèi)核框架讓你可以很容易實(shí)現(xiàn)基于位置的設(shè)備,此外,MapKit(包括在iPhone SDK中)可以在地圖上顯示位置信息。如果沒(méi)有創(chuàng)建過(guò)基于位置的應(yīng)用,我想你看完本文就應(yīng)該動(dòng)手一試,還等什么呢? via:http://ming-fanglin.javaeye.com/blog/703744

小結(jié):iPhone GPS定位系統(tǒng) 實(shí)例操作的內(nèi)容介紹完了,希望本文對(duì)你有所幫助!更多相關(guān)內(nèi)容請(qǐng)參考編輯推薦!

本篇文章來(lái)源于 黑軟基地-中國(guó)最大的黑客軟件安全教程下載站!(手機(jī)資訊) 原文鏈接:http://www.hackvip.com/mobiwen/html/Mobile_220397_3.html

責(zé)任編輯:zhaolei 來(lái)源: 互聯(lián)網(wǎng)
相關(guān)推薦

2014-11-13 14:07:04

2011-08-08 15:56:18

iPhone 震動(dòng) NSUserDefa

2011-07-07 16:42:38

iPhone Sqlite3 數(shù)據(jù)庫(kù)

2011-07-06 16:25:10

iPhone 程序 調(diào)用

2021-01-01 19:02:08

GPS定位欺騙網(wǎng)絡(luò)安全

2011-07-19 10:42:41

iPhone 應(yīng)用程序 模型

2011-07-19 11:12:07

iPhone 控制器

2011-07-19 10:56:15

iPhone 控制器 視圖

2011-07-26 18:11:56

iPhone Sqlite 數(shù)據(jù)庫(kù)

2011-08-03 16:01:24

iPhone應(yīng)用開(kāi)發(fā) 自動(dòng)登陸

2011-05-27 15:56:30

Android

2011-07-06 16:55:56

iPhone php Push

2011-07-26 11:13:15

iPhone PXL

2011-08-10 11:12:33

iPhone文件

2011-05-24 09:23:00

中電信定位云

2011-07-25 18:02:51

iPhone LibFetion 移植

2011-07-18 17:52:47

Linux iPhone

2011-05-23 10:06:15

2009-06-20 13:37:18

iPhone OS 3iPhone OS

2010-04-20 17:07:57

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 久久精品手机视频 | 中文字幕高清av | 91久久精品一区二区二区 | 人人玩人人添人人澡欧美 | 欧美精品在线观看 | 欧美综合在线视频 | 精品国产一区二区三区久久久蜜月 | 国产精品久久久久久久久久不蜜臀 | 国产在线观看免费 | 午夜在线视频一区二区三区 | 91精品国产色综合久久 | 精品欧美一区免费观看α√ | 国产免费拔擦拔擦8x高清 | 欧美爱爱视频 | 国内自拍视频在线观看 | 欧美亚洲国产一区 | 美女视频一区二区三区 | 美日韩视频 | av影片在线 | 亚洲国产精品va在线看黑人 | 免费的色网站 | 国产黄色网 | 成人免费在线 | 国产精品久久久久久久7电影 | 亚洲一区二区三区在线视频 | 天堂亚洲| 日本三级电影在线看 | 欧美精品一区二区在线观看 | 99这里只有精品视频 | 91亚洲国产成人久久精品网站 | 国产九九九九 | 国产精品久久久久久久久久妇女 | 欧美888| 一级黄色毛片子 | 国内久久 | 日韩av一区二区在线观看 | 免费毛片网 | 超碰最新在线 | 一级免费毛片 | 久久69精品久久久久久久电影好 | 色吊丝2288sds中文字幕 |