问题
i'm try to retreiving 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";
Application works only the first time i get my value. on the second time app crash. I think is because geocoder is started and i think i must have one and only one istance running. (but i'm really not shure of this...). in which way i can controll geocoder is running?
I've see that i can istance mkreversegeocoder in my viewdidload using cllocationcoordinate2d but ... in which way i can retreive newlocation.coordinate?
I've partially resolve declaring geocoder as 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 release or cancel my object? I'm feeling so stupid :D
回答1:
Don't create the reverse geocoder as a local variable.
Look at the MKReverseGeocoder example in the CurrentAddress sample app provided by Apple. See MapViewController.h and MapViewController.m.
Follow the same pattern in your code.
来源:https://stackoverflow.com/questions/2637152/cllocation-and-mkreversegeocoder