努力學習,當然也不能忘記總結。趁着休息的時間總結一下關於Core Location與Map Kit的一些值得注意的知識點:
當然我們知道如何在模擬器上顯示地圖:
// 添加MapKit框架
MKMapView *mapView = [[MKMapView alloc]initWithFrame:self.view.bounds];
// 設置地圖的顯示類型,標準、衛星、混合,這裏定義爲混合型的
mapView.mapType = MKMapTypeHybrid;
// 添加地圖
[self.view addSubview:mapView];
而在設計地圖的時候能夠顯示還不夠,主要的是如何配置地圖的一些屬性。
// 定義地圖是否可以放大、縮小,Default is YES
mapView.zoomEnabled = YES;
// 定義地圖是否可以滑動,Default is YES
mapView.scrollEnabled = YES;
// 定義地圖是否可以旋轉, default YES on Mac OS X and on iOS 7.0 and later.
mapView.rotateEnabled = YES;
// 定義地圖相機的俯仰信息是否被使用 Default same to rotateEnable
mapView.pitchEnabled = YES;
// 定義地圖是否顯示用戶當前位置,Default is NO
mapView.showsUserLocation = YES;
地圖顯示用戶位置的時候,在iOS早期版本用的是大頭針形狀的,現在改成了一個藍色點。當然地圖的屬性也遠不止這些,在而這些只是常用的幾個,想要掌握它們需要我們都一一嘗試一下還是比較好的.
另外我們也可以自己定義地圖的鎖定座標,在一個地區的地理座標位置的時候,(緯度,經度)北緯是正數,東經是正數,這裏比較容易出錯的是,定義coordinate時不能直接賦值而是需要:
CLLocationCoordinate2D coordinate;
coordinate.latitude = 34.7568711;
coordinate.longitude = 113.663221;</span>
錯誤的賦值格式是:CLLocationCoordinate2D coordinate = {34.7568711,113.663221}
接着設置顯示區域的精度,值越小表示的範圍越精確,值越大表示的範圍越大,但是不精確
MKCoordinateSpan span = {100,100};
根據地理座標和span創建表示區域的值。(通常一個圓,和半徑)可以使用四種方法:
1、 MKCoordinateRegion region = {coordinate,span}; //初始化方法
2、 MKCoordinateRegion region;
region = {coordinate,span};
3、 region = MKCoordinateRegionMake(coordinate, span);
4、 region = MKCoordinateRegionMakeWithDistance(coordinate, 111*1000*span.latitudeDelta, 111*1000*span.longitudeDelta);
最後將顯示的區域告訴地圖: [mapView setRegion:region];
其三,對地圖進行位置管理:
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;//當讓這裏樣遵守CLLocationManagerDelegate協議
locationManager.distanceFilter = kCLDistanceFilterNone;//指定以米爲單位的最小更新距離。客戶端將不會收到通知如果小於規定值的動作,除非精度有所提高。通過在kCLDistanceFilterNone,是獲得所有運動的通知。默認情況下, 使用kCLDistanceFilterNone。
// 設置位置管理的精度
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
這裏需要注意的是:對於位置管理來說, 並非是精度越大越好。因爲GPS模塊,是手機最耗電的模塊,所以爲最大限度讓我們應用程序省電,需要根據具體的應用來設定具體的精度。
最後開始更新位置顯示:
[locationManager startUpdatingLocation];
而在CLLocationManager的delegate方法中,我們經常用到的當然是更新位置的方法,這裏提供了兩個方法:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(@"%@",NSStringFromSelector(_cmd));
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
NSLog(@"%s",__func__);
}
對於這兩種方法看過蘋果官方的解釋會知道前一個方法是不推薦的用的,主推後一個方法:
* locationManager:didUpdateToLocation:fromLocation:
*
* Discussion:
* Invoked when a new location is available. oldLocation may be nil if there is no previous location
* available.
*
* This method is deprecated. If locationManager:didUpdateLocations: is
* implemented, this method will not be called.
意思是在使用更新位置方法的時候,如果調用了後一個方法,前一個方法是不會被調用的。所以我們一般使用後一個方法。
最後,我們在設計地圖的時候,有時候會遇到顯示大頭針界面的需求,以及地圖的註解等等。而這些知識會在下一次與大家繼續探討。