IOS 使用程序外地图(IOS Map and google Map)

时间:2023-03-08 22:58:13
IOS 使用程序外地图(IOS Map and google Map)

1.调用IOS6苹果地图

IOS6中实现这个功能需要使用Map Kit中的MKPlaceMark和MKMapItem两个类,因此我们需要在工程中添加MapKit.framework主要代码如下:

 - (IBAction)geocodeQuery:(id)sender {

     if(_txtQueryKey.text == nil || [_txtQueryKey.text length] == )
{
return;
} CLGeocoder *geocoder = [[CLGeocoder alloc]init];
[geocoder geocodeAddressString:_txtQueryKey.text completionHandler:^(NSArray *placemarks, NSError *error) { NSLog(@"查询记录数:%i",[placemarks count]); if([placemarks count]>)
{
CLPlacemark *placemark = placemarks[]; CLLocationCoordinate2D coordinat = placemark.location.coordinate;
NSDictionary *address = placemark.addressDictionary; MKPlacemark *place = [[MKPlacemark alloc]initWithCoordinate:coordinat addressDictionary:address]; MKMapItem *mapItem = [[MKMapItem alloc]initWithPlacemark:place]; [mapItem openInMapsWithLaunchOptions:nil]; //关闭键盘
[_txtQueryKey resignFirstResponder];
}
}];
}

在调用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是启动参数。如果想使用这个方法,则上面的例子可以修改如下:

 - (IBAction)geocodeQuery:(id)sender {

     if(_txtQueryKey.text == nil || [_txtQueryKey.text length] == )
{
return;
} CLGeocoder *geocoder = [[CLGeocoder alloc]init];
[geocoder geocodeAddressString:_txtQueryKey.text completionHandler:^(NSArray *placemarks, NSError *error) { NSMutableArray *array = [NSMutableArray new]; for (int i=; i<[placemarks count]; i++) {
CLPlacemark *placemark = placemarks[i]; CLLocationCoordinate2D coordinat = placemark.location.coordinate;
NSDictionary *address = placemark.addressDictionary; MKPlacemark *place = [[MKPlacemark alloc]initWithCoordinate:coordinat addressDictionary:address]; MKMapItem *mapItem = [[MKMapItem alloc]initWithPlacemark:place]; [array addObject:mapItem];
//关闭键盘
[_txtQueryKey resignFirstResponder];
} if([array count]>)
{
[MKMapItem openMapsWithItems:array launchOptions:nil];
}
}];
}

2.调用谷歌web地图

也可以借助谷歌的web地图API进行开发地图应用程序,但这里涉及的技术都是web技术了,而非本地技术,谷歌提供的地图查询url形式如下:

 - (IBAction)geocodeQuery:(id)sender {

     if(_txtQueryKay.text == nil || [_txtQueryKay.text length] == )
{
return;
} CLGeocoder *geocoder = [[CLGeocoder alloc]init];
[geocoder geocodeAddressString:_txtQueryKay.text completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"查询记录数:%i",[placemarks count]); if([placemarks count]>)
{
CLPlacemark *placemark = placemarks[]; CLLocationCoordinate2D coordinate = placemark.location.coordinate; NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/map?q=%f, %f",coordinate.latitude,coordinate.longitude]; NSURL *url= [NSURL URLWithString:urlString]; [[UIApplication sharedApplication] openURL:url]; //关闭键盘
[_txtQueryKay resignFirstResponder];
}
}];
}