如何在mkmap中的注释中添加图像

时间:2014-06-26 09:07:02

标签: ios objective-c xcode annotations mkmapview

我是Xcode的新手,我需要在注释点设置图像。我正在尝试替换典型的iOS mapkit" pin"带有特定的图像。我对编码很陌生,所以我不确定为什么这不起作用,但这是我尝试过的 我的代码是

- (void)viewDidLoad

{

    [super viewDidLoad];


    NSString *urlMapString=[NSString stringWithFormat:@"http://www.xxxx.com/xxxx_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"];

    NSString *latOrgstring = [latitudeString objectAtIndex:0];

    double latitude=[latOrgstring doubleValue];

    NSArray *longitudeString=[resMap valueForKey:@"longitude"];

    NSString *longOrgstring = [longitudeString objectAtIndex:0];

    double longitude=[longOrgstring doubleValue];

    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];

    myAnnotation.image = [UIImage imageNamed:@"icon.gif"];

    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];
    addressOutlet=[dictionary valueForKey:@"Street"];
    City=[dictionary valueForKey:@"City"];

    State=[dictionary valueForKey:@"State"];

    myAnnotation.coordinate=wimbLocation;

    if (addressOutlet!=NULL&&City!=NULL)

        {

            myAnnotation.title=addressOutlet;

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

            myAnnotation.image = [UIImage imageNamed:@"icon.gif"];

            }

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

        {


            myAnnotation.title=City;

            myAnnotation.subtitle=[NSString stringWithFormat:@"%@,%@", City, State];
            myAnnotation.image = [UIImage imageNamed:@"icon.gif"];

             }

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

        {

            myAnnotation.title=addressOutlet;

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

            myAnnotation.image = [UIImage imageNamed:@"icon.gif"];
        }

        else if(addressOutlet==NULL&&City==NULL)

        {

            myAnnotation.title=State;

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

            myAnnotation.image = [UIImage imageNamed:@"icon.gif"];

           }

       [self.MapViewC addAnnotation:myAnnotation];

       }];

}

请指导我设置图片.. 在此先感谢

1 个答案:

答案 0 :(得分:2)

您可以使用mapView Delegate方法。

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
    {
        if (annotation == mapView.userLocation)
            return nil;

        static NSString *s = @"identifier";
        MKAnnotationView *pin = [mapView dequeueReusableAnnotationViewWithIdentifier:s];
        if (!pin) {
            pin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:s];
            pin.canShowCallout = YES;
            pin.image = [UIImage imageNamed:@"pin.png"];
            pin.calloutOffset = CGPointMake(0, 0);
        }
        return pin;
    }