Set annotation's title as current address

后端 未结 3 1242
天涯浪人
天涯浪人 2021-01-22 13:46

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. An

相关标签:
3条回答
  • 2021-01-22 13:55

    I should implement this method

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

    Sorry for that stupid question.

    0 讨论(0)
  • 2021-01-22 13:58

    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?

    0 讨论(0)
  • 2021-01-22 14:02

    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];    
    
    0 讨论(0)
提交回复
热议问题