尝试获取包含我所有位置、标题和字幕的 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 注释的内存位置。 叹息。
我确定我在做一些简单而错误的事情,但如果有任何帮助,我将不胜感激。我整天都在搜索,在这里看到了一些似乎是答案的东西,但在这个特定的结构中没有任何帮助我的东西。
提前谢谢你。
以下代码在主视图 Controller 中触发,第一个看到的方法在 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)zoomToLocationid)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;
}
Best Answer-推荐答案 strong>
这一行:
myAnnotation=[[MKPointAnnotation alloc]init];
当前位于创建注释的 for 循环之外,因此只有一个对象被实例化,而您不断修改该实例。
将该行移到 for 循环内(例如,在设置坐标的行上方)。
此外,如果您不使用 ARC,请在 addAnnotation 行之后添加 [myAnnotation release]; 。
关于ios - 学习 iOS MapKit - 来自 NSDictionary 的 map 上的多个图钉,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/9830074/
|