向地图添加多个注释时出现问题

发布于 2024-08-02 01:41:56 字数 2335 浏览 1 评论 0原文

好的,所以我遇到了这个问题。 我想要做的是手动向地图添加多个注释。 当我只添加一个注释时,它就可以完美地工作。 图钉掉落,你可以点击它来查看它的标注,生活是美好的。

当我想添加多个时,问题就出现了。 当我添加第二个时,突然,图钉的颜色不正确(即,根据它们的大小,它们应该是某种颜色,但它们现在都是相同的......),更重要的是,当您单击它们时,可以看到它们的颜色标注,应用程序因 exex_bad_access 而崩溃。 我真的不知道出了什么问题,也许我向地图添加了太多视图? 但它只有 9 个引脚,并且引脚本身可以添加得很好。 这是我的代码......

    - (void)viewDidLoad {
    [super viewDidLoad];
    NSMutableArray *stops = [[NSMutableArray alloc] init];  //Get list of all the stops available
    Bus *bus1 = [[Bus alloc] init];                         // Bus 1 holds the stops
    stops = [bus1 returnStops];
    for (NSString *stop in stops)                           //Go through each stop to add annotation to map
    {
        Bus *bus2 = [bus1 initWithStop:stop];                //Create an instance of bus with a given stop
        MapAnnotation *eqAnn = [MapAnnotation annotationWithBus:bus2]; 
        [self.mapView addAnnotation:eqAnn];                    //Add the annotation to the map
        //[eqAnn release];
        //[bus2 release];
    }
    [self recenterMap];
    [stops release];

}
- (MKAnnotationView *)mapView:(MKMapView *)mapView 
            viewForAnnotation:(id <MKAnnotation>)annotation {
    MKAnnotationView *view = nil;
    if(annotation != mapView.userLocation) {
        MapAnnotation *eqAnn = (MapAnnotation*)annotation;
        view = [self.mapView dequeueReusableAnnotationViewWithIdentifier:@"busLoc"];
        if(nil == view) {
            view = [[[MKPinAnnotationView alloc] initWithAnnotation:eqAnn
                                                    reuseIdentifier:@"busLoc"] autorelease];
        }
        CGFloat magnituide = [eqAnn.bus.magnitude floatValue];

        if(magnituide >= .80f) {
            [(MKPinAnnotationView *)view setPinColor:MKPinAnnotationColorRed];
        } else if(magnituide >= .60f) {
            [(MKPinAnnotationView *)view setPinColor:MKPinAnnotationColorPurple];
        } else 
        {
            [(MKPinAnnotationView *)view setPinColor:MKPinAnnotationColorGreen];
        }
        [(MKPinAnnotationView *)view setAnimatesDrop:YES];
        [view setCanShowCallout:YES];
    } 

    return view;
}

甚至尝试删除第二个函数,但它没有做任何事情。

谢谢您的帮助! PS我还应该添加,通常有 9 个引脚中的一两个在您单击注释时起作用......

如果我什至尝试在程序中手动仅手动添加两个注释(即删除循环),它仍然会失败并且颜色还是不对……

Ok, so I’m having this problem. What I want to do is manually add multiple annotations to a map. When I add just one annotation, it works flawlessly. The pin drops, you can click on it to see its callout, life is good.

The problem comes when I want to add more than one. When I add the second, suddenly the pin’s aren’t coloured correctly (i.e. depending on their magnitude they should be a certain color, but they’re now both the same…), and more importantly when you click on them, to see their callout, the app crashes with exex_bad_access. I really have no idea what’s wrong, maybe I’m adding too many views to the map? But it’s only 9 pins and the pins themselves add just fine.
Here’s my code…

    - (void)viewDidLoad {
    [super viewDidLoad];
    NSMutableArray *stops = [[NSMutableArray alloc] init];  //Get list of all the stops available
    Bus *bus1 = [[Bus alloc] init];                         // Bus 1 holds the stops
    stops = [bus1 returnStops];
    for (NSString *stop in stops)                           //Go through each stop to add annotation to map
    {
        Bus *bus2 = [bus1 initWithStop:stop];                //Create an instance of bus with a given stop
        MapAnnotation *eqAnn = [MapAnnotation annotationWithBus:bus2]; 
        [self.mapView addAnnotation:eqAnn];                    //Add the annotation to the map
        //[eqAnn release];
        //[bus2 release];
    }
    [self recenterMap];
    [stops release];

}
- (MKAnnotationView *)mapView:(MKMapView *)mapView 
            viewForAnnotation:(id <MKAnnotation>)annotation {
    MKAnnotationView *view = nil;
    if(annotation != mapView.userLocation) {
        MapAnnotation *eqAnn = (MapAnnotation*)annotation;
        view = [self.mapView dequeueReusableAnnotationViewWithIdentifier:@"busLoc"];
        if(nil == view) {
            view = [[[MKPinAnnotationView alloc] initWithAnnotation:eqAnn
                                                    reuseIdentifier:@"busLoc"] autorelease];
        }
        CGFloat magnituide = [eqAnn.bus.magnitude floatValue];

        if(magnituide >= .80f) {
            [(MKPinAnnotationView *)view setPinColor:MKPinAnnotationColorRed];
        } else if(magnituide >= .60f) {
            [(MKPinAnnotationView *)view setPinColor:MKPinAnnotationColorPurple];
        } else 
        {
            [(MKPinAnnotationView *)view setPinColor:MKPinAnnotationColorGreen];
        }
        [(MKPinAnnotationView *)view setAnimatesDrop:YES];
        [view setCanShowCallout:YES];
    } 

    return view;
}

