你好,歡迎來到IOS教程網

 Ios教程網 >> IOS訊息 >> 關於IOS >> IOS之地圖MapKit

IOS之地圖MapKit

編輯:關於IOS

iOS地圖位置開發

iPhone SDK提供了三個類來管理位置信息:CLLocation CLLocationManager 和 CLLHeading(不常用)。除了使用GPS來獲取當前的位置信息外,iPhone也可以基於WiFi基站和無線發射塔來獲得位置信息。GPS的精度最高,可以精確到米級別,但是也最耗電。

------------CLLocation
CLLocation類代表一個位置信息,其中還包括了方向和速度。比如我在長安街188號以5公裡/小時的速度往西走。CLLocation具有下面的屬性和方法:
@property CLLocationCoordinate2D coordinate; //以經度和緯度表示的位置信息
@property CLLocationDistance altitude; //海拔
@property CLLocationAccuracy horizontalAccuracy; //水平精度(如:精確到米)
@property CLLocationAccuracy verticalAccuracy; //垂直精度
@property CLLocationDirection course; //方向
@property CLLocationSpeed speed; //速度
-(NSDate *)timeStamp;
//兩個位置之間的距離
-(CLLocationDistance)distanceFromLocation:(CLLocation *)location;

-------------CLLocationManager
CLLocationManager類管理和提供位置服務。它的屬性和方法有:
@property CLLocation *location; //位置
@property id delegate;
@property CLLocationDistance distanceFilter; //距離過濾,比如:500以內
@property CLlocationAccuracy verticalAccuracy; //垂直精度
-(void) startUpdatingLocation; //開始更新位置(比如:你在往某個地方走)
-(void)stopUpdatingLocation; //停止更新位置
-(void)startUpdatingHeading; //開始更新方向(比如:你改往東走)
-(void)stopUpdatingHeading; //停止更新方向
CLLocationManagerDelegate是一個委托類。你的應用程序需要使用這個委托類。當用戶改變位置的時候,CLLocationManager回調的方法是:
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation;
當用戶改變方向的時候,所調用的方法是:
-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLLHeading *)newHeading;
當iPhone無法獲得當前位置的信息時,所回調的方法是:
-(void)locationManager: (CLLocationManager *)manager didFailLoadWithError:(NSError *)error;

=========================================================================
下面我們來看一個位置類的基本步驟:
一、啟動定位服務
CLLocationManager *locManager = [[CLLocationManager alloc] init];
locManager.delegate = self;
[locManager startUpdatingLocation];
二、獲得位置信息
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation fromLocation: (CLLocation *)oldLocation
{
NSTimeInterval howRecent = [newLocation.timestamp timeIntervalSinceNow];
if(howRecent < -10) return ; //離上次更新的時間少於10秒
if(newLocation.horizontalAccuracy > 100) return; //精度> 100米
//經度和緯度
double lat = newLocation.coordinate.latitude;
double lon = newLocation.coordinate.longitude;
}
三、獲得方向信息(比如往南走)
-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
//獲得方向
CLLocationDirection heading = newHeading .trueHeading;
}
四、停止定位
[locManager stopUpdatingLocation];
你可以設置你想要的精度和距離過濾:
locManager.desiredAccuracy = kLLocationAccuracyBest;
locManager.distanceFilter = 1000;

MapKit框架:
主要提供了四個功能:顯示地圖、CLLocation和地址之間的轉換、支持在地圖上做標記(比如標記北京天安門廣場)、把一個位置解析成地址(比如我在水立方,想要知道確切的地址信息)。
MKMapView類:主要是完成下述功能:
-------顯示地圖,比如:顯示北京市的地圖;
-------提供多種顯示方式,比如標准地圖格式,衛星地圖等;
-------支持地圖的放大縮小;
-------支持在地圖上做標記,比如標記天安門廣場;
-------在地圖上顯示手機所在的當前位置。
MKMapView類的屬性有:
@property MKCoordinateRegin region; //地圖所顯示的區域
@property CLLocationCoordinate2D centerCoordinate; //經度和緯度確定的中心位置
@property MKMapView mapType; //地圖的顯示類型,如:衛星地圖
@property NSArray *annotations; //地圖上的標記
@property MKUserLocation userLocation; //用戶位置
@property id delegate; //委托類

裝載地圖時的回調方法有:
-(void)mapViewWillStartLocationMap:(MKMapView *) mapView; //開始裝載地圖
-(void)mapViewDidFinishLocationMap:(MKMapView *)mapView; //結束裝載地圖
-(void)mapVewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error; //裝載失敗

當位置發生轉變時的回調方法:
-(void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated; //將要更改
-(void)mapView: (MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated; //已經更改

MKPlacemark、MKUserLocation和MKReverseGeocoder
在地圖上做標記是通過MKPlacemark類來完成的。這個類使用(符合)MKAnnotation協議。MKAnnotation包含了多個屬性,如:位置(經緯度,CLLocationCoordinate2D類型)、文字標記信息(NSString類型)等。
MKPlacemark保存了位置(經緯度)和地址(字典類)之間的映射。下面是它的初始化方法:
-(void)initWithCoordinate:(CLLocationCoordinate2D *)coordinate addressDictionary:(NSDictionary *)dictionary;
MKUserLocation就是指手機的當前位置,它是MKAnnotation的一個特別案例(因為MKAnnotation可以是地圖上的任何標記,而MKUserLocation只是標記了地圖上手機所在的當前位置)。這個類包含了多個屬性:手機的位置(類型為CLLocation)、位置文字信息(類型為NSString)等。
MKPlacemark保存了位置(經緯度)和地址之間的映射。那麼,有沒有工具在這兩者之間做轉換呢?這就是MKRecerseGeocoder.給定一個位置信息,這個類可以返回相應的地址信息。MKReverseGeocoder的初始化方法為:
-(void)initWithCoodinate:(CLLocationCoordinate2D)coordinate;
下面是MKReverseGeocoder常用的一些屬性和方法:
@property id delegate; //委托
-(void)start; //開始轉換
-(void)cancel; //取消轉換

回調的方法有:
-(void)reverseGeocoder:(MKReverseGeocoder *) geocoded didFindPlacemark:(MKPlacemark *)placemark; //轉換成功
-(void)reverseGeocoder : (MKReverseGeocoder *)geocoded didFailWithError:(NSError *)error; //轉換失敗

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved