无法在 Mapkit 中显示 2 种颜色

发布于 2024-11-16 13:22:01 字数 3037 浏览 2 评论 0原文

好吧,绝望。这真是要了我的命..我完全不理解MapKit..尽管阅读了这么多教程..=(

有关更多信息,我有2个注释类 - MyAnnotation 和 MyAnnotation2,它似乎也不起作用..请有人帮忙.. 恐怕我的头发很快就没有了=(

- (void)viewWillAppear:(BOOL)animated
{

[super viewWillAppear:animated];


_annotations = [[NSMutableArray alloc] init];
_annotation2 = [[NSMutableArray alloc] init];

CLLocation *userLoc = _mapView.userLocation.location;
CLLocationCoordinate2D userCoordinate = userLoc.coordinate;

NSLog(@"user latitude = %f",userCoordinate.latitude);
NSLog(@"user longitude = %f",userCoordinate.longitude);


_listOfPolyClinics = [[NSMutableArray alloc] init];
_listOfPatients = [[NSMutableArray alloc] init ];



for (PolyClinics *polyclinics in [[PatientDatabase database] 
                                  polyClinics]){
    [_listOfPolyClinics addObject:polyclinics];
}
NSLog(@"%i", [_listOfPolyClinics count]);

for (PatientDetails *patientDetails in [[PatientDatabase database] 
                                        patientCategoryList:_category]){
    [_listOfPatients addObject:patientDetails];
}

NSLog(@"%i", [_listOfPatients count]);




for (PolyClinics *polyclinics in _listOfPolyClinics){
    MyAnnotation * myAnnotation =[[MyAnnotation alloc] init];

    CLLocationCoordinate2D theCoordinate;
    theCoordinate.longitude = polyclinics.longtitude;
    theCoordinate.latitude = polyclinics.latitude;

    myAnnotation.pinColor = MKPinAnnotationColorPurple;
    myAnnotation.coordinate = theCoordinate;
    myAnnotation.title = polyclinics.name;
    myAnnotation.subtitle = [NSString stringWithFormat:@"%i",polyclinics.telephone];
    //myAnnotation.annotationsPatients = 

    [_mapView addAnnotation:myAnnotation];
    [_annotation2 addObject:myAnnotation];
}

for(PatientDetails *patientDetails in _listOfPatients){
    MyAnnotation2 * myAnnotation =[[MyAnnotation2 alloc] init];

    CLLocationCoordinate2D theCoordinate;
    theCoordinate.longitude = patientDetails.longitude;
    theCoordinate.latitude = patientDetails.latitude;

    myAnnotation.pinColor = MKPinAnnotationColorGreen;
    myAnnotation.coordinate = theCoordinate;
    myAnnotation.title = patientDetails.nric;
    myAnnotation.subtitle = [NSString stringWithFormat:@"%i",patientDetails.category];

    [_mapView addAnnotation:myAnnotation];
    [_annotation2 addObject:myAnnotation];
}

}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{

NSLog(@"%i", [_annotation2 count]);
    MKPinAnnotationView *pinView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:[annotation title]];

    for(id <MKAnnotation> a in _annotation2){


        if([a isKindOfClass:[MyAnnotation class]]){
        pinView.pinColor = MKPinAnnotationColorPurple;
        }
        else{
            pinView.pinColor = MKPinAnnotationColorGreen;
        }
        pinView.animatesDrop=NO;
        pinView.canShowCallout=YES;
        return [pinView autorelease];



    }
    return pinView;

}

ok desperation. This is killing me.. I totally do not understand MapKit at all.. Despite reading so many tutorial.. =(

For more information, I have 2 annotation classes - MyAnnotation and MyAnnotation2, it doesnt seems to work too.. Somebody plz help.. I am afraid I have no hair left soon =(

- (void)viewWillAppear:(BOOL)animated
{

[super viewWillAppear:animated];


_annotations = [[NSMutableArray alloc] init];
_annotation2 = [[NSMutableArray alloc] init];

CLLocation *userLoc = _mapView.userLocation.location;
CLLocationCoordinate2D userCoordinate = userLoc.coordinate;

NSLog(@"user latitude = %f",userCoordinate.latitude);
NSLog(@"user longitude = %f",userCoordinate.longitude);


_listOfPolyClinics = [[NSMutableArray alloc] init];
_listOfPatients = [[NSMutableArray alloc] init ];



for (PolyClinics *polyclinics in [[PatientDatabase database] 
                                  polyClinics]){
    [_listOfPolyClinics addObject:polyclinics];
}
NSLog(@"%i", [_listOfPolyClinics count]);

for (PatientDetails *patientDetails in [[PatientDatabase database] 
                                        patientCategoryList:_category]){
    [_listOfPatients addObject:patientDetails];
}

NSLog(@"%i", [_listOfPatients count]);




for (PolyClinics *polyclinics in _listOfPolyClinics){
    MyAnnotation * myAnnotation =[[MyAnnotation alloc] init];

    CLLocationCoordinate2D theCoordinate;
    theCoordinate.longitude = polyclinics.longtitude;
    theCoordinate.latitude = polyclinics.latitude;

    myAnnotation.pinColor = MKPinAnnotationColorPurple;
    myAnnotation.coordinate = theCoordinate;
    myAnnotation.title = polyclinics.name;
    myAnnotation.subtitle = [NSString stringWithFormat:@"%i",polyclinics.telephone];
    //myAnnotation.annotationsPatients = 

    [_mapView addAnnotation:myAnnotation];
    [_annotation2 addObject:myAnnotation];
}

for(PatientDetails *patientDetails in _listOfPatients){
    MyAnnotation2 * myAnnotation =[[MyAnnotation2 alloc] init];

    CLLocationCoordinate2D theCoordinate;
    theCoordinate.longitude = patientDetails.longitude;
    theCoordinate.latitude = patientDetails.latitude;

    myAnnotation.pinColor = MKPinAnnotationColorGreen;
    myAnnotation.coordinate = theCoordinate;
    myAnnotation.title = patientDetails.nric;
    myAnnotation.subtitle = [NSString stringWithFormat:@"%i",patientDetails.category];

    [_mapView addAnnotation:myAnnotation];
    [_annotation2 addObject:myAnnotation];
}

}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{

NSLog(@"%i", [_annotation2 count]);
    MKPinAnnotationView *pinView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:[annotation title]];

    for(id <MKAnnotation> a in _annotation2){


        if([a isKindOfClass:[MyAnnotation class]]){
        pinView.pinColor = MKPinAnnotationColorPurple;
        }
        else{
            pinView.pinColor = MKPinAnnotationColorGreen;
        }
        pinView.animatesDrop=NO;
        pinView.canShowCallout=YES;
        return [pinView autorelease];



    }
    return pinView;

}

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

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

发布评论

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

评论(1

旧人哭 2024-11-23 13:22:01
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation;

每次 MKMapView 需要显示注释视图时都会调用 MKMapView,它会出于某种原因向您传递该注释。因此,不要迭代它们并仅返回数组中第一个的颜色(您需要了解从函数/方法返回不会让循环迭代更多),而是使用参数。

另外一个好的做法是使用

- (MKAnnotationView *)dequeueReusableAnnotationViewWithIdentifier:(NSString *)identifier;

AnnotationViews ,这样就不会从头开始重新创建,特别是在内存方面!

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    BOOL isGreen = YES;
    if([annotation isKindOfClass:[MyAnnotation class]])
         isGreen = NO;

    MKPinAnnotationView *pinView = nil;

    if (isGreen) {
        static NSString *greenPin = @"greenPin";
        pinView = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:greenPin];
        if (!pinView) {
             pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:greenPin] autorelease];
             pinView.pinColor = MKPinAnnotationColorGreen;
             pinView.animatesDrop = NO;
             pinView.canShowCallout = YES;
        }
        else
            pinView.annotation = annotation;

    }
    else {
        static NSString *purplePin = @"purplePin";
        pinView = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:purplePin];
        if (!pinView) {
             pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:purplePin] autorelease];
             pinView.pinColor = MKPinAnnotationColorPurple;
             pinView.animatesDrop = NO;
             pinView.canShowCallout = YES;
        }
        else
            pinView.annotation = annotation;
    }


    return pinView;
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation;

