如何查找当前位置地址?

时间:2016-04-04 08:55:18

标签: objective-c

我在Mapkit上完成了使用以下代码查找currentLocation的任务。现在我想在注释pain中显示当前位置地址。如何校准地理编码方法以显示当前位置address.latitude和经度值?

 #import "MapKitViewController.h"
     #import "ViewController.h"
    #define METERS_MILE 1609.344
    #define METERS_FEET 3.28084

@interface MapKitViewController ()<CLLocationManagerDelegate,MKMapViewDelegate >

@end

@implementation MapKitViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    _mapView.delegate = self;


    locationManager =[[CLLocationManager alloc] init];

    [[self mapView  ] setShowsUserLocation:YES];
    [locationManager setDelegate:self];
    [locationManager requestWhenInUseAuthorization];
    [locationManager requestAlwaysAuthorization];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [locationManager startUpdatingLocation];
    NSLog(@" startUpdatingLocation");


}

- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray<CLLocation *> *)locations{
    CLLocation *location=locations.lastObject;
    [[self latitudeValue] setText:[NSString stringWithFormat:@"%.6f",location.coordinate.latitude]];
    [[self longnitudeValue] setText:[NSString stringWithFormat:@"%.6f",location.coordinate.longitude]];
//    [[self altitudeValue] setText:[NSString stringWithFormat:@"%.2f feet",location.altitude*METERS_FEET]];
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, 2*METERS_MILE, 2*METERS_MILE);
    [[self mapView] setRegion:viewRegion animated:YES];
    MKPointAnnotation *point=[[MKPointAnnotation alloc]init];
    point.coordinate=location.coordinate;

    [self.mapView addAnnotation:point];

    point.title=@"this is my place";


    NSLog(@"I got the point");




    NSLog(@"didUpdateLocations");


}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


@end

1 个答案:

答案 0 :(得分:0)

对于地图上的显示当前位置,请使用此..

 MKPointAnnotation *annonation = [[MKPointAnnotation alloc]init];
CLLocationCoordinate2D mycordinate;
mycordinate.latitude = [self.strlog floatValue];
mycordinate.longitude =[self.strlat floatValue];
annonation.coordinate = mycordinate;
[self.mapview addAnnotation:annonation];