2011-10-21 37 views
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

只要将这个委托方法,我希望这可以帮助你。

-- (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; 

} 
+0

这已在IOS 5 – Rob

+0

MK_CLASS_DEPRECATED(NA,NA,3_0,5_0) – VietHung

1

我有同样的问题,MKReverseGeocoder并不总是返回完整的地址,有时东阳的坐标是不够准确,服务不能产生近似地址。 为了获得最佳效果,我实施了针对雅虎的其他反向地理编码。所以基本上如果MKReverseGeocoder(谷歌)不返回完整的地址,我查询可以生成一个近似的雅虎。

在另一方面是carreful与MKReverseGeocoder它已经在iOS 5中被否决,他们对推荐使用CLGeocoder代替

+0

1被废弃为CLGeocoder建议。 –

6

你真的不应该使用MKReverseGeocoder,因为它已经在iOS5中已经贬值了的苹果,你真的应该使用CLGeocoder。以下示例将返回基于NSArray *地标的全部信息,然后您可以遍历它们并调用assoc数组中的键。

#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