在 Xcode 中从 plist 映射标注?
我正在查看以下问题的讨论,以尝试理解如何从 plist 中读取位置并相应地放置地图视图:
我在理解如何进行这项工作时遇到问题。如果我在注释中进行硬编码,那么我可以让它正常工作,所以我不明白我哪里出了问题。我认为这可能是我访问 .plst 中数据的方式。我的 plist 具有以下结构:
<array>
<dict>
<key>rowData</key>
<array>
<dict>
<key>details</key>
<string>Some minor info</string>
<key>latitude</key>
<strong>53.958756</string>
<key>Location</key>
<string>Location One</string>
<key>lontitude</key>
<string>-1.07937</string>
</dict>
</array>
</dict>
</array>
我尝试使用上面问题的答案中的代码来访问此数据,如下所示 但并不高兴:
-(id)initWithDictionary:(NSDictionary *)dict{
self = [super init];
if(self!=nil){
coordinate.latitude = [[dict objectForKey:@"latitude"] doubleValue];
coordinate.longitude = [[dict objectForKey:@"longitude"] doubleValue];
self.title = [dict objectForKey:@"Location"];
self.subtitle = [dict objectForKey:@"details"];
}
return self;
我的 viewDidLoad 看起来也与我上面引用的其他问题有点不同。它看起来像这样:
- (void)gotoLocation
{
// set location as York, UK
MKCoordinateRegion newRegion;
newRegion.center.latitude = 53.960025;
newRegion.center.longitude = -1.082697;
newRegion.span.latitudeDelta = 0.0012872;
newRegion.span.longitudeDelta = 0.0159863;
[self.map setRegion:newRegion animated:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
//Set up map
self.map.mapType = MKMapTypeStandard; // also MKMapTypeSatellite or MKMapTypeHybrid
[self gotoLocation];
// Get the the plist in application bundle
NSString *path = [[NSBundle mainBundle] pathForResource:@"Places" ofType:@"plist"];
// Retrieve the plists root element array
NSArray *array = [[NSArray alloc] initWithContentsOfFile:path];
NSLog(@"Grabbed locations.plist ok");
if (array) {
NSMutableDictionary* myDict = [NSMutableDictionary dictionaryWithCapacity:[array count]];
for (NSDictionary* dict in array) {
MapAnnotations* annotation = [[MapAnnotations alloc]initWithDictionary:dict];
[self.map addAnnotation:annotation];
[annotation release];
}
NSLog(@"The count: %i", [myDict count]);
}
else {
NSLog(@"Plist does not exist");
}
}
如果有人能向我解释我哪里出了问题以及如何做我需要做的事情,我将不胜感激。
感谢您的阅读。
I was looking at the following discussion of a question to try and understand how to read locations from a plist and drop pins an Map view accordingly:
MapKit based app crashing when loading from plist
I am having problems understanding how to make this work. If I hard code in the annotations then I can make it work fine so I don't understand where I'm going wrong. I think it is probably how I am accessing the data in the .plst. My plist has the structure:
<array>
<dict>
<key>rowData</key>
<array>
<dict>
<key>details</key>
<string>Some minor info</string>
<key>latitude</key>
<strong>53.958756</string>
<key>Location</key>
<string>Location One</string>
<key>lontitude</key>
<string>-1.07937</string>
</dict>
</array>
</dict>
</array>
I try to access this data using the code from the answer to the question above like this But with not joy:
-(id)initWithDictionary:(NSDictionary *)dict{
self = [super init];
if(self!=nil){
coordinate.latitude = [[dict objectForKey:@"latitude"] doubleValue];
coordinate.longitude = [[dict objectForKey:@"longitude"] doubleValue];
self.title = [dict objectForKey:@"Location"];
self.subtitle = [dict objectForKey:@"details"];
}
return self;
My viewDidLoad also looks a bit different to the other question I referenced above. It looks like this:
- (void)gotoLocation
{
// set location as York, UK
MKCoordinateRegion newRegion;
newRegion.center.latitude = 53.960025;
newRegion.center.longitude = -1.082697;
newRegion.span.latitudeDelta = 0.0012872;
newRegion.span.longitudeDelta = 0.0159863;
[self.map setRegion:newRegion animated:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
//Set up map
self.map.mapType = MKMapTypeStandard; // also MKMapTypeSatellite or MKMapTypeHybrid
[self gotoLocation];
// Get the the plist in application bundle
NSString *path = [[NSBundle mainBundle] pathForResource:@"Places" ofType:@"plist"];
// Retrieve the plists root element array
NSArray *array = [[NSArray alloc] initWithContentsOfFile:path];
NSLog(@"Grabbed locations.plist ok");
if (array) {
NSMutableDictionary* myDict = [NSMutableDictionary dictionaryWithCapacity:[array count]];
for (NSDictionary* dict in array) {
MapAnnotations* annotation = [[MapAnnotations alloc]initWithDictionary:dict];
[self.map addAnnotation:annotation];
[annotation release];
}
NSLog(@"The count: %i", [myDict count]);
}
else {
NSLog(@"Plist does not exist");
}
}
If anyone can explain to me where I am going wrong and how to do what I need to do, I'd appreciate it.
Thanks for reading.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,plist 有几个问题(至少在您问题中的示例中):
(在
latitude
键下)应该是lontitude
应该是longitude
接下来,您的 plist 结构是一个数组,其中包含一个字典,其中一个键为“rowData”,其值为一个数组字典。但是代码循环遍历 array 变量,该变量仅包含最外面的数组(仅包含带有“rowData”键的单个字典)。因此,在
initWithDictionary
中,objectForKey
调用全部返回 nil,因为与位置相关的键不在最外层字典中。您想要重新构造 plist,使其只是一个位置字典数组(消除“rowData”字典),或者更新代码以获取“rowData”键内的数组并循环遍历该数组:
最后, < code>myDict 字典并没有真正被使用。其计数的 NSLog 将始终显示零,因为
dictionaryWithCapacity
行仅分配内存但不添加任何对象。我认为不需要myDict
变量 - 我将删除它。First, there are a couple of problems with the plist (at least with the example in your question):
<strong>
(underlatitude
key) should be<string>
lontitude
should belongitude
Next, your plist structure is an array containing one dictionary with one key "rowData" with a value that is an array of dictionaries. But the code loops through the
array
variable which contains only the outermost array (which only contains the single dictionary with the "rowData" key). So ininitWithDictionary
, theobjectForKey
calls all return nil because the location related keys aren't in the outermost dictionary.You want to either re-structure the plist so that it is just an array of location dictionaries (eliminate the "rowData" dictionary) or update the code to get the array inside the "rowData" key and loop through that:
Finally, the
myDict
dictionary is not being used really. The NSLog of its count will always display zero because thedictionaryWithCapacity
line just allocates memory but doesn't add any objects. I don't see a need for themyDict
variable--I'd remove it.