2013-02-18 41 views
3

我正试图获取用户点击的具体详细信息。详细信息取自plist文件。对于这个功能,我使用- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view; 使用this.while我点击特定的annotation它从Map提取的位置详细信息为"Pune, Maharashtra, India @ <+18.50565666,+73.85559082> +/- 0.00m",它不检查我的plist values.even在给出这些值作为属性在plist中作为每个latlong location.can有人解决我的问题?正在获取所选地图的详细信息来自plist

我的plist文件的详细信息:

2013-02-18 12:16:56.039 MapView[9407:13d03] { 
contents = "This Fish will be Found Rarely in South Asia"; 
imagesName = "kfish.jpg"; 
latlong = "Bangalore, Karnataka, India @ <+12.98333330,+77.58333330> +/- 0.00m"; 
listName = kFish; 
location = "Bangalore, Karnataka, India"; 
} 
2013-02-18 12:16:56.039 MapView[9407:13d03] { 
contents = "This Fish will be Found Rarely in South Asia"; 
imagesName = "wallfish.png"; 
latlong = "Delhi, India @ <+28.59100531,+77.08826373> +/- 0.00m"; 
listName = WallFish; 
location = "Delhi, India"; 
} 
2013-02-18 12:16:56.039 MapView[9407:13d03] { 
contents = "This Fish will be Found Rarely in South Asia"; 
imagesName = "fish.jpg"; 
latlong = "Chennai, Tamil Nadu, India @ <+13.07983465,+80.24625757> +/- 0.00m"; 
listName = Fish; 
location = "Chennai,TamilNadu,India"; 
} 

2013-02-18 12:16:56.039 MapView[9407:13d03] { 
contents = "This Fish will be Found Rarely in South Asia"; 
imagesName = "bluefish.jpg"; 
latlong = "Mumbai, Maharashtra, India @ <+18.99460885,+72.82287598> +/- 0.00m"; 
listName = BlueFish; 
location = "Mumbai, Maharashtra, India"; 
} 

2013-02-18 12:16:56.040 MapView[9407:13d03] { 
contents = "This Fish will be Found Rarely in South Asia"; 
imagesName = "Rarefish.jpg"; 
latlong = "Chennai, Tamil Nadu, India @ <+13.07983465,+80.24625757> +/- 0.00m"; 
listName = RareFish; 
location = "Chennai,TamilNadu,India"; 
} 

我的编码:

- (void)geocodeRequest 
{ 
CLGeocoder *geocoder = [[CLGeocoder alloc]init]; 
for (int i = 0; i < total; i++) { 
    NSString *address = [allValues objectAtIndex:i] ; 

      [geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) { 
         CLPlacemark *geocodedPlacemark = [placemarks objectAtIndex:0]; 
         MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:geocodedPlacemark.location.coordinate addressDictionary:geocodedPlacemark.addressDictionary]; 

         int zoomLevel=20; 
         MKCoordinateRegion region=MKCoordinateRegionMake(geocodedPlacemark.location.coordinate, MKCoordinateSpanMake(zoomLevel,zoomLevel)); 
         [self.mapView addAnnotation:placemark]; 
         [self.mapView setRegion:region animated:YES]; 
         [self.mapView setZoomEnabled:YES]; 
            }]; 

} 


