【问题标题】:Learning iOS MapKit - multiple pins on map from NSDictionary学习 iOS MapKit - 来自 NSDictionary 的地图上的多个图钉
【发布时间】:2012-03-22 20:34:16
【问题描述】:

尝试获取包含我所有位置、标题和字幕的 NSSDictionary 并枚举它们并将它们添加到我的 mapView 中。第一部分看起来不错;我阅读了 Plist 并且可以访问各个部分。我遇到的问题是遍历枚举。

请求标题的 NSLog 正确地将该属性报告给控制台。

不过,最后我没有看到别针。另外,在NSLog调用之后:

NSLog(@"My %i annotations are: %@", self.mapView.annotations.count, self.mapView.annotations);

我得到回应:

"My 1 annotations are: (
    "<MKPointAnnotation: 0xd0151f0>" )

这似乎只是 one 用于 one 注释的内存位置。 叹息

我确定我在做一些简单而错误的事情,但如果有任何帮助,我将不胜感激。我整天都在搜索,在这里看到了一些似乎是答案的东西,但在这个特定的结构中没有任何帮助我的东西。

提前谢谢你。

以下代码在主视图控制器中触发,第一个看到的方法在 ViewDidLoad 结束时调用。

-(void) loadUpLocations{

NSPropertyListFormat format;
NSString *errorDesc=nil;

//make path to plist in bundle
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"locations2" ofType:@"plist"];
//fill data object with contents of file
NSData *myData = [[NSData alloc] initWithContentsOfFile:plistPath];
myLocations=[NSPropertyListSerialization propertyListFromData:myData 
                                             mutabilityOption:NSPropertyListMutableContainersAndLeaves 
                                                       format:&format
                                             errorDescription:&errorDesc];
if (!myLocations) {
    NSLog(@"Error reading Plist: %@ format: %@", errorDesc,format);
}

myAnnotation=[[MKPointAnnotation alloc]init];

for (NSString *loc in myLocations) {
    NSDictionary *value =[myLocations objectForKey:loc];

    //NSLog(@"Loc is equal to: %@",loc);

    //loop through and make annotations

    myAnnotation.coordinate = CLLocationCoordinate2DMake(
                                      [[value objectForKey:@"latitude"] doubleValue],
                                      [[value objectForKey:@"longitude"] doubleValue]);
    myAnnotation.title = [value objectForKey:@"title"];
    myAnnotation.subtitle = [value objectForKey:@"subtitle"];

    NSLog(@"%@",myAnnotation.title);
    [self.mapView addAnnotation:myAnnotation];

}

NSLog(@"My %i annotations are: %@", self.mapView.annotations.count, self.mapView.annotations);
}

- (IBAction)zoomToLocation:(id)sender {


//set center for zoom
MKCoordinateRegion region;
region.center=CLLocationCoordinate2DMake(33.75070416856952, -84.37034368515015);;

//set level of zoom
MKCoordinateSpan span;
span.latitudeDelta=.03;
span.longitudeDelta=.03;
region.span=span;


[mapView setRegion:region animated:YES];
}

// This gets called every time an annotation is in the map view

- (MKAnnotationView *)mapView:mapView viewForAnnotation:annotation{
NSLog(@"in annotationsView");


MKPinAnnotationView *myPins=[[MKPinAnnotationView alloc]
                                 initWithAnnotation:annotation 
                                 reuseIdentifier:@"myPins"];
myPins.animatesDrop=YES;
return myPins;
}

【问题讨论】:

    标签: ios mapkit


    【解决方案1】:

    这一行:

    myAnnotation=[[MKPointAnnotation alloc]init];
    

    当前位于创建注释的for 循环之外,因此只有一个对象被实例化,并且您不断修改该实例。

    将该线移到for 循环内(例如,在设置坐标的线上方)。


    此外,如果您不使用 ARC,请在 addAnnotation 行之后添加 [myAnnotation release];

    【讨论】:

    • 是的,我正在使用 ARC。当我将初始化放在循环中时,我一直收到错误消息。它抱怨实例被过早释放。我会再试一次,并报告实际错误。感谢观看。
    • 这里是错误: MKPointAnnotation 类的实例 0x6d2f2f0 已被释放,而键值观察者仍向其注册。观察信息被泄露,甚至可能被错误地附加到其他对象上。在 NSKVODeallocateBreak 上设置断点以在调试器中停止。这是当前的观察信息: ( Context: 0x0, Property: 0x6d2d5e0>
    • 那是一个单独的问题。当纬度或经度值无效时,可能会发生该错误。设置 myAnnotation.coordinate 后,使用NSLog(@"lat = %f, long = %f", myAnnotation.coordinate.latitude, myAnnotation.coordinate.longitude); 记录它。确保纬度在 -90 到 90 之间,经度在 -180 到 180 之间。
    • 数字看起来还可以。一个奇怪的地方是有 6 个位置,但最后的 NSLog 只报告 5 个 Annotations。
    • 这让我找到了正确的答案。我的 Plist 的经纬度颠倒了。让这成为我们所有人的一个教训。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多