示意图如下:
LBS.H
@property (weak, nonatomic) IBOutletUITextField *latitudeField; @property (weak, nonatomic) IBOutletUITextField *longitudeField; @property (weak, nonatomic) IBOutletUITextField *accuracyField; @property (nonatomic) CLLocationManager *lm;
LBS.M
- (void)viewDidLoad { [superviewDidLoad]; // Do any additional setup after loading the view, typically from a nib. lm = [[CLLocationManageralloc] init]; if ([lmlocationServicesEnabled]) { lm.delegate = self; lm.desiredAccuracy = kCLLocationAccuracyBest; lm.distanceFilter = 1000.0f; [lmstartUpdatingLocation]; } } - (void) locationManager: (CLLocationManager *) manager didUpdateToLocation: (CLLocation *) newLocation fromLocation: (CLLocation *) oldLocation{ NSString *lat = [[NSStringalloc] initWithFormat:@"%g", newLocation.coordinate.latitude]; self.latitudeField.text = lat; NSString *lng = [[NSStringalloc] initWithFormat:@"%g", newLocation.coordinate.longitude]; self.longitudeField.text = lng; NSString *acc = [[NSStringalloc] initWithFormat:@"%g", newLocation.horizontalAccuracy]; self.accuracyField.text = acc; } - (void) locationManager: (CLLocationManager *) manager didFailWithError: (NSError *) error { NSString *msg = @"Error obtaining location"; UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:@"Error" message:msg delegate:nil cancelButtonTitle: @"Done" otherButtonTitles:nil]; [alert show]; }
mapviewer.m
- (void)viewDidAppear:(BOOL)animated { MKCoordinateSpan span; span.latitudeDelta=.009; span.longitudeDelta=.009; MKCoordinateRegion region; CLLocation *newLocation = ((NaviViewController *)self.parentViewController).location; region.center = newLocation.coordinate; region.span=span; [self.mapView setRegion:region animated:TRUE]; self.mapView.showsUserLocation = YES; }