iphone - 将注解的标题设置为当前地址
<p><p>我想获取当前位置的地址并将其设置为注释的标题。但它没有用。我认为这是因为阻塞,但我不知道如何修复它。任何帮助将不胜感激。最相关的代码如下:</p>
<p><strong>WhereAmIAnotation.h</strong></p>
<pre><code>#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
</code></pre>
<p><strong>WhereAmIAnnotation.m</strong></p>
<pre><code>#import "WhereAmIAnnotation.h"
@implementation WhereAmIAnnotation
@synthesize myLocation, addr;
- (CLLocationCoordinate2D)coordinate;
{
return self.myLocation.coordinate;
}
- (NSString *)title
{
return self.addr;
}
@end
</code></pre>
<p><strong>MapViewController.m</strong></p>
<pre><code>- (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;
;
WhereAmIAnnotation *whereAmIAnnotation = [ init];
whereAmIAnnotation.myLocation = currentLocation;
whereAmIAnnotation.addr = ;
;
self.mapView.showsUserLocation = NO;
}
- (NSString *)addrAtLocation:(CLLocation *)location{
[self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemark, NSError *error) {
CLPlacemark *topResult = ;
self.addr = ;
}];
return self.addr;
}
</code></pre>
<p>我应该实现这个方法</p>
<pre><code>- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id )annotation{}
</code></pre>
<p><strong>抱歉这个愚蠢的问题!</strong></p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您在异步 block 内修改地址时返回地址。您应该将 return 移到 block 内,或者创建一个协议(protocol)来处理将此信息传递回调用者。</p>
<p>将 block 更改为:</p>
<pre><code>- (void)addrAtLocation:(CLLocation *)location{
[self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemark, NSError *error) {
CLPlacemark *topResult = ;
whereAmIAnnotation.addr = ;
}];
}
</code></pre>
<p>然后将 <code>whereAmIAnnotation</code> 设为成员变量。您了解为什么这可以解决您的问题吗?</p></p>
<p style="font-size: 20px;">关于iphone - 将注解的标题设置为当前地址,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/10633957/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/10633957/
</a>
</p>
页:
[1]