4

私はいくつかのチュートリアルに従い、ひどいコードを混ぜます。これで現在地を取得できます。ボタンをタップすると、現在の場所にあるピンがトリガーされます。ピンをタップすると、メッセージに現在のアドレスが表示されます。

2 つの質問があります。

  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
4

3 に答える 3

6

MKReverseGeocoder は Apple によって 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
于 2011-10-21T15:02:59.880 に答える
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;

}
于 2011-10-21T13:24:44.767 に答える
1

私は同じ問題を抱えています. MKReverseGeocoder は常に完全な住所を返すとは限りません. 最良の結果を得るために、yahoo に対して別のリバース ジオコーディングを実装しました。したがって、基本的に MKReverseGeocoder (google) が完全な住所を返さない場合は、おおよその住所を生成できる yahoo に問い合わせます。

一方、MKReverseGeocoder には気をつけてください。IOS 5 で廃止されました。代わりに CLGeocoder を使用することをお勧めします。

于 2011-10-21T13:50:48.410 に答える