iOS地圖開發MapKit

來源:互聯網
上載者:User

標籤:style   blog   color   使用   os   strong   檔案   io   

現在O2O應用非常火,因為基於地理的社交和電子商務應用都有非常廣闊的前景

二O2O的移動載體就是手機了(平板基本忽略不計),所以會點手機上Map開發還是不錯的

首先蘋果已經封裝了一套地圖架構供我們使用,首先要使用蘋果提供的地圖架構需要匯入MapKit架構

匯入完成後即可使用

 

首先在你的視圖裡拖入一個MapView,

然後運行就行了~這是你就會看到地圖(如果用的是真機最好,模擬器有點蛋疼)

 

但是現實的是一個大大的地圖,趕腳沒什麼用,我們需要的是一個比較精準的地圖,這時進入MKMapView.h檔案中去看看如何使用這個視圖

首先看到的是一個代理~這就說明mapView通知外界訊息的方式之一用了代理,點入代理會看到很多方法,具體這些方法有什麼用自己去探索吧~

然後看到的是一個mapType,這時一個枚舉變數,可以看到地圖顯示的模式有三種,一種是標準,一種是衛星,還有一種是混合的,分別設定然後運行一下就知道他們的區別了

再往下看是一個region屬性,下面是一個setRegion方法,點option看一下協助貌似是我們想要的,那個可以追蹤位置的屬性

我們嘗試著設定一下,但是貌似我們不知道自己所在位置的經緯度,這時候看看代理裡面有個叫update的方法,應該是定位使用者以後調用的,此時應該已經有經緯度了

嘗試著用一下啊

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{    NSLog(@"%f %f", userLocation.coordinate.latitude, userLocation.coordinate.longitude);        // 顯示使用者周邊資訊    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 100, 100);    [mapView setRegion:region animated:YES];}

設定完代理,然後實現這個方法以後,果然會定位到當前所在的位置了,上面的結構體都不難,自己查下標頭檔就好了,其實一個是經緯度,一個是顯示比例

 

有時候我們還需要顯示周邊的位置資訊,並且有醒目的表徵圖,這時候就要用到MKAnnotation協議了,這個協議不是之前常用的代理協議,這種協議是另一種協議

這種協議需要我們自訂類去遵守,並且包含裡面有的屬性等

/** 在協議中定義屬性的用處  可以保證所有遵守該協議的對象,都具有協議定義的屬性  使用帶屬性的協議,通常可以將協議中定義的屬性,直接複製到新類中,取消屬性的Readonly描述符即可  協議只有.h,也就是只有定義,沒有實現。 */@interface MyAnnotation : NSObject <MKAnnotation>@property (nonatomic, assign) CLLocationCoordinate2D coordinate;@property (nonatomic, copy) NSString *title;@property (nonatomic, copy) NSString *subtitle;@property (nonatomic, strong) NSString *imageName;@end

上面就是一個自訂類,實現協議

 

如何在地圖上添加自訂的點呢?添加自頂一個點其實就是添加一個經緯度,下面的代碼就是在mapVIew上添加兩個自訂的點

  // 添加圖釘    MyAnnotation *annotation1 = [[MyAnnotation alloc] init];    annotation1.coordinate = CLLocationCoordinate2DMake(30, 116);    annotation1.title = @"我的地盤";    annotation1.subtitle = @"哈哈哈";    annotation1.imageName = @"head0";        [_mapView addAnnotation:annotation1];        MyAnnotation *annotation2 = [[MyAnnotation alloc] init];    annotation2.coordinate = CLLocationCoordinate2DMake(30, 106);    annotation2.title = @"重慶";    annotation2.subtitle = @"哈哈哈";    annotation2.imageName = @"head0";        [_mapView addAnnotation:annotation2];

這樣在運行一下就加上去了,有時候我們需要自己定義表徵圖,而不用系統內建的那個紅頭圖釘

 

這個自訂方法應該會在mapView的代理裡面,所以我們去代理裡面找找,看有沒有返回UIView或其子類的方法,

然後就找到了~

下面的代碼就是用返回自頂一個視圖,代替系統提供的

#pragma mark 自訂圖釘視圖- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{    // 判斷annotation參數是否是MyAnnotation    // 如果不是MyAnnotaion說明是系統的圖釘,無需做處理    if (![annotation isKindOfClass:[MyAnnotation class]]) {        // 使用系統預設的圖釘        return nil;    }        // 可重用標示符    static NSString *ID = @"MyAnno";        // 查詢可重用的圖釘    MKAnnotationView *annoView = [mapView dequeueReusableAnnotationViewWithIdentifier:ID];        // 如果沒有找到,再去執行個體化圖釘    if (annoView == nil) {        annoView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ID];                // 自訂圖釘視圖,如果要接受使用者響應,需要設定此屬性        annoView.canShowCallout = YES;    }        // 設定圖釘    annoView.annotation = annotation;    // 轉換成MyAnnotation    // 設定圖釘視圖的映像    MyAnnotation *anno = annotation;    annoView.image = [UIImage imageNamed:anno.imageName];        NSLog(@"自訂圖釘");        return annoView;}

之所以要判斷類型是因為系統的Annotation協議對象沒有image屬性,如果不判斷會崩潰掉

另外我們還經常用到就是計算兩點之間的距離,這種需求蘋果也想到了.所以這種方法蘋果已經封裝好了,下面就是實現代碼

// 根據經緯度計算兩點間的距離    CLLocation *location1 = [[CLLocation alloc] initWithLatitude:30 longitude:116];    CLLocation *location2 = [[CLLocation alloc] initWithLatitude:30 longitude:106];        CLLocationDistance distance = [location1 distanceFromLocation:location2];        NSLog(@"距離:%f", distance);

怎麼樣?好用吧,不過一般商業級應用都是使用高德和百度地圖的SDK~所以真想學點東西的話,出了要瞭解蘋果這一套架構還要去研究研究百度和高德的SDK

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.