iphone – 获取当前用户位置的地址
发布时间:2020-12-14 18:08:07 所属栏目:百科 来源:网络整理
导读:我按照一些教程并混合我糟糕的代码.现在我可以获得我当前的位置.当我点击一个按钮时,它会触发一个位于我当前位置的引脚.如果我点击引脚,该消息会显示当前的地址. 我有两个问题: 为什么我无法获得更精确的地址?现在我收到国家,城市和地区的地址,但没有道路
我按照一些教程并混合我糟糕的代码.现在我可以获得我当前的位置.当我点击一个按钮时,它会触发一个位于我当前位置的引脚.如果我点击引脚,该消息会显示当前的地址.
我有两个问题: >为什么我无法获得更精确的地址?现在我收到国家,城市和地区的地址,但没有道路的名称. 非常感谢你! 这是.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,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 解决方法
你不应该真的使用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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |