How to add image in annotation in mkmap [duplicate]

二次信任 提交于 2019-12-11 18:38:35

问题


I am new to Xcode, i need to set the image in annotation point. I am trying to replace the typical iOS mapkit "pin" with a particular image. I'm new to coding so I'm not sure exactly why this isn't working, but here is what i've attempted My code is

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

       }];

}

Please guide me to set the image.. Thanks in Advance


回答1:


You can use mapView Delegate method.

- (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;
    }


来源:https://stackoverflow.com/questions/24426521/how-to-add-image-in-annotation-in-mkmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!