使用GPS在iOS中定位

示意图如下:

b_large_45SX_39960000026b1261b_large_iz4Y_7182000001d31263

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;

  • }