问题描述
我尝试使用cllocation和mkreversegeocoder来检索城市名称。我的viewdidload方法中的
我是cllocationmanager:
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];
以及之后:
- (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?
我看到我可以使用cllocationcoordinate2d在我的viewdidload中使用mkreversegeocoder但是......我可以通过哪种方式来检索newlocation 。坐标?
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];
}
但....如果在我的
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark
*)placemark
或在我的
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailedWithError:(@NSError
*)error
我发布或取消我的对象?
我感觉很蠢:D
i release or cancel my object?I'm feeling so stupid :D
推荐答案
不要将反向地理编码器创建为局部变量。
Don't create the reverse geocoder as a local variable.
查看示例应用程序。请参阅MapViewController.h和MapViewController.m。
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.
这篇关于cllocation和mkreversegeocoder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!