2

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!

Dharmesh Dhorajiya
  • 3,976
  • 9
  • 30
  • 39
lu yuan
  • 7,207
  • 9
  • 44
  • 78
  • Does this code compile? In WhereAmIAnnotation.m, geocoder is synthesized but it's not declared. In the addrAtLocation method in MapViewController.m, it refers to self.addr but isn't addr a property in WhereAmIAnnotation? –  May 17 '12 at 13:24
  • @AnnaKarenina just deleted it, it's typo. – lu yuan May 17 '12 at 13:42

3 Answers3

3

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?

Sam
  • 2,579
  • 17
  • 27
  • could you plz show me some related code to create a protocol to handle passing this information back to the caller? I will appreciate it very much:) – lu yuan May 17 '12 at 14:33
  • I make whereAmIAnnotation as a property, but still don't work. – lu yuan May 17 '12 at 16:26
0

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];    
Abhishek Patel
  • 71
  • 4
  • 12
  • thx for your answer. But i do not understand here "ann.title = AppMain.Heading; ann.subtitle = AppMain.Detail; " – lu yuan May 17 '12 at 11:25
  • this class give you two things 1st is heading and 2nd is his detail, so you can set you text in ann.title=@"Yourheading"; and ann.subtitle= @"this is detail text"; – Abhishek Patel May 17 '12 at 11:32
  • MKUserLocation class? I have tried your codes, seems not working. – lu yuan May 17 '12 at 11:35
  • have you add framework - corelocation and mapkit...?? did you get error .? – Abhishek Patel May 17 '12 at 11:47
  • Yes, I do add them, and without error. What's your AppMain.Heading and AppMain.Detail? I wanna get the current address. – lu yuan May 17 '12 at 11:48
  • you mean you need current longitude and latitude...?? – Abhishek Patel May 17 '12 at 11:49
  • AppMain.Heading is a simple NSString that i define in Delegate. – Abhishek Patel May 17 '12 at 11:49
  • - (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; } – lu yuan May 17 '12 at 11:56
  • you want current location detail from tapping on map...?? – Abhishek Patel May 17 '12 at 12:09
  • I have a button to get current location, and use reverseGeocodeLocation to get the address. and i wanna my annotation to display the addr – lu yuan May 17 '12 at 13:23
0

I should implement this method

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

Sorry for that stupid question.

Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
lu yuan
  • 7,207
  • 9
  • 44
  • 78