将注释的标题设置为当前地址

时间:2012-05-17 10:26:57

标签: iphone ios mkannotation cllocation

我想获取当前位置的地址并将其设置为注释的标题。但它没有用。我认为这是因为阻止,但我不知道如何解决它。任何帮助将不胜感激。最相关的代码如下:

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{}

抱歉这个愚蠢的问题!

3 个答案:

答案 0 :(得分:3)

在异步块中修改地址时返回地址。您应该在块内移动返回,或者创建一个协议来处理将此信息传递回调用者。

将块更改为:

- (void)addrAtLocation:(CLLocation *)location{
    [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemark, NSError *error) {
        CLPlacemark *topResult = [placemark objectAtIndex:0];
        whereAmIAnnotation.addr = [NSString stringWithFormat:@"%@ %@ %@ %@", topResult.country, topResult.locality, topResult.subLocality, topResult.thoroughfare];
    }];
}

然后将whereAmIAnnotation作为成员变量。你明白为什么这会解决你的问题吗?

答案 1 :(得分:0)

创建.h文件

@interface MapPinClass : NSObject <MKAnnotation>{
CLLocationCoordinate2D coordinate; 
NSString *title; 
NSString *subtitle,*color,*index,*locationId;

}

@property (nonatomic, assign) CLLocationCoordinate2D coordinate; 
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle,*color,*index,*locationId;

创建.m文件

@implementation MapPinClass
@synthesize coordinate,title,subtitle,color,index,locationId;

-(void)dealloc{
[title release];
[super dealloc];

}

编写设置

的代码
 MapPinClass *ann = [[MapPinClass alloc] init]; 
ann.title = AppMain.Heading;
ann.subtitle = AppMain.Detail; 
ann.coordinate = region.center; 
[myMapView addAnnotation:ann];    

答案 2 :(得分:0)

我应该实现这个方法

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation{}

抱歉这个愚蠢的问题。