原文出处: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;
本文介绍了一个iOS应用中使用MapKit进行本地搜索的例子。该应用通过获取地理位置坐标,并以此为中心,在指定范围内搜索零售商店。同时展示了如何解析搜索结果并在地图上显示。
1116

被折叠的 条评论
为什么被折叠?