-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{ 
    total = self.arrayImages.count; 
NSArray *newIndex = [mapView selectedAnnotations]; 
NSLog(@"sel :%@",newIndex); 

    for (index = 0; index < total; index++) { 
     dict = [self.arrayImages objectAtIndex:index]; 
     NSLog(@"%@",dict); 

     if (latlong == newIndex) { 

      CGRect rec = CGRectMake(0, 0, 250, 250); 
      [self setView:[[[UIView alloc] initWithFrame:rec] autorelease]]; 
      [[self view] setBackgroundColor:[UIColor whiteColor]]; 
      UIImage *img = [UIImage imageNamed:[dict objectForKey:@"imagesName"]]; 

      UIImageView *im = [[UIImageView alloc] initWithImage:img]; 
      UILabel *lab1 = [[UILabel alloc] init]; 
      UITextView *detailsView = [[UITextView alloc] init]; 
      lab1.text = [NSString stringWithFormat:[dict objectForKey:@"listName"]]; 
      detailsView.text = [NSString stringWithFormat:[dict objectForKey:@"contents"]]; 
      detailsView.editable = NO; 
      detailsView.bounds = CGRectMake(20, 20, 150, 150); 
      detailsView.frame = CGRectMake(210, 720, 390, 350); 
      detailsView.font = [UIFont systemFontOfSize:24]; 

      lab1.bounds = CGRectMake(20, 20, 150, 150); 
      lab1.frame = CGRectMake(360, 600, 90, 50); 
      lab1.font = [UIFont systemFontOfSize:24]; 
      im.bounds = CGRectMake(10, 10, 50, 50); 
      im.frame = CGRectMake(200, 120, 370, 450); 

     [UIView transitionWithView:[self view] duration:1.5 
          options:UIViewAnimationOptionTransitionCurlUp 
         animations:^ { [[self view] addSubview:im]; 
             [[self view] addSubview:lab1]; 
             [[self view] addSubview:detailsView]; 

     }completion:nil]; 
     } 
    } 


} 

MapView的fine.but工程上取选定的位置只有它没有working.can人帮助我在哪里有我错在哪里??

在此先感谢。

回答

3

在您的didSelectAnnotationView方法中,您似乎正在遍历您的arrayImages,正在寻找其中的latlong == newIndex。一对夫妇的想法:

  1. 使用等于运算符,==比较对象,一般是不审慎的技术。您无法保证地图视图的注释是同一个对象(如果它们的属性使用的是copy,则它不会相同,因为您的对象和它们的地址可能不同)。即使它确实发生了工作,也不是非常可靠的工作方式。

  2. 我不十分如果我按照你的逻辑,无论如何,即使运营商的平等没有工作,因为在geocodeRequest你是一个新的MKPlacemark,从allValues,但didSelectAnnotationView你比较它的arrayImages 。并且didSelectAnnotationView中的if声明正在查看latlongnewIndex(前者我没有看到您设置,后者是一组注释)。这对我来说没有意义。我不明白他们怎么可能匹配。

  3. 但在这之前的点是无关紧要的,因为正确的解决方案是不添加MKPlacemark到地图中,然后通过你的列表中didSelectAnnotationView迭代狩猎比赛,而是与所有创建自己的注释类您希望在用户点击注解视图时能够检索的属性。或者,至少有自己的注释子类,并定义一个属性,您可以使用它来查看数组中的特定细节。这两种方法都有效。但关键是不要尝试遍历寻找匹配的对象,而是定义一个自定义注释,其中包含您需要立即查找的所有内容。

    有关自定义注释的示例,请参阅位置感知编程指南的Defining a Custom Annotation Object部分。一旦你用自己认为需要的所有额外属性定义了你自己的注解子类,你就会(a)改变你的geocodeRequest来创建新注解子类的实例,而不是MKPlacemark;和(b)将您的didSelectAnnotationView更改为从annotation(这是annotationView的财产)中获取必要的数据。这样,它完全消除了遍历可能的注释列表寻找匹配的麻烦。

仅供参考,在另一个S.O. answer,我给关于处理标注,popovers等对选项的一些例子,也许有那么点意思,可能对你有所帮助的背景下,太。我不在那里使用自定义注释,但也许它会给你一些关于你与MapKit集成的想法。


让我来说明一下这种做法。假设我们要注解我们的地图,但想跟踪其他属性。首先,我将定义一个具有我的附加属性的注释自定义类。 (我会从MKPointAnnotation继承这使生活更轻松一点对我来说。)

@interface CustomAnnotation : MKPointAnnotation 

