请指导我在MkMap中更改颜色并设置注释图像

时间:2014-06-24 08:48:36

标签: ios iphone annotations mkmapview mapkit

我需要更改颜色,在某些位置我需要为注释点设置图像。我的代码只显示红色注释,请指导我,我的代码是下面的,

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //MAP VIEW WebService
   // NSLog(@"%@",nam2);
    NSString *urlMapString=[NSString stringWithFormat:@"http://www.tranzlogix.com/tranzlogix_webservice/map.php?format=json&truckno=%@",nam2];
    NSURL *urlMap=[NSURL URLWithString:urlMapString];
    NSData *dataMap=[NSData dataWithContentsOfURL:urlMap];
    NSError *errorMap;

    NSDictionary *jsonMap = [NSJSONSerialization JSONObjectWithData:dataMap options:kNilOptions error:&errorMap]; NSArray *resultsMap = [jsonMap valueForKey:@"posts"];
    NSArray *resMap = [resultsMap valueForKey:@"post"];


    NSArray *latitudeString=[resMap valueForKey:@"latitude"];
    // NSLog(@"%@", latitudeString);
    NSString *latOrgstring = [latitudeString objectAtIndex:0];
    // NSLog(@"%@", latOrgstring);
    double latitude=[latOrgstring doubleValue];

    NSArray *longitudeString=[resMap valueForKey:@"longitude"];
    // NSLog(@"%@", longitudeString);
    NSString *longOrgstring = [longitudeString objectAtIndex:0];
    // NSLog(@"%@", longOrgstring);
    double longitude=[longOrgstring doubleValue];
    // NSLog(@"latdouble: %f", longitude);

    //MAP VIEW Point
    MKCoordinateRegion myRegion;

    //Center
    CLLocationCoordinate2D center;
    center.latitude=latitude;
    center.longitude=longitude;

    //Span
    MKCoordinateSpan span;
    span.latitudeDelta=THE_SPAN;
    span.longitudeDelta=THE_SPAN;

    myRegion.center=center;
    myRegion.span=span;

    //Set our mapView
    [MapViewC setRegion:myRegion animated:YES];

    //Annotation

    //1.create coordinate for use with the annotation
    CLLocationCoordinate2D wimbLocation;
    wimbLocation.latitude=latitude;
    wimbLocation.longitude=longitude;

    Annotation * myAnnotation= [Annotation alloc];

    CLLocation *someLocation=[[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:someLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        NSDictionary *dictionary = [[placemarks objectAtIndex:0] addressDictionary];
        // NSLog(@"%@",dictionary);
        addressOutlet=[dictionary valueForKey:@"Street"];
        // NSLog(@"%@",addressOutlet);
        City=[dictionary valueForKey:@"City"];
        // NSLog(@"%@",City);
        State=[dictionary valueForKey:@"State"];
        // NSLog(@"%@",State);

        myAnnotation.coordinate=wimbLocation;

        if (addressOutlet!=NULL&&City!=NULL)
        {
            myAnnotation.title=addressOutlet;
            myAnnotation.subtitle=[NSString stringWithFormat:@"%@,%@", City, State];

        }
        else if (addressOutlet==NULL&&City!=NULL)
        {

            myAnnotation.title=City;
            myAnnotation.subtitle=[NSString stringWithFormat:@"%@,%@", City, State];
        }
        else if (addressOutlet!=NULL&&City==NULL)
        {
            myAnnotation.title=addressOutlet;
            myAnnotation.subtitle=[NSString stringWithFormat:@"%@", State];
        }
        else if(addressOutlet==NULL&&City==NULL)
        {

            myAnnotation.title=State;
            myAnnotation.subtitle=[NSString stringWithFormat:@"%@",State];
        }

        [self.MapViewC addAnnotation:myAnnotation];

    }];

}

请指导我,我对xcode&目标c

1 个答案:

答案 0 :(得分:0)

您需要子类化MKAnnotationView并覆盖几个属性和方法来设置首选项的图像。因为那只是太多的步行,我设法捞出blog来帮助你做到这一点。