標籤:des color 使用 資料 art for
#import "ViewController.h"
#import <MapKit/MapKit.h>
@interface ViewController () <CLLocationManagerDelegate,MKMapViewDelegate>
//這裡為什麼要把它設定成為屬性?為了就是解決強引用的問題,如果不設定,locationManger會在花括弧之後,就會釋放掉,這樣就看不到了我們想要的地址改變資訊,arc情況下,解決辦法只有把它設定為成員屬性
@property (retain,nonatomic) CLLocationManager *locationManger;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 建立顯示地圖的視圖MKMapView
MKMapView *map = [[MKMapView alloc]initWithFrame:self.view.bounds];
// 這裡打了一個tag,用於以後識別要顯示的地圖試圖
map.tag = 1000;
// 設定span
// span 指的是顯示地圖精確度,值越小顯示的地圖資料越精確,越詳細,如果值越大, 則顯示的地區越大
MKCoordinateSpan span ={0.1,0.1};
// 結構體的第二種賦值方法
// MKCoordinateSpan span2;
// span2.latitudeDelta = 100;
// span2.longitudeDelta = 100;
// 採用C函數來建立結構體的具體資料
// MKCoordinateSpan span3;
// span3 = MKCoordinateSpanMake(100, 100);
/*
*在OC環境下 對於結構體變數的賦值,有三種方式:
1?初始化的時候, 直接採用花括弧文法, 在花括裡,按結構體聲明的順序依次賦值
2?先聲明結構體變數,可以使用點文法,來指定相對應的成員變數的值
3?採用SDK的C 函數直接完成設定
*/
// 設定經緯度————是地圖當前顯示位置的地理座標
CLLocationCoordinate2D coordinate = {34.7568711,113.663221};
// 設定題圖上顯示的地區,這裡需要設定兩個參數————CLLocationCoordinate2D還有MKCoordinateSpan
MKCoordinateRegion region = {coordinate,span};
// 設定顯示介面的region就是上面的MKCoordinateRegion
map.region = region;
// 顯示當前位置
map.showsUserLocation =YES;
// 可放大縮小
map.zoomEnabled = YES;
// 可滑動
map.scrollEnabled = YES;
// 設定顯示的地圖的類型————————三種類型
// 顯示衛星地圖
// map.mapType = MKMapTypeSatellite;
// 顯示衛星地圖和標準地圖的混合
// map.mapType = MKMapTypeHybrid;
// 顯示標準也就是普通地圖,這是預設地圖類型
map.mapType = MKMapTypeStandard;
[self.view addSubview:map];
// 添加地圖的地址管理器————CLLocationManager
self.locationManger = [[CLLocationManager alloc] init];
// 當裝置移動每超過distanceFilter設定的米數時,就會更新一次位置資訊。
// 理解為地圖的位置移動一定距離,地圖的資訊就會更新
self.locationManger.distanceFilter = 100;
// 地圖位置資訊的精確度,這裡設定為best,精確度就是意味著在10米之內
// extern const CLLocationAccuracy kCLLocationAccuracyBestForNavigation;
// extern const CLLocationAccuracy kCLLocationAccuracyBest;
// extern const CLLocationAccuracy kCLLocationAccuracyNearestTenMeters;
// extern const CLLocationAccuracy kCLLocationAccuracyHundredMeters;
// extern const CLLocationAccuracy kCLLocationAccuracyKilometer;
// extern const CLLocationAccuracy kCLLocationAccuracyThreeKilometers;
self.locationManger.desiredAccuracy = kCLLocationAccuracyBest;
// 設定代理
self.locationManger.delegate = self;
// 這一句不能忘,只有設定了startUpdatingLocation,才能運行
[self.locationManger startUpdatingLocation];
// 註解類很重要,如果想自訂地圖上的一些文本和表徵圖,就必須用到——————註解
// 註解類,用於提供註解資料,真正用於顯示註解視圖,需要在委託方法去建立
MKPointAnnotation *pointAnnotation = [[MKPointAnnotation alloc]init];
pointAnnotation.title = @"河南省";
pointAnnotation.subtitle = @"鄭州市";
// 設定註解的位置,這裡就需要用到————coordinate
pointAnnotation.coordinate =CLLocationCoordinate2DMake(34.7568711,113.663221);
// 把註解加到地圖視圖上
[map addAnnotation:pointAnnotation];
}
#pragma mark -
#pragma mark CLLocationManagerDelegate
//第一個參數,表示是哪個地址管理器
//第二個參數,表示當位置更新的時候, 新位置資訊。
//第三個參數,表示位置更新時, 老的位置資訊。
// 這裡要做到的效果是——————做到位置的改變,地圖顯示隨著位置的改變,轉變視圖的介面
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(@"newLocation‘s lat is :%f, long is :%f",newLocation.coordinate.latitude,newLocation.coordinate.longitude);
NSLog(@"oldLocation‘s lat is :%f, long is :%f",oldLocation.coordinate.latitude,oldLocation.coordinate.longitude);
// 設定span
// span 指的是顯示地圖精確度,值越小顯示的地圖資料越精確,越詳細,如果值越大, 則顯示的地區越大
// 這裡調用了c語言的方法進行賦值------c賦值
MKCoordinateSpan span = MKCoordinateSpanMake(1, 1);
// 設定地圖上的顯示地區
MKCoordinateRegion region = {newLocation.coordinate,span};
// 這裡用tag,表示修改後現實的介面還是以前我們建立的map
// 另一種方法是把它設定成為成員屬性,也可以答到效果
MKMapView *map = (MKMapView *)[self.view viewWithTag:1000];
// 設定地圖介面的顯示地區,同時再加上一個動畫效果,
// 類似於map.region = region
[map setRegion:region animated:YES];
// 計算兩個位置之間的相隔距離, 單位是米----CLLocationDistance
CLLocationDistance distance = [newLocation distanceFromLocation:oldLocation];
NSLog(@"兩地之間的距離是:%f",distance/1000);
// 調用此方法, 是終止位置管理器更新位置資訊,在實際應用中, 一定調用這個方法, 解決裝置耗電量問題。
// [self.locationMgr stopUpdatingLocation];
}
#if 0
// 這個和上面的方法其實是一個方法,6.0以後,系統推薦使用這種方法,同時存在,上面的方法無效
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
// 列印當面方法
NSLog(@"%@",NSStringFromSelector(_cmd));
NSLog(@"%@",NSStringFromSelector(@selector(locationManager:didUpdateLocations:)));
// 數組locations的最後一個元素, 是最新的位置資訊
// 通過地址管理器的地址數組,取最新地址
CLLocation *newLocation = locations[0];
// 設定span
MKCoordinateSpan span = MKCoordinateSpanMake(1, 1);
// 設定region
MKCoordinateRegion region = MKCoordinateRegionMake(newLocation.coordinate, span);
// 這裡用tag,表示修改後現實的介面還是以前我們建立的map
// 另一種方法是把它設定成為成員屬性,也可以答到效果
MKMapView *map = (MKMapView *)[self.view viewWithTag:1000];
// 設定地圖介面的顯示地區,同時再加上一個動畫效果,
// 類似於map.region = region
[map setRegion:region animated:YES];
}
#endif
-(void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(@"%s",__func__);
NSLog(@"%@",error);
}
#pragma mark -
#pragma mark MKMapViewDelegate
// 這裡是實現註解的具體的代理方法
//這裡有兩個參數————————MKMapView還有——————————annotation
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
// 如果是系統預設的使用者當前地址,返回預設的註解,
if ([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
// 註解建立的方法和tabelview很像
// 都是先建立一個標識
static NSString *pinViewIndentifier [email protected]"pinViewIndentifier";
// 從可重用隊列裡取出來
MKPinAnnotationView *pinView =(MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:pinViewIndentifier];
// 如果為空白,建立pinview;
if (pinView == nil) {
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinViewIndentifier];
}
// 修改圖釘的顯示顏色
pinView.pinColor =MKPinAnnotationColorGreen;
// 讓圖釘出現的時候, 展現從天而降的效果
pinView.animatesDrop =YES;
// 如果想要展示上述效果, 需要設定canShowCallout為yes
pinView.canShowCallout =YES;
return pinView;
}
@end