Set annotation name as current address

I want to get the current location address and set it to the annotation header. But that did not work. I think this is because of the block, but I don’t know how to fix it. Any help would be appreciated. The most related code is as follows:

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 have to implement this method

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

Sorry for this stupid question!

+2
source share
3 answers

I have to implement this method

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

Sorry for this stupid question.

0
source

You return the address by changing it inside the asynchronous block. You must move the return within the block or create a protocol to process 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 member variable. Do you understand WHY this fixes your problems?

+3
source

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 the code in which you installed

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

Source: https://habr.com/ru/post/1412254/


All Articles