1.调用IOS6苹果地图
IOS6中实现这个功能需要使用Map Kit中的MKPlaceMark和MKMapItem两个类,因此我们需要在工程中添加MapKit.framework主要代码如下:
1 - (IBAction)geocodeQuery:(id)sender { 2 3 if(_txtQueryKey.text == nil || [_txtQueryKey.text length] == 0) 4 { 5 return; 6 } 7 8 CLGeocoder *geocoder = [[CLGeocoder alloc]init]; 9 [geocoder geocodeAddressString:_txtQueryKey.text completionHandler:^(NSArray *placemarks, NSError *error) { 10 11 NSLog(@"查询记录数:%i",[placemarks count]); 12 13 if([placemarks count]>0) 14 { 15 CLPlacemark *placemark = placemarks[0]; 16 17 CLLocationCoordinate2D coordinat = placemark.location.coordinate; 18 NSDictionary *address = placemark.addressDictionary; 19 20 MKPlacemark *place = [[MKPlacemark alloc]initWithCoordinate:coordinat addressDictionary:address]; 21 22 MKMapItem *mapItem = [[MKMapItem alloc]initWithPlacemark:place]; 23 24 [mapItem openInMapsWithLaunchOptions:nil]; 25 26 //关闭键盘 27 [_txtQueryKey resignFirstResponder]; 28 } 29 }]; 30 }
在调用IOS自带的苹果地图应用,openInMapsWithLaunchOptions:方法时MKMapItem类的实例方法,参数时NSDictionary类型,这个参数可以控制地图初始化信息,它包含一些键:
(1)MKlaunchOptionsDirectionsModeKey 设定路线模式,他有两个值MKLaunchOptionsDirectionModeDriving(驾车路线)和 MKLaunchOptionsDirectionsModeWalking(步行路线).
(2)MKLaunchOptionsMapTypeKey 设定地图类型。
(3)MKLaunchOptionsMapCenterKey设定地图中心点。
(4)MKLaunchOptionsMapSpanKey设置地图跨度。
(5)MKLaunchOptionsShowsTrafficKey设置交通状况。
例如,可以使用下面的代码在地图上设置行车路线:
NSDictionary *options=[[NSDictionary alloc]initwithObjectsAndKeys:MKLaunchOptionsDirectionsModeDriving,MkLaunchOptionsDirectionsModeKey,nil];
MKMapItem *mapItem = [[MKMapItem alloc] initwithPlacemark:place];
[mapItem openInMapsWithLaunchOptions:options];
设置行车路线后,地图上标注出路线,默认情况下起点时当前位置,这个位置是通过定位服务获得的。
如果有多个点需要标注,我们可以使用MKMapItem的类方法:
+(BOOL)openMapsWithItems:(NSArray *)mapItems launchOptions:(NSDictionary *)launchOptions
其中参数mapItems是标注点的集合,launchOptions是启动参数。如果想使用这个方法,则上面的例子可以修改如下:
1 - (IBAction)geocodeQuery:(id)sender { 2 3 if(_txtQueryKey.text == nil || [_txtQueryKey.text length] == 0) 4 { 5 return; 6 } 7 8 CLGeocoder *geocoder = [[CLGeocoder alloc]init]; 9 [geocoder geocodeAddressString:_txtQueryKey.text completionHandler:^(NSArray *placemarks, NSError *error) { 10 11 NSMutableArray *array = [NSMutableArray new]; 12 13 for (int i=0; i<[placemarks count]; i++) { 14 CLPlacemark *placemark = placemarks[i]; 15 16 CLLocationCoordinate2D coordinat = placemark.location.coordinate; 17 NSDictionary *address = placemark.addressDictionary; 18 19 MKPlacemark *place = [[MKPlacemark alloc]initWithCoordinate:coordinat addressDictionary:address]; 20 21 MKMapItem *mapItem = [[MKMapItem alloc]initWithPlacemark:place]; 22 23 [array addObject:mapItem]; 24 //关闭键盘 25 [_txtQueryKey resignFirstResponder]; 26 } 27 28 if([array count]>0) 29 { 30 [MKMapItem openMapsWithItems:array launchOptions:nil]; 31 } 32 }]; 33 }
2.调用谷歌web地图
也可以借助谷歌的web地图API进行开发地图应用程序,但这里涉及的技术都是web技术了,而非本地技术,谷歌提供的地图查询url形式如下:
1 - (IBAction)geocodeQuery:(id)sender { 2 3 if(_txtQueryKay.text == nil || [_txtQueryKay.text length] == 0) 4 { 5 return; 6 } 7 8 CLGeocoder *geocoder = [[CLGeocoder alloc]init]; 9 [geocoder geocodeAddressString:_txtQueryKay.text completionHandler:^(NSArray *placemarks, NSError *error) { 10 NSLog(@"查询记录数:%i",[placemarks count]); 11 12 if([placemarks count]>0) 13 { 14 CLPlacemark *placemark = placemarks[0]; 15 16 CLLocationCoordinate2D coordinate = placemark.location.coordinate; 17 18 NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/map?q=%f, %f",coordinate.latitude,coordinate.longitude]; 19 20 NSURL *url= [NSURL URLWithString:urlString]; 21 22 [[UIApplication sharedApplication] openURL:url]; 23 24 //关闭键盘 25 [_txtQueryKay resignFirstResponder]; 26 } 27 }]; 28 }