現在地の住所を取得して、注釈のタイトルに設定したいと思います。しかし、それはうまくいきませんでした。ブロックのせいだと思いますが、どうやって直せばいいのかわかりません。どんな助けでもありがたいです。最も関連性の高いコードは次のとおりです。
WhereAmIAnnotation.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface WhereAmIAnnotation : NSObject <MKAnnotation>
{
CLLocation *myLocation;
NSString *addr;
}
@property (nonatomic, retain) CLLocation *myLocation;
@property (nonatomic, copy) NSString *addr;
@end
WhereAmIAnnotation.m
#import "WhereAmIAnnotation.h"
@implementation WhereAmIAnnotation
@synthesize myLocation, addr;
- (CLLocationCoordinate2D)coordinate;
{
return self.myLocation.coordinate;
}
- (NSString *)title
{
return self.addr;
}
@end
MapViewController.m
- (IBAction)gotoCurrentLocation{
self.mapView.showsUserLocation = YES;//a bar button linked with this action
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
CLLocation *currentLocation = userLocation.location;
MKCoordinateRegion newRegion;
newRegion.center = currentLocation.coordinate;
newRegion.span.latitudeDelta = 0.02;
newRegion.span.longitudeDelta = 0.02;
[self.mapView setRegion:newRegion animated:YES];
WhereAmIAnnotation *whereAmIAnnotation = [[WhereAmIAnnotation alloc] init];
whereAmIAnnotation.myLocation = currentLocation;
whereAmIAnnotation.addr = [self addrAtLocation:currentLocation];
[self.mapView addAnnotation:whereAmIAnnotation];
self.mapView.showsUserLocation = NO;
}
- (NSString *)addrAtLocation:(CLLocation *)location{
[self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemark, NSError *error) {
CLPlacemark *topResult = [placemark objectAtIndex:0];
self.addr = [NSString stringWithFormat:@"%@ %@ %@ %@", topResult.country, topResult.locality, topResult.subLocality, topResult.thoroughfare];
}];
return self.addr;
}
このメソッドを実装する必要があります
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id )annotation{}
このばかげた質問でごめんなさい!