1

次のコードは、「注釈が選択されていません」というメッセージを返します。CalloutAccessoriesデリゲートは、選択された注釈を特定するのに役立ちます。私のコードは以下の通りです。何か案は)?前もって感謝します

「RemoveViewController.m」

- (IBAction)removePin:(id)sender {
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[[appDelegate viewController] removeAnno];
[self.navigationController popViewControllerAnimated:YES];

}

「ViewController.m」

- (void)mapView:(MKMapView *)mapView 
 annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {

    if ([(UIButton*)control buttonType] == UIButtonTypeDetailDisclosure){
        // Do your thing when the detailDisclosureButton is touched
        RemoveViewController *settingAnnotation = [[RemoveViewController alloc]initWithNibName:@"RemoveViewController" bundle:nil];
        Annotation *annotation1;
        annotation1 = view.annotation;
        settingAnnotation.title = annotation1.title;
        settingAnnotation.subtitle = annotation1.subtitle;
        [self.navigationController pushViewController:settingAnnotation animated:YES];
    } 

}

- (void)removeAnno{

    [mapView removeAnnotations: mapView.selectedAnnotations];
    if (mapView.selectedAnnotations.count == 0)
    {
        NSLog(@"no annotation selected");
    }
    else
    {
        id<MKAnnotation> ann = [mapView.selectedAnnotations objectAtIndex:0];
        CLLocationCoordinate2D coord = ann.coordinate;
        NSLog(@"lat = %f, lon = %f", coord.latitude, coord.longitude);
    }
}
4

4 に答える 4

1

問題が修正されました。[mapView removeAnnotations:mapView.selectedAnnotations]は、条件の後に来るはずです!以下のコードは正常に機能しています!

「RemoveViewController.m」

- (IBAction)removePin:(id)sender {
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[[appDelegate viewController] removeAnno];
[self.navigationController popViewControllerAnimated:YES];

}

「ViewController.m」

- (void)mapView:(MKMapView *)mapView 
 annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {

    if ([(UIButton*)control buttonType] == UIButtonTypeDetailDisclosure){
        // Do your thing when the detailDisclosureButton is touched
        RemoveViewController *settingAnnotation = [[RemoveViewController alloc]initWithNibName:@"RemoveViewController" bundle:nil];
        Annotation *annotation1;
        annotation1 = view.annotation;
        settingAnnotation.title = annotation1.title;
        settingAnnotation.subtitle = annotation1.subtitle;
        [self.navigationController pushViewController:settingAnnotation animated:YES];
    } 

}

- (void)removeAnno{


    if (mapView.selectedAnnotations.count == 0)
    {
        NSLog(@"no annotation selected");
    }
    else
    {
        id<MKAnnotation> ann = [mapView.selectedAnnotations objectAtIndex:0];
        CLLocationCoordinate2D coord = ann.coordinate;
        NSLog(@"lat = %f, lon = %f", coord.latitude, coord.longitude);
    }
  [mapView removeAnnotations: mapView.selectedAnnotations];
}
于 2012-10-15T03:22:27.067 に答える
0

selectedAnnotationsはい、配列のインデックス0で注釈(存在する場合)を取得し、そのcoordinateプロパティを読み取ります。

if (mapView.selectedAnnotations.count == 0)
{
    NSLog(@"no annotation selected");
}
else
{
    id<MKAnnotation> ann = [mapView.selectedAnnotations objectAtIndex:0];
    CLLocationCoordinate2D coord = ann.coordinate;
    NSLog(@"lat = %f, lon = %f", coord.latitude, coord.longitude);
}

を呼び出す前に、おそらくこれを行う必要がありますremoveAnnotations

于 2012-10-15T01:58:57.227 に答える
0

試す

CLLocationCoordinate2D coord = [[mapView.selectedAnnotations objectAtIndex:0] coordinate]

の最初のオブジェクトにアクセスしようとしていると仮定しますmapView.selectedAnnotations

于 2012-10-15T01:59:56.067 に答える
0

これにより、住所文字列の座標がState、City、Street、Numberの形式で提供されます。

-(CLLocationCoordinate2D) addressLocation:(NSString*) locationSTR {
NSString *addressM = [NSString stringWithString:locationSTR];

NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", 
                       [addressM stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURL *urlCont = [NSURL URLWithString:urlString];
NSError* error = nil;
NSString *locationString = [NSString stringWithContentsOfURL:urlCont encoding:NSASCIIStringEncoding error:&error];
NSArray *listItems = [locationString componentsSeparatedByString:@","];

double latitude = 0.0;
double longitude = 0.0;
if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) {
    latitude = [[listItems objectAtIndex:2] doubleValue];
    longitude = [[listItems objectAtIndex:3] doubleValue];
}
else {
    //Show error
}
CLLocationCoordinate2D location;
location.latitude = latitude;
location.longitude = longitude;

return location;

}

于 2012-10-15T02:36:32.917 に答える