Iphone 地理编码+;我的球打得不好
我遇到了一个最奇怪的问题,它让我头疼。我在单例中设置的全局变量在其设置的函数中正确报告,然后在下一个函数(我需要访问它的地方)中报告为NULL,但在另一个视图中报告为正确!因此,变量设置正确,但它在某个函数中不起作用。还有一个奇怪的错误警告是由违规行(我在*)生成的 警告是:Iphone 地理编码+;我的球打得不好,iphone,ios,geocoding,Iphone,Ios,Geocoding,我遇到了一个最奇怪的问题,它让我头疼。我在单例中设置的全局变量在其设置的函数中正确报告,然后在下一个函数(我需要访问它的地方)中报告为NULL,但在另一个视图中报告为正确!因此,变量设置正确,但它在某个函数中不起作用。还有一个奇怪的错误警告是由违规行(我在*)生成的 警告是: Property access result unused - getters should not be used for side effects. 抱歉,代码太零碎了。我正在进行原型设计和学习,所以这是我从网络上拼
Property access result unused - getters should not be used for side effects.
抱歉,代码太零碎了。我正在进行原型设计和学习,所以这是我从网络上拼凑出来的一堆东西。代码的作用是识别地图视图上的一个长点击,然后在该位置放置一个pin(同时记录该位置),我尝试使用地理代码在pin位置显示地址
第一个功能如下:
- (void)handleLongPress:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state != UIGestureRecognizerStateBegan)
return;
CGPoint touchPoint = [gestureRecognizer locationInView:self.fullMapView];
CLLocationCoordinate2D touchMapCoordinate =
[self.fullMapView convertPoint:touchPoint toCoordinateFromView:self.fullMapView];
//save new birthplace in global variable
globalsSingle.gblBirthplace = touchMapCoordinate;
//place user location and record it
MKUserLocation *location = fullMapView.userLocation;
globalsSingle.gblCurrentLocation = location.coordinate;
//first remove any previous birthplace pin
[self removeAllPinsButUserLocation];
[self reverseGeocode];
//place new birthplace pin
MKPointAnnotation *birthPlacePin = [[MKPointAnnotation alloc] init];
birthPlacePin.coordinate = touchMapCoordinate;
birthPlacePin.title = @"My Birthplace";
**** birthPlacePin.subtitle = @"%@", globalsSingle.gblAddress; ****
[self.fullMapView addAnnotation:birthPlacePin];
NSLog(@"gblAddress = %@", globalsSingle.gblAddress);
}
上述函数调用下一个:
-(void)reverseGeocode {
CLGeocoder *ceo = [[CLGeocoder alloc]init];
CLLocation *loc = [[CLLocation alloc]initWithLatitude:globalsSingle.gblBirthplace.latitude longitude:globalsSingle.gblBirthplace.longitude]; //insert your coordinates
[ceo reverseGeocodeLocation: loc completionHandler:
^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
//String to hold address
NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
// save the address text
globalsSingle.gblAddress = locatedAt;
NSLog(@"addressDictionary %@", placemark.addressDictionary);
NSLog(@"placemark %@",placemark.region);
NSLog(@"placemark %@",placemark.country); // Give Country Name
NSLog(@"placemark %@",placemark.locality); // Extract the city name
NSLog(@"location %@",placemark.name);
NSLog(@"location %@",placemark.ocean);
NSLog(@"location %@",placemark.postalCode);
NSLog(@"location %@",placemark.subLocality);
NSLog(@"location %@",placemark.location);
//Print the location to console
NSLog(@"I am currently at %@",locatedAt);
NSLog(@"gblAddress from reverse Geocode = %@", globalsSingle.gblAddress);
}
];
}
(对我来说)更奇怪的是,reverseGeocode中的NSLog都正确打印,但是第一个函数中的NSLog报告为空,并且在reverseGeocode中的NSLog之前打印,即使它(我假设)是第二个执行的!例如,调试输出是:
2013-05-21 23:41:04.662 Project Name[5659:c07] gblAddress = (null)
2013-05-21 23:41:04.808 Project Name[5659:c07] gblAddress from reverse Geocode = Januária - MG, Brazil
如果有人愿意提供任何帮助,我将不胜感激,因为我被迷惑了:)当你打电话给[self reverseGeocode];handleLongPress的其余部分将继续运行,无需等待反转代码完成。这就是为什么您看到打印函数被调用的顺序出乎您的意料
[self performSelectorOnMainThread:@selector(reverseGeocode) withObject:nil waitUntilDone:YES];
如果handleLongPress在主线程上运行,则上面的行可以替换[self reverseGeocode],并应产生预期的结果。方法reverseGeocodeLocation:completionHandler:是异步执行的,这意味着它将在完成之前移动到下一行 异步调用它是因为方法reverseGeocodeLocation:completionHandler:可能需要一些时间来完成,完成后,将调用完成块 您应该仅在调用reverseGeocodeLocation的完成块之后(例如在完成块内部)放置新的出生地pin,以确保在放置pin之前先获得placemark数据。或者,您可以只更新完成块中新添加的pin的字幕
[ceo reverseGeocodeLocation: loc completionHandler:
^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
//String to hold address
NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
// save the address text
globalsSingle.gblAddress = locatedAt;
//place new birthplace pin
MKPointAnnotation *birthPlacePin = [[MKPointAnnotation alloc] init];
birthPlacePin.coordinate = globalsSingle.gblBirthplace;
birthPlacePin.title = @"My Birthplace";
birthPlacePin.subtitle = globalsSingle.gblAddress;
[self.fullMapView addAnnotation:birthPlacePin];
}
];
}
谢谢你的回复,这真是个好主意。然而。。。没有骰子。我认为MKPointAnnotation subtitle或任何其他元素都不能使用变量。它需要纯文本。我将尝试通过MKAnnotation或MKAnnotationView使用不同的实现。。。也许……还有。结果和我一直遇到的问题一样。。。“字幕值拒绝显示,而且仍然有那个深奥的警告。@肯威廉姆森——那完全是另一回事了。”。我以为问题是关于执行代码的顺序。哦,是的,也是这样,谢谢你的提醒。有一些事情让我感到困惑,我认为这可以归结为MKPointAnnotation问题(我认为,这不可能是其他任何事情)。如果是这样的话,编写一个不接受文本值变量的类是非常奇怪的。。。实际上有点没用。如果可以的话,我会投票给你们所有人-我的声誉还没有达到15.2013-05-22 03:24:58.990幸运键[6676:c07]-[MapViewController reverseGeocode:]:未识别的选择器发送到实例0x10d6d290 2013-05-22 03:24:58.991幸运键[6676:c07]***由于未捕获的异常“NSInvalidArgumentException”终止应用程序,原因:'-[MapViewController reverseGeocode:]:发送到实例0x10d6d290'***的无法识别的选择器第一次抛出调用堆栈:()libc++abi.dylib:terminate调用抛出一个它编译好的异常…谢谢你的回复!非常感谢。啊,我的错。去掉选择器的尾随冒号。我将编辑我的答案以反映这一点。因为你的方法不接受对象,所以这是不需要的。它现在运行时不会崩溃,但它仍然只会运行ows“%@”正如我所说的,我真的认为这是MKAnnotationPoint.Subtitle/title属性的一个限制。我认为它们不能接受变量,只能接受纯文本。这是我的新手总结:)非常感谢你的帮助,尽管Psiticosis。我还没有足够的声誉或任何东西来支持你,但我对这个网站感到惊讶你们真是帮了大忙!顺便说一句,我将使用你们版本的函数调用,因为下面的解决方案已经解决了这个问题,这比我的版本更正确。非常感谢!我会在可能的时候回来投票:)