標籤:蘋果地圖 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 調用蘋果地圖