您的位置:首页 > 移动开发 > IOS开发

iOS.定位服务与地图应用.06.调用iOS苹果地图

2014-06-22 10:52 645 查看
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

@interface T20140621001526ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UITextField *txtQueryKey;

@property (weak, nonatomic) IBOutlet UITextView *txtView;

- (IBAction)geocodeQuery:(id)sender;

@end


#import "T20140621001526ViewController.h"

@interface T20140621001526ViewController ()

@end

@implementation T20140621001526ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (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 *place = [[MKPlacemark alloc]
initWithCoordinate:coordinate addressDictionary:address];

MKMapItem *mapItem = [[MKMapItem alloc]initWithPlacemark:place];
[mapItem openInMapsWithLaunchOptions:nil];

/*
//地图上设置行车路线
NSDictionary* options =[[NSDictionary alloc]initWithObjectsAndKeys:
MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsDirectionsModeKey, nil];

MKMapItem *mapItem = [[MKMapItem alloc]initWithPlacemark:place];
[mapItem openInMapsWithLaunchOptions:options];
*/

//关闭键盘
[_txtQueryKey resignFirstResponder];
}
}];

}

/*
//多个点需要标注
- (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 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];
}

//关闭键盘
[_txtQueryKey resignFirstResponder];

if ([array count] > 0) {
[MKMapItem openMapsWithItems:array launchOptions:nil];
}
}];
}
*/
@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: