我的问题是我连接了与委托的连接,但它没有中断其委托方法。我想获取我的注释的地址并在注释视图中显示它。我成功获取了地址,但mapview加载需要时间,这就是我使用NsConnection方法的原因。如果我犯了任何错误,请帮助我。
CLLocationCoordinate2D coordinate;
coordinate.latitude = 28.6667;
coordinate.longitude = 77.2167;
clusterMap.region = MKCoordinateRegionMakeWithDistance(coordinate, 5000, 5000);
self.blocks = 4;
self.minimumClusterLevel = 100000;
// super.delegate = self;
zoomLevel = clusterMap.visibleMapRect.size.width * clusterMap.visibleMapRect.size.height;
NSMutableArray *pins = [[NSMutableArray alloc]init];
for(int i =0 ; i < 50; i++)
{
CGFloat latDelta = rand()*0.125/RAND_MAX - 0.02;
CGFloat lonDelta = rand()*0.130/RAND_MAX - 0.08;
newCoord.latitude = coordinate.latitude+latDelta;
newCoord.longitude = coordinate.longitude+lonDelta;
NSString *urlString = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=false",newCoord.latitude, newCoord.longitude];
// NSLog(@"Is%@ main thread", ([NSThread isMainThread] ? @"" : @" NOT"));
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
[connection start];
customannView = [[AnnotationView alloc]initWithLatitude:newCoord.latitude andLongitude:newCoord.longitude];
// customannView.title = [NSString stringWithFormat:@"Pin %i",i];
customannView.title =locationString;
customannView.subtitle = [NSString stringWithFormat:@"Pin %i",i];
customannView.coordinate = newCoord;
customannView.imgName = @"bookmark1.png";
[pins addObject:customannView];
[connection cancel];
}
[self addAnnotations:pins];
UILongPressGestureRecognizer *dropPin = [[UILongPressGestureRecognizer alloc] init];
[dropPin addTarget:self action:@selector(handleLongPress:)];
dropPin.minimumPressDuration = 0.5;
[clusterMap addGestureRecognizer:dropPin];
}
// [self performSelector:@selector(mapViewWillStartLoadingMap:) withObject:nil afterDelay:0.01];
UILongPressGestureRecognizer *dropPin = [[UILongPressGestureRecognizer alloc] init];
[dropPin addTarget:self action:@selector(handleLongPress:)];
dropPin.minimumPressDuration = 0.5;
[clusterMap addGestureRecognizer:dropPin];
}
#pragma mark NSURLConnection Delegate Methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
// A response has been received, this is where we initialize the instance var you created
// so that we can append data to it in the didReceiveData method
// Furthermore, this method is called each time there is a redirect so reinitializing it
// also serves to clear it
[responseData setLength:0];
NSLog(@"Response :: %@",responseData);
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Append the new data to the instance variable you declared
[responseData appendData:data];
}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse*)cachedResponse {
// Return nil to indicate not necessary to store a cached response for this connection
return nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// The request is complete and data has been received
// You can parse the stuff in your instance variable now
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// The request has failed for some reason!
// Check the error var
}
答案 0 :(得分:1)
你为什么要这样做
[connection cancel];
因为它在命中委托之前取消了该过程。移除该行并且代码将起作用
答案 1 :(得分:0)
您的方法的难点在于您正在产生一些NSURLConnections,然后尝试使用一个委托来处理。
为了确定哪个连接和响应数据与委托方法中的纬度/经度和响应数据对象相关联,您需要从连接获取原始请求,然后解析包含该信息的URL。至少这很麻烦。
我建议创建一个类(或使用第三方库),它封装一个NSURLConnection请求,响应数据和可能的其他状态信息。有目的地,这个类有一个完成块,可以很容易地将来自调用站点的纬度/经度和响应数据与已完成连接的每个完成处理程序相关联。