-1

我有 3 种类型的位置。

我从我的服务器获取数据,然后解析它并在 mapView 上显示位置。

我想为不同类型的数据显示不同的颜色。3 种类型 = 3 种颜色。

我该如何控制这个?

4

3 回答 3

1

viewForAnnotation为此实现委托方法。

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    static NSString *identifier = @"MyLocation";
    if ([annotation isKindOfClass:[yourAnnotationLocation class]])
    {

        MKAnnotationView *annotationView = (MKAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
        if (annotationView == nil)
        {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
            annotationView.enabled = YES;
            annotationView.canShowCallout = YES;
            //if you need image you can set it like
            //annotationView.image = [UIImage imageNamed:@"yourImage.png"];//here we use a nice image instead of the default pins
            annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        }
        else
        {
            annotationView.annotation = annotation;
        }
        if ([annotation.title isEqualToString:@"Midhun"])
        {
             annotationView.pinColor = MKPinAnnotationColorGreen;
        }
        else
        {
            annotationView.pinColor = MKPinAnnotationColorRed;
        }
        return annotationView;
    }

return nil;
}

要为您的注释设置自定义属性,请添加一个确认MKAnnotation协议的类。

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface MyLocation : NSObject <MKAnnotation> {
    NSString *_name;
    NSString *_address;
    int _yourValue;
    CLLocationCoordinate2D _coordinate;
}

@property (copy) NSString *name;
@property (copy) NSString *address;
@property (assign) yourValue;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

- (id)initWithName:(NSString*)name address:(NSString*)address coordinate:(CLLocationCoordinate2D)coordinate;

@end

这是一个很好的教程

于 2012-12-27T11:49:15.117 回答
1
Try this,
 annotation1.subtitle = @"1st annotation"; 
     annotation2.subtitle = @"2st annotation"; 
       annotation3.subtitle = @"3st annotation";
    Check annotation 
       if ([annotation.subtitle isEqualToString:@"1st annotation"]) 
       { 
          //change color 
         }
          else if ([annotation.subtitle isEqualToString:@"2st annotation"])
          { 
           //change color 
                 } 
         else if ([annotation.subtitle isEqualToString:@"3st annotation"]) 
          { 
          //change color 
           }
于 2012-12-27T11:51:38.103 回答
1

您可以通过纬度和经度比较来做一些事情

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { static NSString *identifier = @"yourIdentifier";  
MKPinAnnotationView *pin = (MKPinAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if([annotation coordinate].latitude==YourLocationLatitude)
{

    pin.image=[UIImage imageNamed:@"Flag-red.png"];
}
else
{
    pin.image=[UIImage imageNamed:@"Flag-green.png"];
}}
于 2012-12-27T11:59:29.933 回答