-4

MY Problem is that i connected connection with the delegate but it is not interrupting its delegate method. I want to fetch address for my annotation and display it in annotation view. i successfully fetched the address but the mapview loading takes time, that is why i used the NsConnection Method . Please help me if i have made any mistake.

       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
}
Roma Udernani
  • 21
  • 1
  • 7

2 Answers2

1

why are you doing

    [connection cancel];

because it cancel the process before hitting the delegates.Remove that line and the code will work

Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
0

The difficulty with your approach is that you are spawning a number of NSURLConnections and try then to handle with one delegate.

In order to figure out which connection and response data is associated to which latitude/longitude and response data object in your delegate method you would need to obtain the original request from the connection and then parse the URL which contains that info. This is cumbersome at least.

I would suggest to create a class (or use a third party library) that encapsulates one NSURLConnection request, the response data and possibly other state info. Purposefully, this class has a completion block where it becomes easy to associate the latitude/longitude and response data from the call site with each completion handler of a finished connection.

CouchDeveloper
  • 18,174
  • 3
  • 45
  • 67