2016-08-30 12 views
8

CẬP NHẬT 2:Thêm nhiều chú thích vào bản đồ nhưng tất cả hiển thị một số dữ liệu giống nhau

Đây là mã hiện có trong bản đồ, nhưng nó giống như các chú thích lấy hết thứ tự bằng các ghim. Một lần một ghim sẽ có màu xanh lục, lần sau tôi chạy nó, cùng một ghim có màu đỏ. Sự ngắt kết nối đến từ đâu?

-(void) viewWillAppear:(BOOL)animated { 
    self.annotationArray = [[NSMutableArray alloc] init]; 

CLLocationCoordinate2D coord = {.latitude = 15.8700320, .longitude = 100.9925410}; 
MKCoordinateSpan span = {.latitudeDelta = 3, .longitudeDelta = 3}; 
MKCoordinateRegion region = {coord, span}; 
[mapViewUI setRegion:region]; 
    PFQuery *query = [PFQuery queryWithClassName:@"Share"]; 
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
     if (!error) { 
         self.mapViewData = objects; 
         for (int i=0;i<[objects count];i++) 
      { 
       // Question * q = [[Question alloc]init]; 

       PFObject * obj = [self.mapViewData objectAtIndex:i]; 
       NSLog(@"%@", obj); 
       self.theObject = obj; 

       NSString *string = obj[@"WhereAt"]; 


       NSArray *stringArray = [string componentsSeparatedByString: @","]; 

       CLLocationDegrees myLatitude = [[stringArray objectAtIndex:0] doubleValue]; 
       CLLocationDegrees myLongitude = [[stringArray objectAtIndex:1] doubleValue]; 
       CLLocationCoordinate2D coord2 = {.latitude = myLatitude, .longitude = myLongitude}; 
       NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
       dateFormatter.dateFormat = @"MMM dd, yyyy"; 

       MKPointAnnotation *annotation2 = [[MKPointAnnotation alloc] init]; 
       [annotation2 setCoordinate:coord2]; 
       [annotation2 setTitle:obj[@"FamilyName"]]; 
       [annotation2 setSubtitle:obj[@"Result"]]; 
       [mapViewUI addAnnotation:annotation2]; 


      } 


     } else { 
      // Log details of the failure 
      NSLog(@"Error: %@ %@", error, [error userInfo]); 
     } 


    }]; 

} 
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { 
    if (annotation == mapViewUI.userLocation) 
    { 
     return nil; 
    } 
    else 
    { 
    NSUInteger index = [[mapView annotations] indexOfObject:annotation]; 
    PFObject *objectForCurrentAnnotation = [self.mapViewData objectAtIndex:index]; 
    MKPinAnnotationView *annotationView = [[MKPinAnnotationView alloc] init]; 

    annotationView.enabled = YES; 
    annotationView.canShowCallout = YES; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    dateFormatter.dateFormat = @"MMM dd, yyyy"; 
    NSString *theResult = objectForCurrentAnnotation[@"Result"]; 
    NSLog(@"Result is %@", theResult); 

    if ([theResult isEqualToString:@"Accepted Bible"]) { 
     NSLog(@"Accepted"); 
     annotationView.pinTintColor = [UIColor greenColor]; 

    } 
    else if ([theResult isEqualToString:@"Requested Different Material"]) { 
     NSLog(@"Different"); 

     annotationView.pinTintColor = [UIColor blackColor]; 

    } 
    else if ([theResult isEqualToString:@"Not Home"]) { 
     NSLog(@"Not Home"); 

     annotationView.pinTintColor = [UIColor yellowColor]; 

    } 
    else if ([theResult isEqualToString:@"Rejected Bible"]) { 
     NSLog(@"Rejected"); 

     annotationView.pinTintColor = [UIColor redColor]; 

    } 



    return annotationView; 
    } 

    return nil; 
} 

UPDATE:

Tôi thấy bây giờ mà vấn đề là self.theObject được thiết lập một thời gian, vì vậy nó chỉ kéo dữ liệu từ đó cho tất cả mọi thứ không được xử lý bằng cách chú thích. Tôi đoán vấn đề bây giờ là, làm thế nào tôi có thể làm cho nó để hiển thị cho mỗi đối tượng thay vì bất cứ điều gì một bộ cuối cùng?

