在 Xcode 中从 plist 映射标注?

发布于 2024-11-25 22:46:23 字数 2779 浏览 1 评论 0原文

我正在查看以下问题的讨论,以尝试理解如何从 plist 中读取位置并相应地放置地图视图:

从 plist 加载时基于 MapKit 的应用程序崩溃

我在理解如何进行这项工作时遇到问题。如果我在注释中进行硬编码,那么我可以让它正常工作,所以我不明白我哪里出了问题。我认为这可能是我访问 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

莫多说 2024-12-02 22:46:23

首先,plist 有几个问题(至少在您问题中的示例中):

  • (在 latitude 键下)应该是
  • lontitude 应该是 longitude

接下来,您的 plist 结构是一个数组,其中包含一个字典,其中一个键为“rowData”,其值为一个数组字典。但是代码循环遍历 array 变量,该变量仅包含最外面的数组(仅包含带有“rowData”键的单个字典)。因此,在 initWithDictionary 中,objectForKey 调用全部返回 nil,因为与位置相关的键不在最外层字典中。

您想要重新构造 plist,使其只是一个位置字典数组(消除“rowData”字典),或者更新代码以获取“rowData”键内的数组并循环遍历该数组:

NSArray *rowDataArray = [[array objectAtIndex:0] objectForKey:@"rowData"];
for (NSDictionary* dict in rowDataArray) //instead of "in array"

最后, < code>myDict 字典并没有真正被使用。其计数的 NSLog 将始终显示零,因为 dictionaryWithCapacity 行仅分配内存但不添加任何对象。我认为不需要 myDict 变量 - 我将删除它。

First, there are a couple of problems with the plist (at least with the example in your question):

  • <strong> (under latitude key) should be <string>
  • lontitude should be longitude

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 in initWithDictionary, the objectForKey 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:

NSArray *rowDataArray = [[array objectAtIndex:0] objectForKey:@"rowData"];
for (NSDictionary* dict in rowDataArray) //instead of "in array"

Finally, the myDict dictionary is not being used really. The NSLog of its count will always display zero because the dictionaryWithCapacity line just allocates memory but doesn't add any objects. I don't see a need for the myDict variable--I'd remove it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文