获取当前用户位置的地址

4
我跟着一些教程混合了我的糟糕代码。现在我可以获取我的当前位置。当我点击按钮时,它会触发一个定位在我的当前位置的标记。如果我点击标记,消息将显示我的当前地址。
我有两个问题:
1. 为什么我无法获取更精确的地址?现在我收到的地址只有国家、城市和区,没有道路名称。 2. 如何获取当前位置地址的“文本”?我想在其他地方显示地址的UILabel,但不知道如何做。
非常感谢!
以下是.h文件。
#import <UIKit/UIKit.h>
#import "MapKit/MapKit.h"
#import "CoreLocation/CoreLocation.h"

@interface UserLocationAddressViewController : UIViewController     <MKMapViewDelegate,MKReverseGeocoderDelegate>
{
    MKMapView *userLocationAddMapView;
    CLLocationManager *locationManager;
}

@property (nonatomic, retain) MKMapView *userLocationAddMapView;

-(IBAction)getUserLocationAddress:(id)sender;

@end

这里是 .m 文件。
#import "UserLocationAddressViewController.h"

@implementation UserLocationAddressViewController

@synthesize userLocationAddMapView;

-(IBAction)getUserLocationAddress:(id)sender
{
    MKReverseGeocoder *geoCoder = [[MKReverseGeocoder alloc] initWithCoordinate:locationManager.location.coordinate];
    geoCoder.delegate = self;
    [geoCoder start]; 
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

-(void)dealloc
{
    [userLocationAddMapView release];
    [super dealloc];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

self.userLocationAddMapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
    userLocationAddMapView.showsUserLocation = YES;
    userLocationAddMapView.delegate = self;
    self.userLocationAddMapView.mapType = MKMapTypeStandard;

    locationManager = [[CLLocationManager alloc] init];
    [locationManager startUpdatingLocation];
    CLLocationCoordinate2D coordinate = locationManager.location.coordinate;

    MKCoordinateRegion mapRegion;
    mapRegion.center = coordinate;
    MKCoordinateSpan mapSpan;
    mapSpan.latitudeDelta = 0.006;
    mapSpan.longitudeDelta = 0.006;
    mapRegion.span = mapSpan;
    [userLocationAddMapView setRegion:mapRegion animated:YES];
    self.userLocationAddMapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:self.userLocationAddMapView];

    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Get Add" style:UIBarButtonItemStylePlain target:self action:@selector(getUserLocationAddress:)] autorelease];  
}

-(void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
    NSLog(@"placemark %f %f", placemark.coordinate.latitude, placemark.coordinate.longitude);
    NSLog(@"addressDictionary %@", placemark.addressDictionary);
    [userLocationAddMapView addAnnotations:[NSArray arrayWithObjects:placemark, nil]];
    [geocoder release];
}

-(void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error
{
    NSLog(@"reverseGeocoder fail");
    [geocoder release];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
3个回答

6

您不应该使用MKReverseGeocoder,因为它已在iOS5中被苹果弃用,您应该使用CLGeocoder。以下示例将返回基于NSArray * placemarks的大量信息,然后您可以遍历它们并调用关联数组中的键。

#import "MapPoint.h"

@implementation MapPoint
@synthesize coordinate, title, dateAdded, subtitle, city, reverseGeo;

-(id)initWithCoordinates:(CLLocationCoordinate2D)c 
                   title:(NSString *)t {

    self = [super init];
    if (self) {

        coordinate = c;
        [self setTitle: t];
        [self setCurrentCity: [[CLLocation alloc] initWithLatitude:c.latitude longitude:c.longitude]];

        [self setDateAdded: [[NSDate alloc] init]];

    }
    return self;

}

-(void)setCurrentCity: (CLLocation *)loc {

    if (!self.reverseGeo) {
        self.reverseGeo = [[CLGeocoder alloc] init];
    }

    [self.reverseGeo reverseGeocodeLocation: loc completionHandler: 
     ^(NSArray *placemarks, NSError *error) {

         for (CLPlacemark *placemark in placemarks) {

             NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
             [formatter setTimeStyle:NSDateFormatterNoStyle];
             [formatter setDateStyle:NSDateFormatterLongStyle];

             NSString *dateString = [formatter stringFromDate: [self dateAdded]];
             [self setSubtitle: [dateString stringByAppendingString: [placemark locality] ] ];             

         }
     }];
}

@end

3

只需放置这些委托方法,希望这能帮助您。

-- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error {
    NSLog(@"MKReverseGeocoder has failed.");

}

-- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark {
    currentLocation = [placemark.locality stringByAppendingFormat:@", "];
    currentLocation = [currentLocation stringByAppendingString:placemark.country];
    [indicator stopAnimating];
    txtLocation.text = currentLocation;

}

这在iOS 5中已被弃用。 - serverpunk
MK_CLASS_DEPRECATED(NA, NA, 3_0, 5_0) - Tony

1

我遇到了同样的问题,MKReverseGeocoder有时候不能返回完整的地址,因为有时候坐标不够准确,服务无法生成近似地址。 为了获得最佳结果,我实现了另一种反向地理编码方法来使用雅虎。所以基本上如果MKReverseGeocoder(谷歌)不能返回完整的地址,我会查询可以生成近似地址的雅虎。

另一方面,要小心MKReverseGeocoder已经在IOS 5中被弃用,他们建议使用CLGeocoder代替。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接