Cllocation and mkreversegeocoder

I am trying to find a city name with cllocation and mkreversegeocoder. in my viewdidload method i istance cllocationmanager:

self.locManager = [[CLLocationManager alloc] init];
locManager.delegate = self;
locManager.desiredAccuracy = kCLLocationAccuracyBest;
[locManager startUpdatingLocation];

and after:

- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation 
*)newLocation
fromLocation:(CLLocation *)oldLocation {
//some code to retrieve my information

MKReverseGeocoder *geoCoder = [[MKReverseGeocoder alloc]     
initWithCoordinate:newLocation.coordinate ];
geoCoder.delegate = self;
[geoCoder start];
}

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark  
*)placemark
{
MKPlacemark *myPlacemark = placemark;
citta.text = [placemark locality];
}

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error{
citta.text =@ "Unknow";

The app only works the first time I get my value. the second time the application crashes. I think this is because the geocoder is running, and I think I should have one and only one launch. (but I really don't understand this ...). In what mode can I control the operation of the geocoder?

I see that I can istance mkreversegeocoder in my viewdidload using cllocationcoordinate2d, but ... how can I restore newlocation.coordinate?

I partially allow declaring a geocoder as a class variable and checking

if (self.geocoder != nil) {
   [geocoder release];
}

but .... if in my

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark  
*)placemark

or in my

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailedWithError:(@NSError 
*)error

i free or cancel my object? I feel so stupid: D

+3
1

.

MKReverseGeocoder CurrentAddress Apple. . MapViewController.h MapViewController.m.

.

+7

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


All Articles