草庐IT

ios - 向 map 添加多个注释但都显示一些相同的数据

coder 2023-09-21 原文

更新 2:

这是 map 中的现有代码,但好像注释与图钉一起乱序了。一次 pin 是绿色的,下一次我运行它时,同一个 pin 是红色的。脱节从何而来?

-(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;
}

更新:

我现在看到问题是 self.theObject 被设置了一次,所以它只是从中提取数据,用于注释未处理的所有内容。我想现在的问题是,我怎样才能让它显示每个对象而不是最后一组?

我的应用程序使用 Parse.com 和 PFObjects。该应用程序查询 PFObject 以获取行中每个条目的所有数据,提取该条目给定位置的坐标以及各种数据,并为每个条目添加注释。当我为此设置 MKAnnotationView 以便我可以在 Apple 通常允许您轻松执行的操作之外拥有多个彩色图钉时,我遇到了问题。注释的图像和一些数据将每个引脚设置为最后一个调用的引脚。标题和副标题都很好,但颜色或标注图像不是。这是我的代码,这里出了什么问题?

-(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;
}

最佳答案

子类 MKPointAnnotation 例如 MyPointAnnotation 添加一个属性 result

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

viewWillAppear:中修改这些行

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

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

[mapView addAnnotation:myAnnotation];

viewForAnnotation:委托(delegate)方法中得到这样的结果

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

关于ios - 向 map 添加多个注释但都显示一些相同的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39235505/

有关ios - 向 map 添加多个注释但都显示一些相同的数据的更多相关文章

  1. ruby - 我需要将 Bundler 本身添加到 Gemfile 中吗? - 2

    当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/

  2. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  3. ruby-on-rails - Rails 编辑表单不显示嵌套项 - 2

    我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib

  4. ruby-on-rails - 在 Ruby 中循环遍历多个数组 - 2

    我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代

  5. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  6. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  7. ruby - 将 Bootstrap Less 添加到 Sinatra - 2

    我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它

  8. ruby - 续集在添加关联时访问many_to_many连接表 - 2

    我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以

  9. ruby - 多个属性的 update_column 方法 - 2

    我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2

  10. ruby-on-rails - 使用 Sublime Text 3 突出显示 HTML 背景语法中的 ERB? - 2

    所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择

随机推荐