我有3种类型的地点。
我从我的服务器获取数据,然后我正在解析它并在mapView上显示位置。
我想为不同类型的数据显示不同的颜色。 3种类型= 3种颜色。
我该如何控制它?
答案 0 :(得分: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
这是一个不错的tutorial。
答案 1 :(得分: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
}
答案 2 :(得分: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"];
}}