Ứng dụng của tôi sử dụng Parse.com và PFObjects. Ứng dụng truy vấn PFObject để nhận tất cả dữ liệu cho mỗi mục trong hàng, kéo ra các tọa độ cho một vị trí nhất định cho mục nhập đó, cùng với các bit dữ liệu khác nhau và thêm chú thích cho từng mục. Khi tôi thiết lập MKAnnotationView cho điều này để tôi có thể có nhiều chân màu bên ngoài những gì Apple thường cho phép bạn dễ dàng làm, tôi chạy vào các vấn đề. Hình ảnh và một số dữ liệu cho chú thích đặt từng chốt thành bất kỳ thứ gì được gọi là cuối cùng. Tiêu đề và phụ đề là tốt, nhưng không phải là màu hoặc hình ảnh chú thích. Đây là mã của tôi, những gì đang xảy ra sai ở đây?

-(void) viewWillAppear:(BOOL)animated { 
CLLocationCoordinate2D coord = {.latitude = 15.8700320, .longitude = 100.9925410}; 
MKCoordinateSpan span = {.latitudeDelta = 3, .longitudeDelta = 3}; 
MKCoordinateRegion region = {coord, span}; 
[mapView setRegion:region]; 
    PFQuery *query = [PFQuery queryWithClassName:@"Share"]; 
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
     if (!error) { 
      NSLog(@"YAY"); 
      // The find succeeded. The first 100 objects are available in objects 
      // questionsList = [[NSMutableArray alloc]init]; 
      NSLog(@"Objects%lu", (unsigned long)[objects count]); 

      for (int i=0;i<[objects count];i++) 
      { 
       // Question * q = [[Question alloc]init]; 

       PFObject * obj = [objects objectAtIndex:i]; 
       self.theObject = obj; 

       NSString *string = obj[@"WhereAt"]; 


       NSArray *stringArray = [string componentsSeparatedByString: @","]; 

       CLLocationDegrees myLatitude = [[stringArray objectAtIndex:0] doubleValue]; 
       CLLocationDegrees myLongitude = [[stringArray objectAtIndex:1] doubleValue]; 
       CLLocationCoordinate2D coord2 = {.latitude = myLatitude, .longitude = myLongitude}; 
       NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
       dateFormatter.dateFormat = @"MMM dd, yyyy"; 
       NSString *theResult = obj[@"Result"]; 


       NSString *theDate = [dateFormatter stringFromDate:obj[@"DateVisited"]]; 
       NSString *combined = [[theResult stringByAppendingString:@" on "] stringByAppendingString:theDate]; 
       NSString *theTitle = [[obj[@"FamilyName"] stringByAppendingString:@" "] stringByAppendingString:obj[@"StreetAddress"]]; 

       MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init]; 
       [annotation setCoordinate:coord2]; 
       [annotation setTitle:theTitle]; 
       [annotation setSubtitle:combined]; 

       [mapView addAnnotation:annotation]; 

      } 


     } else { 
      // Log details of the failure 
      NSLog(@"Error: %@ %@", error, [error userInfo]); 
     } 
    }]; 



} 
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { 


    MKPinAnnotationView *annotationView = [[MKPinAnnotationView alloc] init]; 

    annotationView.enabled = YES; 
    annotationView.canShowCallout = YES; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    dateFormatter.dateFormat = @"MMM dd, yyyy"; 
    NSString *theResult = self.theObject[@"Result"]; 
    NSLog(@"Result is %@", theResult); 

    if ([theResult isEqualToString:@"Accepted Bible"]) { 
     // NSLog(@"Accepted"); 
     annotationView.pinTintColor = [UIColor greenColor]; 

    } 
    else if ([theResult isEqualToString:@"Requested Different Material"]) { 
     //NSLog(@"Different"); 

     annotationView.pinTintColor = [UIColor blackColor]; 

    } 
    else if ([theResult isEqualToString:@"Not Home"]) { 
     // NSLog(@"Not Home"); 

     annotationView.pinTintColor = [UIColor yellowColor]; 

    } 
    else if ([theResult isEqualToString:@"Rejected Bible"]) { 
     //NSLog(@"Rejected"); 

     annotationView.pinTintColor = [UIColor redColor]; 

    } 
    //annotationView.image = [UIImage imageNamed:@"arrest.png"];//here we use a nice image instead of the default pins 
    PFFile *thumbnail = self.theObject[@"HousePicture"]; 
    [thumbnail getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) { 

     UIImage *thumbnailImage = [UIImage imageWithData:imageData]; 
     self.theImage = thumbnailImage; 
     //annotationView.image = thumbnailImage; 
     UIView *leftCAV = [[UIView alloc] initWithFrame:CGRectMake(0,0,60,60)]; 
     UIImageView *theImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)]; 
     theImageView.image = self.theImage; 
     annotationView.leftCalloutAccessoryView = leftCAV; 
     [leftCAV addSubview:theImageView]; 
     annotationView.canShowCallout = YES; 

    }]; 


    return annotationView; 


    return nil; 
} 

