-(void) viewdidload
{
[Super Viewdidload];
Self.geocoder = [[Clgeocoder alloc]init];
Set the map to zoom
self.mapView.zoomEnabled = YES;
Set up a map to scroll
self.mapView.scrollEnabled = YES;
Set Map not rotatable
self.mapView.rotateEnabled = NO;
Set display user's current location
Self.mapView.showsUserLocation = YES;
Set the type of map
Self.mapView.mapType = Mkmaptypestandard;
For easy testing, set the text content of the search box directly
Self.searchBar.text [email protected] "Beijing Zoo";
Self.searchBar.delegate = self;
}
Fires this method when the user clicks the Search button on the virtual keyboard
-(void) searchbarsearchbuttonclicked: (Uisearchbar *) Searchbar
{
Call the Searchbar method to search
[Self dosearch:searchbar];
}
Fires this method when the user clicks the Cancel button
Since we have redefined the appearance of the control--changing the text of the Cancel button to "search", clicking the Cancel button also performs a search
-(void) searchbarcancelbuttonclicked: (Uisearchbar *) Searchbar
{
Call the Searchbar method to search
[Self dosearch:searchbar];
}
How to perform a search
-(void) Dosearch: (Uisearchbar *) Searchbar
{
Close the Searchbar associated virtual keyboard
[Self.searchbar Resignfirstresponder];
nsstring* searchtext = Self.searchBar.text;
if (searchtext! = nil && searchtext.length > 0)
{
[Self locateat:searchtext];
}
}
This method is fired when the user enters text in the search box
-(void) searchbartextdidbeginediting: (Uisearchbar *) Searchbar
{
Show Cancel button
Searchbar.showscancelbutton = YES;
Find the Cancel button in the search box by traversing and set the text of the Cancel button to "search"
For (id cc in [searchbar.subviews[0] subviews])
{
if ([Cc Iskindofclass:[uibutton class]])
{
UIButton *button = (UIButton *) cc;
[Button settitle:@ "search" forstate:uicontrolstatenormal];
}
}
}
Converts a string address to longitude, latitude information, and performs a positioning
-(void) Locateat: (nsstring*) address
{
[Self.geocoder geocodeaddressstring:address Completionhandler:
^ (Nsarray *placemarks, Nserror *error)
{
if ([placemarks count] > 0 && error = = nil)
{
NSLog (@ "Search to match%lu address data.", (unsigned long) placemarks.count);
Process the first address
Clplacemark * Placemark = [Placemarks objectatindex:0];
NSLog (@ "longitude =:%f", placemark.location.coordinate.longitude);
NSLog (@ "latitude =:%f", placemark.location.coordinate.latitude);
NSLog (@ "country =%@", placemark.country);
NSLog (@ "postcode =%@", placemark.postalcode);
NSLog (@ "position =%@", placemark.locality);
Set the extent of the map display
Mkcoordinatespan span;
The smaller the map display, the clearer the details
Span.latitudedelta = 0.01;
Span.longitudedelta = 0.01;
Mkcoordinateregion region = {Placemark.location.coordinate,span};
Set the location of the map center to the search
[Self.mapview setregion:region]; ①
Creates a mkpointannotation that will be used as the map anchor Point
Mkpointannotation *point = [[Mkpointannotation alloc]init];
Set the coordinates of a map anchor point
Point.coordinate = placemark.location.coordinate;
Set the title of the map anchor Point
Point.title = Placemark.name;
Set the subtitle of the map anchor Point
Point.subtitle = [NSString stringwithformat:@ "%@-%@-%@-%@",
Placemark.country, Placemark.administrativearea,
Placemark.locality, placemark.sublocality];
Add a map anchor point to the map
[Self.mapview Addannotation:point];
Select the specified anchor point
[Self.mapview Selectannotation:point Animated:yes];
}
Else
{
NSLog (@ "No matching data found");
}
}];
}
Search for the specified string address