iOS定位服务与地图开发(5)---使用程序外地图之调用iOS 6苹果地图
一般,在应用程序中调用程序外地图有2个选择:iOS 6苹果地图和谷歌web地图。
1、调用iOS 6苹果地图:
iOS 设备中带有一个地图应用。
在iOS 6之后,可以在自己的应用程序中调用它,并且可以传递一些参数进行初始化显示。
实现Demo:
iOS 6中实现这个功能需要使用Map Kit中的MKPlacemark和MKMapItem两个类。
- (IBAction)geocodeQuery:(id)sender { // 从界面文本框获取查询地址字符串 if (_txtQueryKey.text == nil || [_txtQueryKey.text length] == 0) { return ; } CLGeocoder *geocoder = [[CLGeocoder alloc] init]; [geocoder geocodeAddressString:_txtQueryKey.text completionHandler:^(NSArray *placemarks, NSError *error) { NSLog(@"查询记录数:%i",[placemarks count]); if ([placemarks count] > 0 ) { CLPlacemark * placemark = placemarks[0]; CLLocationCoordinate2D coordinate = placemark.location.coordinate; NSDictionary *address = placemark.addressDictionary; // MKPlacemark是地图上的地标类,CLPlacemark是定位使用的地标类 MKPlacemark *place = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:address]; // MKMapItem类封装了地图上一个点得信息类 MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:place]; // 调用 iOS 自带的苹果地图应用,是MKMapItem类的实例方法 // 参数:NSDictionary类型,包含一些键: /* MKLaunchOptionsDirectionsModeKey 设定路线模式,值对应2个:驾车路线和步行路线 MKLaunchOptionsShowsTrafficKey 设置显示交通状况 MKLaunchOptionsMapSpanKey 设置地图跨度 MKLaunchOptionsMapCenterKey 设置地图中心点 MKLaunchOptionsMapTypeKey 设置地图类型 */ [mapItem openInMapsWithLaunchOptions:nil]; // 关闭键盘 [_txtQueryKey resignFirstResponder]; } }]; }
效果图:
程序启动后进入页面(起始地点预设为香港) 输入目的地深圳市(点击“查询”按钮) 调转到苹果内置地图
- (IBAction)geocodeQuery:(id)sender { // 从界面文本框获取查询地址字符串 if (_txtQueryKey.text == nil || [_txtQueryKey.text length] == 0) { return ; } ....... // 调用 iOS 自带的苹果地图应用,是MKMapItem类的实例方法 // 参数:NSDictionary类型,包含一些键: /* MKLaunchOptionsDirectionsModeKey 设定路线模式,值对应2个:驾车路线和步行路线 MKLaunchOptionsShowsTrafficKey 设置显示交通状况 MKLaunchOptionsMapSpanKey 设置地图跨度 MKLaunchOptionsMapCenterKey 设置地图中心点 MKLaunchOptionsMapTypeKey 设置地图类型 */ NSDictionary *options = [[NSDictionary alloc] initWithObjectsAndKeys:MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsDirectionsModeKey, nil]; [mapItem openInMapsWithLaunchOptions:options]; // 关闭键盘 [_txtQueryKey resignFirstResponder]; } }]; }
如果有多个点需要标注,我们可以使用MKMapItem的类方法:
+ (BOOL)openMapsWithItems:(NSArray *)mapItems launchOptions:(NSDictionary *)launchOptions
其中mapItmes是标注点的集合
launchOptions是启动参数
- (IBAction)geocodeQuery:(id)sender { // 从界面文本框获取查询地址字符串 if (_txtQueryKey.text == nil || [_txtQueryKey.text length] == 0) { return ; } CLGeocoder *geocoder = [[CLGeocoder alloc] init]; [geocoder geocodeAddressString:_txtQueryKey.text completionHandler:^(NSArray *placemarks, NSError *error) { NSLog(@"查询记录数:%i",[placemarks count]); NSMutableArray *array = [NSMutableArray array]; for (int i = 0; i < [placemarks count]; i++) { CLPlacemark * placemark = placemarks[i]; CLLocationCoordinate2D coordinate = placemark.location.coordinate; NSDictionary *address = placemark.addressDictionary; // MKPlacemark是地图上的地标类,CLPlacemark是定位使用的地标类 MKPlacemark *place = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:address]; // MKMapItem类封装了地图上一个点得信息类 MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:place]; [array addObject:mapItem]; // 关闭键盘 [_txtQueryKey resignFirstResponder]; } NSDictionary *options = [[NSDictionary alloc] initWithObjectsAndKeys:MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsDirectionsModeKey, nil]; if ([array count] > 0) { [MKMapItem openMapsWithItems:array launchOptions:options]; } }]; }