-4

我的问题是我与委托建立了连接,但它没有中断其委托方法。我想为我的注释获取地址并将其显示在注释视图中。我成功获取了地址,但地图视图加载需要时间,这就是我使用 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
}
4

2 回答 2

1

你为什么这么做

    [connection cancel];

因为它在击中代表之前取消了该过程。删除该行并且代码将起作用

于 2013-07-05T12:07:48.410 回答
0

您的方法的困难在于您正在产生许多 NSURLConnections 并尝试与一个委托进行处理。

为了确定哪些连接和响应数据与委托方法中的哪个纬度/经度和响应数据对象相关联,您需要从连接中获取原始请求,然后解析包含该信息的 URL。至少这很麻烦。

我建议创建一个类(或使用第三方库)来封装一个 NSURLConnection 请求、响应数据和可能的其他状态信息。有意地,这个类有一个完成块,可以很容易地将来自呼叫站点的纬度/经度和响应数据与完成连接的每个完成处理程序相关联。

于 2013-07-05T12:31:15.390 回答