将注释的标题设置为当前地址 [英] Set annotation's title as current address

查看:133
本文介绍了将注释的标题设置为当前地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to get the current location's address and set it to the annotation's title. But it didn't work. I think it's because of the block, but I don't know how to fix it. Any help will be appreciated. The most related code is as following:

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

抱歉这个愚蠢的问题!

推荐答案

我应该实现这个方法

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

抱歉这个愚蠢的问题。

这篇关于将注释的标题设置为当前地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