【问题标题】:Multiple annotations not showing (Xcode mapview)多个注释未显示(Xcode mapview)
【发布时间】:2012-01-19 16:59:15
【问题描述】:
[super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

NSArray *name = [NSArray arrayWithObjects:@"Marina Bay Sands", @"Sentosa", @"Singapore Polytechnic",nil];
NSArray *description = [NSArray arrayWithObjects:@"Got casino!!", @"Got Universal Studio Singapore!!", @"Best polytechnic in Singapore!",nil];    

self.mapAnnotations = [[NSMutableArray alloc] initWithCapacity:3];

//Set coordinates for pin
CLLocationCoordinate2D location;
location.latitude = (double)1.2822463547298561;
location.longitude = (double) 103.85830879211426;
MapPin *mapPin = [[MapPin alloc] init];
[mapPin setCoordinate:location];
[mapPin setName: [name objectAtIndex:0]];
[mapPin setDescription:[description objectAtIndex:0]];
[self.mapAnnotations addObject:mapPin];

location.latitude = (double) 1.249404;
location.longitude = (double) 103.830321;
[mapPin setCoordinate:location];
[mapPin setName: [name objectAtIndex:1]];
[mapPin setDescription:[description objectAtIndex:1]];
[self.mapAnnotations addObject:mapPin];

location.latitude = (double) 1.309976;
location.longitude = (double) 103.775921;
[mapPin setCoordinate:location];
[mapPin setName: [name objectAtIndex:2]];
[mapPin setDescription:[description objectAtIndex:2]];
[self.mapAnnotations addObject:mapPin];

[self.mapView addAnnotations:self.mapAnnotations];

self.mapView.mapType = MKMapTypeStandard;
[self location];
mapView.showsUserLocation = YES;

嗨,Xcode 编程相当新,这是我在 viewDidLoad 中的代码。我正在尝试显示多个注释,这只是为了尝试。但是从这段代码中,我只能显示 1 个注释,它是我添加到 ma​​pAnnotations 数组中的最后一个对象。

我可以让它工作的唯一方法是初始化 ma​​pPin1ma​​pPin2,而不是全部使用 ma​​pPin我要创建的两个注释。但是这种方式似乎效率很低(如果我错了,请纠正我)而且,我有数百个数据要添加。不可能对它们进行硬编码。

有人可以帮我解决这个问题吗?

谢谢你=)

【问题讨论】:

    标签: ios xcode ipad annotations android-mapview


    【解决方案1】:

    我已将 cmets 添加到您的代码中,以显示真正正在发生的事情。

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    NSArray *name = [NSArray arrayWithObjects:@"Marina Bay Sands", @"Sentosa", @"Singapore 
        Polytechnic",nil];
    NSArray *description = [NSArray arrayWithObjects:@"Got casino!!", @"Got Universal Studio 
        Singapore!!", @"Best polytechnic in Singapore!",nil];    
    
    self.mapAnnotations = [[NSMutableArray alloc] initWithCapacity:3];
    
    //here you create the properties of the pin the first time
    CLLocationCoordinate2D location;
    location.latitude = (double)1.2822463547298561;
    location.longitude = (double) 103.85830879211426;
    
    //here you create the pin
    MapPin *mapPin = [[MapPin alloc] init];
    
    //now you set its properties
    [mapPin setCoordinate:location];
    [mapPin setName: [name objectAtIndex:0]];
    [mapPin setDescription:[description objectAtIndex:0]];
    //now you add it to the array
    [self.mapAnnotations addObject:mapPin];
    
    //now you are changing the properties of `mapPin`
    location.latitude = (double) 1.249404;
    location.longitude = (double) 103.830321;
    [mapPin setCoordinate:location];
    [mapPin setName: [name objectAtIndex:1]];
    [mapPin setDescription:[description objectAtIndex:1]];
    
    //the problem with adding mapPin again here is that you are adding
    //the same object again, just with different properties, so
    //your array still only has one object in it, `mapPin`, but with its updated properties
    [self.mapAnnotations addObject:mapPin];
    
    //you change the properties again here
    location.latitude = (double) 1.309976;
    location.longitude = (double) 103.775921;
    [mapPin setCoordinate:location];
    [mapPin setName: [name objectAtIndex:2]];
    [mapPin setDescription:[description objectAtIndex:2]];
    
    //and the same phenomenon I previously described happens again here
    [self.mapAnnotations addObject:mapPin];
    
    [self.mapView addAnnotations:self.mapAnnotations];
    
    self.mapView.mapType = MKMapTypeStandard;
    [self location];
    mapView.showsUserLocation = YES;
    

    如果您不想在每次将它们添加到数组时创建新的 mapPins,请在每次将图钉添加到数组时尝试此操作:

    代替

    [self.mapAnnotations addObject:mapPin];
    

    试试

    [self.mapAnnotations addObject:[[mapPin copy] autorelease]];
    

    这将添加您修改后的 mapPin 的副本,而不是指向原来所在的内存空间。

    【讨论】:

    • 很高兴我能帮上忙。如果我的回答解决了你的问题,请注明!
    【解决方案2】:

    您正在创建一个mapPin 并重复添加相同的对象。您需要为要添加到地图的每个注释 alloc/init 一个新的注释对象。

    【讨论】:

      【解决方案3】:

      找线索试试

      int howManyAreThere = [self.mapAnnotations count];
      

      在你的 3 添加并断点它以查看实际发生的情况。

      虽然 NSMutableArray 处理重复项,但它不会复制您传递给它的对象。 比这更聪明

      【讨论】:

        【解决方案4】:

        获取数组中的所有 lat long 并使用以下代码

        for (int i = 0; i < [arrListing count]; i++) {
        
        
                    List *obj = [arrList objectAtIndex:i];
                    NSLog(@"Title %@  long:%@  Lat:%@",obj.Title,obj.log,obj.lat);
        
                    CLLocationCoordinate2D annotationCoord;
        
                    annotationCoord.latitude = [obj.lat floatValue];
                    annotationCoord.longitude = [obj.log floatValue];
        
                    MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
                    annotationPoint.coordinate = annotationCoord;
                    annotationPoint.title = obj.Title;
                    annotationPoint.subtitle = obj.log;
                    [mapView addAnnotation:annotationPoint];
                }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-04-06
          • 1970-01-01
          • 1970-01-01
          • 2023-04-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多