@property (nonatomic) NSUInteger index; 
@property (strong, nonatomic) NSString *property1; 
@property (strong, nonatomic) NSString *property2; 
@property (strong, nonatomic) NSString *property3; 

@end 

现在,当我想我的注释添加到我的地图视图,我会用我的自定义注解类,确保设置我想要的任何属性。你可以保存数字索引数组中,或保存您所关心的实际属性,或两者:

CustomAnnotation *annotation = [[CustomAnnotation alloc] init]; 

// set the MKPointAnnotation standard properties 

annotation.title = item.name; 
annotation.coordinate = item.placemark.coordinate; 

// set my custom properties 

annotation.index = idx; 
annotation.property1 = ...; 
annotation.property2 = ...; 
annotation.property3 = ...; 

// now add the annotation to the map view 

[annotations addObject:annotation]; 

或者,如果你想采取一切一MKPlacemark的各个属性的优势,你可以做以下:

@interface CustomAnnotation : MKPlacemark 

@property (strong, nonatomic) NSString *title; // MKPlacemark doesn't have title, so let's add our own property for that 
@property (nonatomic) NSUInteger index; 
@property (strong, nonatomic) NSString *property1; 
@property (strong, nonatomic) NSString *property2; 
@property (strong, nonatomic) NSString *property3; 

@end 

而且你创建的注释就像你创建一个MKPlacemark,但同样,我们设置自定义属性:

CustomAnnotation *annotation = [[CustomAnnotation alloc] initWithPlacemark:item.placemark]; 

// set my custom properties 

annotation.title = item.name; 
annotation.index = idx; 
annotation.property1 = ...; 

[annotations addObject:annotation]; 

现在,当我本身LECT注释来看,我可以进行相应处理:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view 
{ 
    if ([view.annotation isKindOfClass:[CustomAnnotation class]]) 
    { 
     CustomAnnotation *annotation = view.annotation; 

     // note, I don't have to iterate through any arrays to figure out which 
     // annotation I'm dealing with; I can just look at my custom properties 

     NSLog(@"index = %d; property1 = %@", annotation.index, annotation.property1); 

     // now do whatever you want with that annotation; I could look up the 
     // annotations's details by using the custom `index` property I defined, 
     // or if I defined my annotations to store all of the detail properties 
     // I needed, I can just access them directly 
    } 
} 

顺便说一句,而不是使用didSelectAnnotationView,你可能要采取更标准MKMapView行为,并展示标准标注泡沫,设置正确的标注泡沫附件,然后再处理单击该标注附件:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation 
{ 
    if (![annotation isKindOfClass:[CustomAnnotation class]]) 
     return nil; 

    MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation 
                     reuseIdentifier:@"CustomAnnotationView"]; 
    annotationView.canShowCallout = YES; 
    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 

    return annotationView; 
} 

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 
{ 
    if (![view.annotation isKindOfClass:[CustomAnnotation class]]) 
     return; 

    CustomAnnotation *annotation = view.annotation; 

    // note, I don't have to iterate through any arrays to figure out which 
    // annotation I'm dealing with; I can just look at my custom properties 

    NSLog(@"index = %d; property1 = %@", annotation.index, annotation.property1); 

    // now do whatever you want with that annotation; I could look up the 
    // annotations's details by using the custom `index` property I defined, 
    // or if I defined my annotations to store all of the detail properties 
    // I needed, I can just access them directly 
} 
+0

感谢Rob..i'll现在就来试试吧..上that..i是customAnnotation和item..getting错误 – 2013-02-18 08:36:16

+0

Rob..Confusing可以理解逻辑..但只是在实现它显示错误.. – 2013-02-19 07:15:28

+0

@GreyCode查看https://github.com/robertmryan/MKMapView-custom-annotations的一个工作示例。我从iOS 6.1'MKLocalSearch'中抓取了我的'MKPlacemark'对象,但概念是相同的。 – Rob 2013-02-19 15:35:17