【问题标题】:Get object properties from mapView.SelectedAnnotations从 mapView.SelectedAnnotations 获取对象属性
【发布时间】:2011-01-25 18:59:20
【问题描述】:

我有一个 mapView 和 Annotation 视图,它根据代表物理分支的一系列对象的长/纬度在区域上放置图钉。我有一个注释的标注,提示打开共享应用程序地图以从他们的位置向他们提供方向。但是,我不知道如何从分支对象/注释中获取地址并在何时传递它CalloutAccessoryControlTapped 触发:

分支对象:


CLLocationCoordinate2D newCord;
newCord.latitude = 34.0038;
newCord.longitude = -84.0965;
corp = [[LocationwithAnnotation alloc] initWithCoordinate:newCord];
corp.mTitle = @"Corp Office";
corp.mSubTitle = @"123 Main Street, Duluth GA";
[mapView addAnnotation:corp];
[corp release];

newCord.latitude = 33.0038;
newCord.longitude = -84.3965;
uga = [[LocationwithAnnotation alloc] initWithCoordinate:newCord];
uga.mTitle = @"uga Office";
uga.mSubTitle = @"123 Main Street, Atlanta GA";
[mapView addAnnotation:uga];
[corp release];

这些注释被添加到地图中,然后当标注被点击时,我希望能够将它们推送到带有所选注释(例如公司)副标题的地图应用程序。

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
 MKPinAnnotationView *annView=[[MKPinAnnotationView alloc]  initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
annView.pinColor = MKPinAnnotationColorGreen;
annView.animatesDrop=TRUE;
annView.canShowCallout = YES;
annView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annView.calloutOffset = CGPointMake(-5, 5);
return annView;
}

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
 NSLog(@"%@", mapView.selectedAnnotations);
 UIAlertView *alert = [[UIAlertView alloc]
 initWithTitle:@"Directions"
 message:@"Get directions to this branch?"
 delegate:self
 cancelButtonTitle:@"No"
 otherButtonTitles: @"Yes, please", nil];
 [alert show];
 [alert release];

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
MKUserLocation *loc = mapView.userLocation.location; 
double latitude = loc.location.coordinate.latitude;
NSString *lat = [NSString stringWithFormat:@"%f",latitude];
double longitude = loc.location.coordinate.longitude;
NSString *lon = [NSString stringWithFormat:@"%f",longitude];
NSString *base = @"http://maps.google.com/maps?";
NSString *dest = [@"daddr=" stringByAppendingString: mapView.SelectedAnnotations; // Doesn't work.

我的 NSLog 唯一转储的只是 SelectedAnnotation 的内存地址。那么我怎样才能从中获取对象呢?

【问题讨论】:

    标签: iphone annotations mapkit


    【解决方案1】:
    for(YourMKAnnotationProtocolClass *object in _mapView.selectedAnnotation){
    
            NSLog("%@", object.title); // or you can access other information you've defined
     }
    

    【讨论】:

      【解决方案2】:

      如果你坚持在这里使用 AlertView,而不是直接使用 calloutAccessoryControlTapped,我只需将所选 Annotation 的副标题添加到 alertView 的消息中,然后在 clickedButtonAtIndex 方法中解析它

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-12-04
        • 2011-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多