0
votes

I have this code:

-(void)handleLongPressGesture:(UIGestureRecognizer*)sender {

NSNumber* existingpoints = [[NSNumber alloc]init];


 existingpoints =[NSNumber numberWithInt:0]; 


// This is important if you only want to receive one tap and hold event
if (sender.state == UIGestureRecognizerStateEnded)
{
    [self.mapView removeGestureRecognizer:sender];
}
else {

    do {
        int z = 1;
        existingpoints =[NSNumber numberWithInt:z];

        // Here we get the CGPoint for the touch and convert it to latitude and longitude coordinates to display on the map
        CGPoint point = [sender locationInView:self.mapView];
        CLLocationCoordinate2D locCoord = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
        // Then all you have to do is create the annotation and add it to the map

        MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init]; annotationPoint.coordinate = locCoord;



        NSString *latitude = [[NSString alloc] initWithFormat:@"%f",locCoord.latitude];


        NSString *longitude = [[NSString alloc] initWithFormat:@"%f", locCoord.longitude];


        annotationPoint.title = @"Event";
        annotationPoint.subtitle = [NSString stringWithFormat:@"%@ & %@", latitude, longitude];

        [mapView addAnnotation:annotationPoint];


        [[NSUserDefaults standardUserDefaults]setObject:latitude forKey:@"FolderLatitude"];
        [[NSUserDefaults standardUserDefaults]setObject:longitude forKey:@"FolderLongitude"];


    } while ([existingpoints intValue] == 0);

        }
}

...but the problem is that when I hold, and then drag more than one pin is added. I want to add only one pin. So I tried the do method but it doesn't work. I can't understand, because when I executed the code I turn the value of the NSNumber to 1, and the while says = 0 to run the code.

Please Help!!

1

1 Answers

0
votes

Your current code is prone to have quite a number of memory leaks. For example:

NSNumber* existingpoints = [[NSNumber alloc] init];
existingpoints = [NSNumber numberWithInt:0];

Is leaking because you leave the first instance of existingpoints with retain value of 1 and not freeing it anywhere. Unless you're using ARC. You can optimize the above code with just one instruction:

NSNumber* existingpoints = [NSNumber numberWithInt:0];

And retain it if you need to keep it somewhere (but i belive it's not the case).

Analyzing the code, I'd recommend NOT to use existingpoints as an NSNumber. Use an NSInteger instead (which is not an object, just a typedef to long).

Here's my rewritten code:

-(void)handleLongPressGesture:(UIGestureRecognizer*)sender {
    NSInteger existingpoints = 0;

    // This is important if you only want to receive one tap and hold event
    if (sender.state == UIGestureRecognizerStateEnded) {
        [self.mapView removeGestureRecognizer:sender];
    }
    else {
        do {
            int z = 1;
            existingpoints = z;

            // Here we get the CGPoint for the touch and convert it to latitude and longitude coordinates to display on the map
            CGPoint point = [sender locationInView:self.mapView];
            CLLocationCoordinate2D locCoord = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];

            // Then all you have to do is create the annotation and add it to the map
            MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
            annotationPoint.coordinate = locCoord;

            NSString *latitude = [NSString stringWithFormat:@"%f",locCoord.latitude];
            NSString *longitude = [NSString stringWithFormat:@"%f", locCoord.longitude];

            annotationPoint.title = @"Event";
            annotationPoint.subtitle = [NSString stringWithFormat:@"%@ & %@", latitude, longitude];

            [mapView addAnnotation:annotationPoint];

            [[NSUserDefaults standardUserDefaults] setObject:latitude forKey:@"FolderLatitude"];
            [[NSUserDefaults standardUserDefaults] setObject:longitude forKey:@"FolderLongitude"];

            [annotationPoint release]; // Remove this if you're using ARC.
        } while (existingpoints == 0);
    }
}

Note that I've also changed the code for creating latitude and longitude for not to create any memory leaks when using ARC.

EDIT: Further analyzing your code, I don't see why this method would be dropping two pins at once. Maybe you could check if your method is not being called twice?

More: Why do you have a do/while loop if you just want it to run once? (but maybe you're just paving your ground to further ahead)