私はこのコードを持っています:
-(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);
}
}
...しかし、問題は、押したままドラッグすると、複数のピンが追加されることです。ピンを1つだけ追加したい。だから私はdoメソッドを試しましたが、うまくいきません。コードを実行したときに NSNumber の値を 1 にすると、while は = 0 と言ってコードを実行するため、理解できません。
助けてください!!