【问题标题】:I can't add more than 1 annotation to MKMapview :|我不能向 MKMapview 添加超过 1 个注释:|
【发布时间】:2011-07-04 03:37:59
【问题描述】:

所以我把头发拉到这个上面。我一直在追踪我的应用程序中的一个错误。最初,我试图从数据库中加载长/纬度坐标并执行 for 循环,将每个注释添加到地图视图中。这看起来很简单,但是由于某种原因,当我尝试在循环中添加注释时,它最后只会显示 1 个注释。

所以,使用下面的代码,我决定尝试简单地向 MKMapView 添加两个注释,以确保我可以首先做到这一点,但它不起作用!

[NewAnnotation setLongitude:-104.6200448];
[NewAnnotation setLongitude:50.4908343];
NewAnnotation *newAnnotation = [[NewAnnotation alloc] init];
[self.mapAnnotations insertObject:newAnnotation atIndex:0];
[newAnnotation release];

[CelebsAnnotation setLongitude:-90.6200448];
[CelebsAnnotation setLatitude:51.4908343];
CelebsAnnotation *celebsAnnotation = [[CelebsAnnotation alloc] init];
[self.mapAnnotations insertObject:celebsAnnotation atIndex:1];
[celebsAnnotation release];

[self.mapView addAnnotations:mapAnnotations];

如果我只将其中一个添加到 mapAnnotations 数组中,注释会显示在它们的正确位置(如果我只添加 CelebsAnnotation,我必须将索引调整为 0),但是当我尝试添加两者时,它们会显示在地图上的同一位置!?关于为什么会发生这种情况的任何想法?我很困惑和沮丧..

【问题讨论】:

  • 您似乎将setLongitudesetLatitude 作为类方法。他们在做什么?你是如何定义init 方法的?
  • 我没有定义 init 方法,实际上现在我查看了我的自定义注释类,我在 .h 中没有坐标属性,所以我很困惑为什么它甚至甚至使用一个注释。我将进一步研究如何正确实现自定义注释类,因为这可能是我的问题。
  • -(void)setLongitude:(double)lon { Clongitude = lon; }
  • Clongitude 是什么?它是静态变量吗?如何实现longitude 实例方法?
  • Clongtitude 是一个双精度值,我不确定您要询问的是哪种经度实例方法。我认为只需要@property(非原子,只读)CLLocationCoordinate2D 坐标;我还实现了这个函数来设置坐标 - (CLLocationCoordinate2D)coordinate; { CLLocationCoordinate2D 坐标; theCoordinate.latitude = 纬度; theCoordinate.longitude = 经度;返回坐标; }

标签: iphone objective-c mkmapview


【解决方案1】:

你的问题来了,

- (CLLocationCoordinate2D)coordinate; { 
    CLLocationCoordinate2D theCoordinate; 
    theCoordinate.latitude = Clatitude; 
    theCoordinate.longitude = Clongitude; 
    return theCoordinate; 
} 

每次请求coordinate 时,您都会创建一个新的CLLocationCoordinate2D,并且您将其设置为静态变量Clongitude & Clatitude。由于这些是静态变量,您将为CelebsAnnotation 的所有实例返回相似的CLLocationCoordinate2D 对象。所以基本上所有的实例都会有相同的坐标。

您应该有一个init.. 方法,该方法接受CLLocationCoordinate2D 对象并将其设置为实例变量。例如,查看this tutorial

你最终会做这样的事情,

CLLocationCoordinate2D coordinate;
coordinate.longitude = -90.6200448;
coordinate.latitude = 51.4908343;
CelebsAnnotation *celebsAnnotation = [[CelebsAnnotation alloc] initWithCoordinate:coordinate];
[self.mapAnnotations insertObject:celebsAnnotation atIndex:1];
[celebsAnnotation release];

【讨论】:

    【解决方案2】:

    这里,这段代码从数据库中获取纬度和经度的值,并在地图中放置注释:

                while (sqlite3_step(statement)==SQLITE_ROW)
                {
                    char *iTtl=(char *) sqlite3_column_text(statement, 1);
                    trackedImageTitle=[NSString stringWithUTF8String:iTtl];
                    char *iLat=(char *) sqlite3_column_text(statement, 2);
                    trackedLocationLattitude=[NSString stringWithUTF8String:iLat];
                    //NSLog(@"lat=%@",trackedLocationLattitude);
                    char *iLon=(char *) sqlite3_column_text(statement, 3);
                    trackedLocationLongitude=[NSString stringWithUTF8String:iLon];
                    //NSLog(@"lon=%@",trackedLocationLongitude);    
                    char *lcn=(char *) sqlite3_column_text(statement, 4);
                    trackedLocation=[NSString stringWithUTF8String:lcn];    
                    int iId=sqlite3_column_int(statement, 0);
                    NSString *iIdS =[NSString stringWithFormat:@"%d", iId];
                    [allImageIds addObject:[NSString stringWithFormat:@"%@",iIdS]];
                    //theImage=iId;
                    //NSLog(@"the image=%d, iId= %d",theImage,iId); 
                    location.latitude=[trackedLocationLattitude floatValue];
                    location.longitude=[trackedLocationLongitude floatValue];   
                    addAnnotation = [[MapViewAnnotation alloc] initWithLocation:location withTitle:[NSString stringWithFormat:@"Tuscany"] withSubTitle:[NSString stringWithFormat:@"Italy"] withImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",iId]]];
                    addAnnotation.mTitle=[NSString stringWithFormat:@"%@",trackedImageTitle];
                    addAnnotation.mSubTitle=[NSString stringWithFormat:@"%@",trackedLocation];
                    ////NSLog(@"%@",addAnnotation.mTitle);
                    [mapView addAnnotation:addAnnotation];
                }
    

    mapViewMKMapView 的实例。

    【讨论】:

    • 感谢您发布此代码,但是我已经编写了所有连接到我的 MySQL 数据库并解析包含注释的 JSON 响应的代码。
    • 这不是关于 db 连接,而是关于 while 循环,它获取不同的值并将 em um 放入值中,这会导致删除多个注释
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多