2

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

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

对不起这个愚蠢的问题!

4

3 回答 3

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一个成员变量。您了解为什么这可以解决您的问题吗?

于 2012-05-17T14:29:13.477 回答
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];    
于 2012-05-17T11:08:43.153 回答
0

我应该实现这个方法

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

对不起这个愚蠢的问题。

于 2012-05-17T18:18:24.663 回答