even tried removing the second function, but it didn’t do anything.

Thanks for the help!
P.S I should also add, there’s usually one or two pins out of the 9 which works when you click the annotation…

If i even try to manually just two annotations by hand in the program (i.e., remove the loop), it still fails and the color is still wrong...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

倚栏听风 2024-08-09 01:41:56

您对 stops 变量的内存管理似乎不正确。 您分配一个可变数组,然后用 -[Bus returnStops] 的返回值替换该数组,然后释放它。 另外,还不清楚 bus2 发生了什么 - -[Bus initWithStop:] 是否返回 Bus不同 实例>? 通常不会在已初始化的对象上发送任何方法 -init*。 我认为您可能对 Cocoa Touch 中的内存管理约定感到困惑。 以下是有关 Cocoa 内存管理的文章和其他参考资料的集合 (这是同一个野兽)。

It would appear that your memory management of the stops variable is incorrect. You allocate a mutable array, then replace that array with the return value of -[Bus returnStops], then release that. Also it's not clear what's going on with bus2 - does -[Bus initWithStop:] return a different instance of Bus? It's not usual to send any method -init* on an already-initialised object. I think that you probably are confused by the memory management conventions in Cocoa Touch. Here's a collection of articles and other references on Cocoa memory management (which is the same beast).

凉城 2024-08-09 01:41:56

您是否尝试过使用 AddAnnotations 而不是添加注释? - (void)addAnnotations:(NSArray *)注释。 这可能对您有用...但是查看上面的答案并进一步检查您的 viewDidLoad 中存在一些内存管理问题(尽管这可能不是问题的原因,但也可能是)。 首先分配数组(停止),然后用总线对象中的某个数组对其进行验证,这将导致泄漏。 此外,您还释放了可能导致崩溃的数组,因为您释放了总线对象中实际存在的数组,但没有增加对其的引用计数。 我不确定 initWithStop 在做什么,但如果 initWithStop 保留该对象,您也可能会在这里遇到泄漏。

Have you tried using AddAnnotations instead of add annotation? - (void)addAnnotations:(NSArray *)annotations. This might work for you...but looking at the answer above and further inspection you are having some memory managment issues in your viewDidLoad (though thi s might not be the cause of your problem, but it could be). First of you are allocating the array (stops) and then ovveriding it with some array in the Bus object, this will cause a leak. Also you are then releasing that array which might be causing the crash since you are releasing the array that is actually in the Bus object w ithout having increased a reference count to it. I am not sure what initWithStop is doing but you might be getting a leak here too if initWithStop retains the object.

撞了怀 2024-08-09 01:41:56

我不会称其为内存管理问题——我只是说你错误地使用了数组引用。

使用 NSMutableArray *stops = [[NSMutableArray alloc] init] 构造数组后,下一步是使用 [stops addObject: ] 添加要存储的每个站点。

在那之后? 目前尚不清楚你真正想做什么。

I wouldn't call it a memory management problem -- I'd just say you are using array references incorrectly.

After constructing the array with NSMutableArray *stops = [[NSMutableArray alloc] init], the next step is to use [stops addObject: ] to add each stop you want to store.

After that? It's not clear what you are really trying to do.

幸福不弃 2024-08-09 01:41:56

所以答案是我一直向bus1发送init对象,所以它很困惑。

“嗨,大卫,

你的数据模型对我来说看起来很糟糕。你只有一个反复发送 initWithStop: 的总线对象。

希望这会有所帮助。

祝你好运!

谢谢你们的帮助!你们给了我很大的帮助!”

SO the answer was that I kept sending bus1 the init object, so it got confused.

"Hi David,

Your data model looks hosed to me. You only have one bus object that you are repeatedly sending initWithStop: to.

Hope this helps.

Good luck!
"

Thank you guys for your help! You all helped me quite a bit!

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