我正在尝试在iOS7的苹果地图上添加UISearchBar
。我在How to search MKMapView with UISearchBar?看到了这个问题,但是这个代码在我的项目中不起作用,因为目前有几种方法已被弃用。
这是代码。
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface ViewController : UIViewController <MKMapViewDelegate, MKAnnotation>
@property (strong, nonatomic) IBOutlet MKMapView *mapView;
@property (strong, nonatomic) IBOutlet UISearchBar *SearchBar;
@end
-(void)searchBarSearchButtonClicked:(UISearchBar *)theSearchBar
{
[theSearchBar resignFirstResponder];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:theSearchBar.text completionHandler:^(NSArray *placemarks, NSError *error) {
//Error checking
CLPlacemark *placemark = [placemarks objectAtIndex:0];
MKCoordinateRegion region;
region.center.latitude = placemark.region.center.latitude;
region.center.longitude = placemark.region.center.longitude;
MKCoordinateSpan span;
double radius = placemark.region.radius / 1000; // convert to km
NSLog(@"[searchBarSearchButtonClicked] Radius is %f", radius);
span.latitudeDelta = radius / 112.0;
region.span = span;
[theMapView setRegion:region animated:YES];
}];
}