If you don't talk nonsense, go directly to the project. Let's take a look at it first!
There are two files in the project: The chonViewController and mapLocation files. The mapLocation file is used for displaying landmarks and additional information. The additional information is displayed normally when you did it yesterday, but you do not want to write a blog today, very depressing! <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + ICA8aW1nIHNyYz0 = "" alt = "\">
Remember to add the class library after creating the project. The method for adding MapKit. framework is not described here.
The chonViewController. h file code is as follows:
#import
#import
#import "mapLocation.h"@interface chonViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *txtQueryKey;@property (weak, nonatomic) IBOutlet MKMapView *mapView;- (IBAction)geocodeQuery:(id)sender;@end
The chonViewController. m file code is as follows:
//// ChonViewController. m // MyLocation2 /// Created by choni on 14-5-13. // Copyright (c) 2014 choni. all rights reserved. // # import "chonViewController. h "@ interface chonViewController () @ end @ implementation chonViewController-(void) viewDidLoad {[super viewDidLoad]; // annotation map type _ mapView. mapType = MKMapTypeStandard; // used to assign the current view controller to the delegate attribute _ mapView of the map view. delegate = self;}-(void) didReceiveMemoryWarning {[super didreceivemorywarning];} # pragma mark-query button trigger action-(IBAction) geocodeQuery :( id) sender {if (_ txtQueryKey. text = nil | [_ txtQueryKey. text length] = 0) {return;} CLGeocoder * geocode = [[CLGeocoder alloc] init]; [geocode geocodeAddressString: _ txtQueryKey. text completionHandler: ^ (NSArray * placemarks, NSError * error) {NSLog (@ "Number of query records: % I", [placemarks count]); if ([placemarks count]> 0) {// remove all annotation points on the current map [_ mapView removeAnnotations: _ mapView. annotations] ;}for (int I = 0; I <[placemarks count]; I ++) {CLPlacemark * placemark = placemarks [I]; // close the keyboard [_ txtQueryKey resignFirstResponder]; // adjust the map position and zoom ratio. The first parameter is the center point of the target region, and the second parameter is the span of the north and south of the target region, third parameter: the east-west span of the target region, in the unit of meters MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance (placemark. location. coordinate, 10000,100 00); // reset the display area of the map view [_ mapView setRegion: viewRegion animated: YES]; // instantiate the MapLocation object mapLocation * annotation = [[mapLocation alloc] init]; annotation. streetAddress = placemark. thoroughfare; annotation. city = placemark. locality; annotation. state = placemark. administrativeArea; annotation.zip = placemark. postalCode; annotation. coordinate = placemark. location. coordinate; // Add the annotation point MapLocation object to the map view. Once this method is called, the map view delegate method mapView: ViewForAnnotation: Will be called back [_ mapView addAnnotation: annotation] ;}}] ;}# pragma mark mapView when a map is added as a annotation-(MKAnnotationView *) mapView :( MKMapView *) theMapView viewForAnnotation :( id
) Annotation {// obtain the map annotation object MKPinAnnotationView * annotationView = (MKPinAnnotationView *) [_ mapView annotation: @ "PIN_ANNOTATION"]; if (annotationView = nil) {annotationView = [[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: @ "PIN_ANNOTATION"];} // set the pin annotation view to purple annotationView. pinColor = MKPinAnnotationColorPurple; // specifies whether the map is displayed as an animation on the map. animatesDrop = YES; // used to add annotationView information on the annotation point. canShowCallout = YES; return annotationView;}-(void) mapView :( MKMapView *) mapView didUpdateUserLocation :( MKUserLocation *) userLocation {_ mapView. centerCoordinate = userLocation. location. coordinate;}-(void) mapViewDidFailLoadingMap :( MKMapView *) theMapView withError :( NSError *) error {NSLog (@ "error: % @", [error description]);} @ end
The mapLocation. h code is as follows:
//// MapLocation. h // MyLocation2 /// Created by choni on 14-5-13. // Copyright (c) 2014 choni. all rights reserved. // # import
# Import
@ Interface mapLocation: NSObject
// The map punctuation class must implement the MKAnnotation protocol // geographic coordinate @ property (nonatomic, readwrite) CLLocationCoordinate2D coordinate; // The street attribute information @ property (nonatomic, copy) NSString * streetAddress; // city information attributes @ property (nonatomic, copy) NSString * city; // state, province/city information @ property (nonatomic, copy) NSString * state; // zip code @ property (nonatomic, copy) NSString * zip; @ end
The mapLocation. m file is as follows:
//// MapLocation. m // MyLocation2 /// Created by choni on 14-5-13. // Copyright (c) 2014 choni. all rights reserved. // # import "mapLocation. h "@ implementation mapLocation # Main title on The pragma mark punctuation-(NSString *) title {return @" your location! ";}# Subtitle-(NSString *) subtitle {NSMutableString * ret = [NSMutableString new]; if (_ state) {[ret appendString: _ state] ;}if (_ city) {[ret appendString: _ city] ;}if (_ city & _ state) {[ret appendString :@", "];} if (_ streetAddress & (_ city | _ state | _ zip) {[ret appendString: @" · "];} if (_ streetAddress) {[ret appendString: _ streetAddress];} if (_ zip) {[ret appendFormat: @ ", % @", _ zip];} return ret;} @ end
OK. The comments in the Code are more detailed!