У меня есть этот код:Аннотации в MapView
-(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 для запуска кода.
Пожалуйста, помогите !!
Я использую ARC .. – Alessandro
, кроме части '[annotationPoint release];' часть, код все равно должен работать нормально на ARC. Как я уже сказал, вы должны проверить, не вызван ли метод дважды. –
Я называю это с помощью этой функции: UILongPressGestureRecognizer * longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget: self action: @selector (handleLongPressGesture :)]; [self.mapView addGestureRecognizer: longPressGesture]; – Alessandro