1

以下代码返回“未选择注释” CalloutAccessories 委托有助于确定选择了哪个注释。我的代码如下。有任何想法吗)?提前致谢

“删除视图控制器.m”

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

}

“视图控制器.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] 应该是在条件之后出现的!下面的代码工作正常!

“删除视图控制器.m”

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

}

“视图控制器.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) 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 回答
0

尝试

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

假设您正在尝试访问mapView.selectedAnnotations.

于 2012-10-15T01:59:56.067 回答