我有3種類型的位置。針對不同數據的不同MKPinAnnotation
我從我的服務器獲取數據,然後解析它並在mapView上顯示位置。
我想爲不同類型的數據顯示不同的顏色。 3種= 3種顏色。
我該如何控制它?
我有3種類型的位置。針對不同數據的不同MKPinAnnotation
我從我的服務器獲取數據,然後解析它並在mapView上顯示位置。
我想爲不同類型的數據顯示不同的顏色。 3種= 3種顏色。
我該如何控制它?
執行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。
我不想檢查,如果標題是平等的,因爲有很多標題。 –
@YosiDahan:這是一個例子,你可以在那裏檢查你的類型 –
該類型是數組中每個對象所具有的值。我如何分配每個註釋的類型? –
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
}
你可以用經緯度比較東西
- (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"];
}}
試試這個, annotation1.subtitle = @ 「第一個註釋」; annotation2.subtitle = @「2st annotation」; annotation3.subtitle = @「3st annotation」; 檢查註釋 如果([annotation.subtitle isEqualToString:@ 「第一標註」]){ // 變色 } 否則如果([annotation.subtitle isEqualToString:@ 「2ST註釋」]){ //改變顏色 } 否則如果([annotation.subtitle isEqualToString:@「3ST註釋」]){ // 變色 } – Kalpesh