is called every time the MKMapView needs to show a view for an annotation, it passes you that annotation for a reason. So instead of iterating through them and returning only the color for the first one in your array (you need to understand that returning from a function/method doesn't let the loop iterate more), use the parameter instead.

Also a good practice is to use

- (MKAnnotationView *)dequeueReusableAnnotationViewWithIdentifier:(NSString *)identifier;

so the AnnotationViews don't get created again from scratch, especially memory-wise!

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    BOOL isGreen = YES;
    if([annotation isKindOfClass:[MyAnnotation class]])
         isGreen = NO;

    MKPinAnnotationView *pinView = nil;

    if (isGreen) {
        static NSString *greenPin = @"greenPin";
        pinView = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:greenPin];
        if (!pinView) {
             pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:greenPin] autorelease];
             pinView.pinColor = MKPinAnnotationColorGreen;
             pinView.animatesDrop = NO;
             pinView.canShowCallout = YES;
        }
        else
            pinView.annotation = annotation;

    }
    else {
        static NSString *purplePin = @"purplePin";
        pinView = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:purplePin];
        if (!pinView) {
             pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:purplePin] autorelease];
             pinView.pinColor = MKPinAnnotationColorPurple;
             pinView.animatesDrop = NO;
             pinView.canShowCallout = YES;
        }
        else
            pinView.annotation = annotation;
    }


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