Get the address of the user's current location

I follow the instructions and mix my horrible code. Now I can get my current location. And when I click on the button, it calls up a pin that is in my current location. If I click on a contact, the message displays my current address.

I have two questions:

  • Why can't I get a more accurate address? Now I get the address for the country, city and county, but without the name of the road.

  • How can I get the "text" address of the current location? I want to display the address in UILabel elsewhere, but don’t know how to do it.

Thank you very much!

here .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 

And here .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 
+4
source share
3 answers

You should not use MKReverseGeocoder since it was discounted by an apple in iOS5, you really should use CLGeocoder. The following example will return a whole ton of information that is based on NSArray * methods, then you can iterate over them and call the keys in an array.

 #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 
+6
source

Just post these delegate methods, hope this can help you.

 -- (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; } 
+3
source

I have the same problem, MKReverseGeocoder does not always return the full address, because sometimes the coordinates are not accurate enough and the service cannot generate an approximate address. To get better results, I applied another reverse geocoding against yahoo. So basically, if MKReverseGeocoder (google) doesn't return the full address, I request yahoo, which can generate an approximate one.

On the other hand, it is useful for MKReverseGeocoder, but it is deprecated in iOS 5, they recommend using CLGeocoder instead.

+1
source

All Articles