• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 学习 iOS MapKit - 来自 NSDictionary 的 map 上的多个图钉

[复制链接]
菜鸟教程小白 发表于 2022-12-11 22:16:46 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

尝试获取包含我所有位置、标题和字幕的 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-推荐答案


这一行:

myAnnotation=[[MKPointAnnotation alloc]init];

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

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


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

关于ios - 学习 iOS MapKit - 来自 NSDictionary 的 map 上的多个图钉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9830074/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap