所以我尝试使用CLGeocoder
,
它是我第一次这样做,但它非常不可靠。有时它会起作用,当我回去时,再试一次。崩溃。有时当我加载它时,出现错误PBRequester
错误,错误为Error Domain=NSURLErrorDomain
,然后出现错误。
这是我的代码:
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:placeName completionHandler:^(NSArray *placemarks, NSError *error) {
//Error checking
CLPlacemark *placemark = [placemarks objectAtIndex:0];
MKCoordinateRegion region;
region.center = [(CLCircularRegion *)placemark.region center];
region.center.latitude = placemark.region.center.latitude;
region.center.longitude = placemark.region.center.longitude;
MKCoordinateSpan span;
double radius = placemark.region.radius / 1000; // convert to km
NSLog(@"Radius is %f", radius);
span.latitudeDelta = radius / 112.0;
region.span = span;
[self.mapView setRegion:region animated:YES];
}];
答案 0 :(得分:0)
在地理编码期间,很多事情都可能发生。由于它是互联网服务,其功能基于互联网可达性和可用频段
幸运的是,CLGeocoder在完成块中返回并出错,假设如果发生错误,则必须正确处理。如果您的应用程序崩溃不是地理编码的错误,因为可能您没有很好地处理错误或情况。
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:placeName completionHandler:^(NSArray *placemarks, NSError *error) {
/* Error checking */
if (error)
{
NSLog(@"The error is %@",error.localizedDescription);
return;
}
if (placemarks.count >1)
{
/* Handle multiple placemarks */
}
else {
/* This is probably the source of your crashes if there is an error placemarks it would be an empty array, but if you ask for an object it will raise an exception for out-of-bounds index */
CLPlacemark *placemark = [placemarks objectAtIndex:0];
MKCoordinateRegion region;
region.center = [(CLCircularRegion *)placemark.region center];
region.center.latitude = placemark.region.center.latitude;
region.center.longitude = placemark.region.center.longitude;
MKCoordinateSpan span;
double radius = placemark.region.radius / 1000; /* convert to km */
NSLog(@"Radius is %f", radius);
span.latitudeDelta = radius / 112.0;
region.span = span;
[self.mapView setRegion:region animated:YES];
}
}];
一些建议:
firstObject
方法(仅限iOS7)