Trả lời

0

Subclass MKPointAnnotation ví dụ MyPointAnnotation thêm một tài sản result

@interface MyPointAnnotation : MKPointAnnotation 
    @property (nonatomic, strong) NSString *result; 
@end 

Trong viewWillAppear: sửa đổi những dòng này

NSString *theResult = obj[@"Result"]; 

MyPointAnnotation *myAnnotation = [[MyPointAnnotation alloc] init]; 
[myAnnotation setCoordinate:coord2]; 
[myAnnotation setTitle:theTitle]; 
[myAnnotation setSubtitle:combined]; 
[myAnnotation setResult:theResult]; 

[mapView addAnnotation:myAnnotation]; 

Trong viewForAnnotation: phương pháp đại biểu có được kết quả như thế này

MyPointAnnotation *myAnnotation = (MyPointAnnotation *)annotation; 
NSString *theResult = myAnnotation.result; 
0

Bạn có thể giải quyết vấn đề này theo một vài cách khác nhau.

1. MKMapView lớp học có thuộc tính được gọi là chú thích. Bằng cách gọi thuộc tính này, nó sẽ trả về bạn sắp xếp mảng của tất cả các chú thích trên bản đồ của bạn, đó là đồng liên quan với thứ tự dữ liệu của bạn trong mảng ban đầu của bạn. Bằng cách này, bạn chỉ cần lưu trữ tất cả các đối tượng đã tìm nạp từ Parse. Trong giao diện trong điều khiển điểm của bạn chỉ cần thêm một tài sản mới:

@property (nonatomic, string) NSArray *mapViewData; 

Trong phương pháp viewWillAppear của bạn:

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
      if (!error) { 
       self.mapViewData = objects; 
       // here you form your annotations 
      } 
    }]; 

Sau đó chỉ cần kéo dữ liệu này trong phương pháp đại biểu

- (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { 
    NSUInteger index = [[mapView annotations] indexOfObject:annotation]; 
    PFObject *objectForCurrentAnnotation = [self.mapViewData objectAtIndex:index]; 
    // do your customisation 
} 

2. Ghi đè và phân lớp đối tượng MKPointAnnotation.

@interface SuperAnnotation : MKPointAnnotation 

@property (nonatomic, strong) PFObject *dataObject; 

@end 

@implementation SuperAnnotation 

- (void)setDataObject:(PFObject *)dataObject { 
    _dataObject = dataObject; 

    self.title = // calculate your string for title 
    self.subtitle = // and the same for subtitle 
} 

@end 

Và sau đó trong phương pháp đại biểu chỉ có được đối tượng này từ đối tượng chú thích và làm những gì bạn muốn

- (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { 
    PFObject *objectForCurrentAnnotation = ((SuperAnnotation *) annotation).dataObject;  
    // do your customisation 
} 

3. Bạn có thể tạo từ điển sẽ lưu trữ cặp MKAnnotation và PFObject mà bạn cần để tùy biến (không được khuyến nghị cho số lượng lớn dữ liệu).

@property (nonatomic, string) NSMutableDictionary *dataDictionary; 

trong viewWillAppear:

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
     if (!error) { 
      self.dataDictionary = [NSMutableDictionary new]; 
      // here you form your annotations 

      [self.dataDictionary setObject:yourPFObject forKey:yourAnnoationObject]; 
     } 
}]; 

Và sau đó một lần nữa kéo dữ liệu trong đoàn:

- (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { 
    PFObject *objectForCurrentAnnotation = [self.dataDictionary objectForKey:annotation]; 
    // do your customisation 
} 
+0

Tôi đã được ra khỏi thị trấn và vừa trở về để thử Lựa chọn # 1 bạn đã viết . Tuy nhiên, tôi vẫn còn có một số khó khăn. Nó hiện đang hiển thị các màu khác nhau, nhưng nó không phải lúc nào cũng phù hợp với các vị trí chính xác. Ví dụ, tôi bảo nó làm xanh nếu kết quả là Kinh thánh được chấp nhận. Chú thích bản đồ sẽ nói chính xác về phụ đề rằng nó đã được chấp nhận Kinh Thánh, nhưng màu sắc sẽ hiển thị bị từ chối. – user717452

+0

@ user717452 thử phương pháp số 2. –

+0

@ user717452 bạn có ý nghĩa gì về màu bị từ chối? –

Các vấn đề liên quan