我想要兩種顏色的註釋。爲此,我使用了以下代碼,mapkit中的iOS引腳顏色問題
- (MKAnnotationView *)mapView:(MKMapView *)sender viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *identifier = @"MyLocation";
if ([annotation isKindOfClass:[PlaceMark class]]) {
MKPinAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
@try {
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc]
initWithAnnotation:annotation
reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
if([[nsCenterOrOffice objectAtIndex:z] isEqualToString:@"C"])
{
annotationView.pinColor = MKPinAnnotationColorRed;
}
else
{
annotationView.pinColor = MKPinAnnotationColorGreen;
}
}
}
@catch (NSException *exception) {
NSLog(@"nsCenterOrOffice exception = %@",exception);
}
return annotationView;
}
return nil;
}
但是我仍然無法爲所需的註釋設置所需的顏色。有時特定的註釋針顏色是紅色的,有時是綠色的。我不明白爲什麼會發生這種情況。有誰能夠幫助我 ?謝謝...
我對我的code..this返工是我更新的代碼
MapAnnotation.h
#import <MapKit/MapKit.h>
#import <Foundation/Foundation.h>
@interface MapAnnotation : MKPointAnnotation
{
NSString *title;
NSString *subtitle;
int dealLnk;
float latitude;
float longitude;
CLLocationCoordinate2D coordinate;
}
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, assign) int dealLnk;
@property (nonatomic, assign) float latitude;
@property (nonatomic, assign) float longitude;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (id)initWithTitle:(NSString *)ttl subTitle:(NSString *)subttl dealLink:(int)dealLnk latitude:(float)latitude longitude:(float)longitude andCoordinate:(CLLocationCoordinate2D)c2d;
@end
MapAnnotation.m
#import "MapAnnotation.h"
@implementation MapAnnotation
@synthesize title,subtitle,dealLnk,coordinate,latitude,longitude;
- (id)initWithTitle:(NSString *)ttl subTitle:(NSString *)subttl dealLink:(int)z latitude:(float)latitude1 longitude:(float)longitude1 andCoordinate:(CLLocationCoordinate2D)c2d {
title = ttl;
subtitle = subttl;
dealLnk =z;
coordinate = c2d;
longitude = longitude1;
latitude = latitude1;
return self;
}
@end
這是我實現文件
-(void)startAddingAnnotation
{
@try {
CLLocationCoordinate2D annotationCoord;
z=0;
for (int i=0; i < [nslatitude count] ; i++,z++)
{
MapAnnotation *dealAnnotation = [[MapAnnotation alloc] init];
dealAnnotation.dealLnk = z;
annotationCoord.latitude = (CGFloat) [[nslatitude objectAtIndex:i] floatValue];
annotationCoord.longitude = (CGFloat)[[nslongitude objectAtIndex:i] floatValue];
dealAnnotation.coordinate = annotationCoord;
dealAnnotation.title = [nsCenterName objectAtIndex:i];
dealAnnotation.subtitle = [nsCenterAddress objectAtIndex:i];
NSLog(@"latitude = %f",dealAnnotation.latitude);
NSLog(@"longitude = %f",dealAnnotation.longitude);
NSLog(@"dealAnnotation.dealLnk = %d",dealAnnotation.dealLnk);
NSLog(@"dz = %d",z);
[mapView addAnnotation:dealAnnotation];
}
}
@catch (NSException *exception) {
NSLog(@"Exception = %@",exception);
}
cord.longitude = -112.05186;
cord.latitude = 33.46577;
MKCoordinateRegion region;
region.center = cord;
MKCoordinateSpan span = {.latitudeDelta = 1.0, .longitudeDelta = 1.0};
region.span = span;
[mapView setRegion:region];
}
#pragma mark - MapView_Delegate
- (MKAnnotationView *)mapView:(MKMapView *)sender viewForAnnotation:(id <MKAnnotation>)annotation {
MapAnnotation *annotation1 = (MapAnnotation *)annotation;
z = annotation1.dealLnk;
NSLog(@"annotation1.longitude = %f",annotation1.latitude);
NSLog(@"annotation1.longitude = %f",annotation1.longitude);
if(z<[nslatitude count])
{
MKAnnotationView *pinView = nil;
static NSString *defaultPinID = @"pin1";
pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if (pinView == nil)
pinView = [[MKAnnotationView alloc]
initWithAnnotation:annotation1 reuseIdentifier:defaultPinID];
pinView.canShowCallout = YES;
@try {
if([[nsCenterOrOffice objectAtIndex:z] isEqualToString:@"C"])
{
pinView.image = [UIImage imageNamed:@"flag1.png"];
}
else
{
pinView.image = [UIImage imageNamed:@"flag2.png"];
}
pinView.annotation = annotation1;
pinView.tag = [[nsCenterName objectAtIndex:z] intValue];
}
@catch (NSException *exception) {
NSLog(@"nsCenterOrOffice exception = %@",exception);
}
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
[pinView setRightCalloutAccessoryView:rightButton];
return pinView;
}
else
{
return nil;
}
}
現在的問題是,所有的註釋都指向一些不同的位置。我不明白爲什麼會發生這種情況?我的代碼中是否有任何錯誤?
你檢查了你的條件** [[nsCenterOrOffice objectAtIndex:z] isEqualToString:@「C」] **是否滿足? – Pratik
是的..這種情況正在起作用。該聲明中沒有問題。 – user2260521
然後你可以使用圖像針腳 – Pratik