IOS學習之——地圖3 調用蘋果地圖

來源:互聯網
上載者:User

標籤:蘋果地圖   ios   手機開發   

歡迎轉載,轉載請註明出處

本文地址:http://blog.csdn.net/zhenggaoxing/article/details/42710853

綜述:
這裡的蘋果地圖調用是指從咱們的軟體跳轉到蘋果地圖,我們要做的就是把資料設定弄好,在使用蘋果地圖就可以了
蘋果地圖上展示一個點
添加相關的framework
我們要再蘋果地圖上展示一個點,需要什麼呢?1需要位置,2需要位置相關的資訊 。有這兩個資訊就可以了
    CLGeocoder *geocoder=[CLGeocoder new];    // 使用地理資訊反編碼,來擷取位置和資訊    [geocoder geocodeAddressString:textQueryTextField.text completionHandler:^(NSArray *placemarks, NSError *error) {        NSLog(@"查詢記錄數目:%i",(int)[placemarks count]);        if ([placemarks count]>0) {            CLPlacemark *placemark=placemarks[0];                        CLLocationCoordinate2D coordinate=placemark.location.coordinate;            NSDictionary *address=placemark.addressDictionary;                        // MKPlacemark 是地圖相關資訊的類            MKPlacemark *place=[[MKPlacemark alloc]initWithCoordinate:coordinate/*位置*/ addressDictionary:address/*相關的資訊*/];                        // MKMapItem 類封裝了有關地圖上點的資訊            MKMapItem *mapItem=[[MKMapItem alloc]initWithPlacemark:place];            // 調用蘋果地圖的方法            [mapItem openInMapsWithLaunchOptions:nil];                        [textQueryTextField resignFirstResponder];        }    }];
要解釋一下:1擷取了位置資訊(CLPlacemark) 2 把位置和資訊傳給地圖(MKPlacemark)3 調用蘋果地圖(openInMapsWithLaunchOptions 方法)
效果:

展示路線
這裡展示的路線是從手機定位位置,到你想去位置的路線,展示路線比展示一個點要多一個要設定的:什麼方式去那個點
            //地圖上設定行車路線            NSDictionary* options =[[NSDictionary alloc]initWithObjectsAndKeys:                                    MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsDirectionsModeKey, nil];                        MKMapItem *mapItem = [[MKMapItem alloc]initWithPlacemark:place];            [mapItem openInMapsWithLaunchOptions:options];
這裡解釋一下:
怎麼設定呢:用NSDictionary 類型的參數設定 ,

設定行車路線:
MKLaunchOptionsDirectionsModeDriving 有兩個鍵值:

MKLaunchOptionsDirectionsModeDriving——駕車

MKLaunchOptionsDirectionsModeWalking——騎車


還有幾種設定的key:

MKLaunchOptionsMapTypeKey:設定地圖類型

MKLaunchOptionsShowsTrafficKey:設定交通情況

MKLaunchOptionsMapCenterKey:設定地圖中心

MKLaunchOptionsMapSpanKey:設定地圖跨度

看看效果


蘋果地圖上展示多個點
展示多個點,使用openMapsWithItems 方法實現:載入數組便可
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];    [geocoder geocodeAddressString:self.textTextField.text completionHandler:^(NSArray                                                                         *placemarks, NSError *error) {        NSLog(@"查詢記錄數:%i",(int)[placemarks count]);                NSMutableArray* array = [NSMutableArray new];                for (int i = 0; i < [placemarks count]; i++) {                        CLPlacemark* placemark = placemarks[i];                        CLLocationCoordinate2D coordinate = placemark.location.coordinate;            NSDictionary* address = placemark.addressDictionary;                        MKPlacemark *place = [[MKPlacemark alloc]                                  initWithCoordinate:coordinate addressDictionary:address];                        MKMapItem *mapItem = [[MKMapItem alloc]initWithPlacemark:place];                        [array addObject:mapItem];        }                //關閉鍵盤        [self.textTextField resignFirstResponder];                if ([array count] > 0) {            [MKMapItem openMapsWithItems:array launchOptions:nil];        }    }];
看看效果



原始碼:

https://git.oschina.net/zhengaoxing/No15.3appleMap/tree/master






















IOS學習之——地圖3 調用蘋果地圖

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.