I'm trying to add a UISearchBar
on map for apple in iOS7. I've seen this question here How to search MKMapView with UISearchBar?, but this code doesn't work in my project because there are several methods currently deprecated.
This is the code.
#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];
}];
}