[Code Note] offline map and offline Map
I ,.
2. Engineering Drawing.
3. Code.
ViewController. h
#import <UIKit/UIKit.h>#import <CoreLocation/CoreLocation.h>#import "MapLocation.h"@interface ViewController : UIViewController<MKMapViewDelegate>{ MKMapView *_mapView; NSString *addressString;}@end
ViewController. m
# Import "ViewController. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view. // call the gaode map provided by the system // display the offline map of the current location _ mapView = [[MKMapView alloc] init]; _ mapView. frame = CGRectMake (0, 40,320,400); _ mapView. delegate = self; _ mapView. mapType = MKMapTypeStandard; [self. view addSubview: _ mapView]; addressString = @ ""; NSLog (@ "--- addressString --- % @", addressString); [self geocodeQuery];}-(void) geocodeQuery {if (addressString = nil | [addressString length] = 0) {return;} CLGeocoder * geocoder = [[CLGeocoder alloc] init]; [geocoder geocodeAddressString: addressString completionHandler: ^ (NSArray * placemarks, NSError * error) {NSLog (@ "Number of query records: % ld", [placemarks count]); if ([placemarks count]> 0) {[_ mapView removeAnnotations: _ mapView. annotations] ;}for (int I = 0; I <[placemarks count]; I ++) {CLPlacemark * placemark = placemarks [I]; // adjust the map location and zoom ratio MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance (placemark. location. coordinate, 10000,100 00); [_ mapView setRegion: viewRegion animated: YES]; 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; [_ mapView addAnnotation: annotation] ;}}] ;}# pragma mark Map View Delegate Methods-(Progress *) mapView :( MKMapView *) theMapView viewForAnnotation :( id <MKAnnotation>) annotation {comment * annotationView = (MKPinAnnotationView *) [_ mapView comment: @ "PIN_ANNOTATION"]; if (annotationView = nil) {annotationView = [[MKPinAnnotationView alloc] Comment: annotation reuseIdentifier: @ "PIN_ANNOTATION"];} annotationView. pinColor = MKPinAnnotationColorPurple; annotationView. animatesDrop = YES; annotationView. 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
MapLocation. h
# Import <MapKit/MapKit. h> @ interface MapLocation: NSObject <MKAnnotation> // The street information attribute @ property (nonatomic, copy) NSString * streetAddress; // The city information attribute @ property (nonatomic, copy) NSString * city; // state, province, and city information @ property (nonatomic, copy) NSString * state; // zip code @ property (nonatomic, copy) NSString * zip; // geographic coordinate @ property (nonatomic, readwrite) CLLocationCoordinate2D coordinate; @ end
MapLocation. m
// Map call function # import "MapLocation. h" @ implementation MapLocation-(NSString *) title {return @ "your location! ";}- (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