Set annotation's title as current address

一曲冷凌霜 提交于 2020-01-21 18:08:42

问题


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

I should implement this method

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

sorry for this stupid question!


回答1:


You are returning the address while modifying it inside an asynchronous block. You should move the return inside the block, or a create a protocol to handle passing this information back to the caller.

Change the block to this:

- (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];
    }];
}

Then make whereAmIAnnotation a member variable. Do you understand WHY this fixes your issues?




回答2:


Create .h file

@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;

Create .m file

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

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

}

write a code where you set

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



回答3:


I should implement this method

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

Sorry for that stupid question.



来源:https://stackoverflow.com/questions/10633957/set-annotations-title-as-current-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!