MapView中的注释

时间:2012-06-22 11:20:34

标签: objective-c xcode

我有这段代码:

-(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);

        }
}

...但问题是,当我按住,然后拖动多个引脚被添加。我想只添加一个引脚。所以我尝试了do方法,但它不起作用。我无法理解,因为当我执行代码时,我将NSNumber的值转为1,而while表示= 0来运行代码。

请帮助!!

1 个答案:

答案 0 :(得分:0)

您当前的代码容易出现大量内存泄漏。例如:

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

是否泄漏是因为您保留existingpoints的第一个实例,其保留值为1,并且不会将其释放到任何位置。除非你使用ARC。您只需一条指令即可优化上述代码:

NSNumber* existingpoints = [NSNumber numberWithInt:0];

如果你需要将它保留在某个地方,请保留它(但我相信并非如此)。

分析代码时,我建议不要将现有点用作NSNumber。请改用NSInteger(不是对象,只是long的typedef。)

这是我重写的代码:

-(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);
    }
}

请注意,我还更改了创建latitudelongitude的代码,以便在使用ARC时不会造成任何内存泄漏。

编辑: 进一步分析你的代码,我不明白为什么这个方法会同时丢失两个引脚。也许你可以检查你的方法是否没有被调用两次?

更多:如果你只想让它运行一次,为什么你有一个do / while循环? (但也许你只是在为更进一步的发展奠定基础)