MKLocalSearch用法

原文出处:http://phpadvocate.com/blog/2013/01/ios-6-1-simple-example-using-mklocalsearch/

/* MyMapViewController.m */
 
/** 
 * @author Jonathon Hibbard
 */
#import "MyMapViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import <AddressBook/AddressBook.h>
 
 
@interface StoresMapViewController () <MKMapViewDelegate>
// Don't forget to hook this up through IB/NiB - You'll also need to set the mapview's delegate to be this object as well...
@property (nonatomic, weak) IBOutlet MKMapView *mapView;
 
// LocalSearch Stuff...
@property (nonatomic, strong) MKLocalSearch *localSearch;
@property (nonatomic, strong) MKLocalSearchRequest *localSearchRequest;
 
@property CLLocationCoordinate2D coords;
 
//@property MKMapItem *currentLocation;
 
@end
 
@implementation StoresMapViewController
 
 
-(void)viewDidLoad {
 
    [super viewDidLoad];
 
    // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    //
    // Temporary - change this to use the user's current location or whatever you want...
    //
    // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    [self setupCoordsUsingAddress:@"Manchester KY 40962"];
}
 
 
-(void)didReceiveMemoryWarning {
 
    [super didReceiveMemoryWarning];
 
    self.localSearch = nil;
    self.localSearchRequest = nil;
}
 
 
-(void)setupCoordsUsingAddress:(NSString *)address {
 
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
 
        if(error) {
 
          NSLog(@"FAILED to obtain geocodeAddress String. Error : %@", error);
          abort();
 
        } else if(placemarks && placemarks.count > 0) {
 
              // Find all retail stores...
              [self issueLocalSearchLookup:@"retail" usingPlacemarksArray:placemarks];
        }
    }];
}
 
 
// Ex: [self issueLocalSearchLookup:@"grocery"];
-(void)issueLocalSearchLookup:(NSString *)searchString usingPlacemarksArray:(NSArray *)placemarks {
 
    // Search 0.250km from point for stores.
    CLPlacemark *placemark = placemarks[0];
    CLLocation *location = placemark.location;
 
    self.coords = location.coordinate;
 
    // Set the size (local/span) of the region (address, w/e) we want to get search results for.
    MKCoordinateSpan span = MKCoordinateSpanMake(0.6250, 0.6250);
    MKCoordinateRegion region = MKCoordinateRegionMake(self.coords, span);
 
    [self.mapView setRegion:region animated:NO];
 
    // Create the search request
    self.localSearchRequest = [[MKLocalSearchRequest alloc] init];
    self.localSearchRequest.region = region;
    self.localSearchRequest.naturalLanguageQuery = searchString;
 
    // Perform the search request...
    self.localSearch = [[MKLocalSearch alloc] initWithRequest:self.localSearchRequest];
    [self.localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
 
        if(error){
 
            NSLog(@"localSearch startWithCompletionHandlerFailed!  Error: %@", error);
            return;
 
        } else {
 
            // We are here because we have data!  Yay..  a whole 10 records of it too *flex*
            // Do whatever with it here...
 
            for(MKMapItem *mapItem in response.mapItems){
 
                // Show pins, pix, w/e...
 
                NSLog(@"Name for result: = %@", mapItem.name);
                // Other properties includes: phoneNumber, placemark, url, etc.
                // More info here: https://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKLocalSearch/Reference/Reference.html#//apple_ref/doc/uid/TP40012893
            }
 
            MKCoordinateSpan span = MKCoordinateSpanMake(0.2, 0.2);
            MKCoordinateRegion region = MKCoordinateRegionMake(self.coords, span);
            [self.mapView setRegion:region animated:NO];
        }
    }];
}
 
@end;

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值