How to add a Map annotation on MKMapView?

前端 未结 3 1343
被撕碎了的回忆
被撕碎了的回忆 2020-12-10 07:04

Hey, im trying to add an annotation on my map. How can i do it?

Heres my code:

- (void)abreMapa:(NSString *)endereco {

NSString *urlString = [NSStri         


        
相关标签:
3条回答
  • 2020-12-10 07:36

    You'll also have to code up an annotation class that adopts the MKAnnotation protocol. e.g.:

    @interface MyMapAnnotation : NSObject <MKAnnotation> {
    ...
    }
    
    0 讨论(0)
  • 2020-12-10 07:37

    Since MKAnnotation is a protocol, you'll have to define your own class that implements the protocol. For example,

    @interface SPAnnotation : NSObject <MKAnnotation> {
        CLLocationCoordinate2D coordinate;
    }
    
    @property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
    
    - (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate;
    

    If you have a latitude, longitude for the location you'd like to map:

    SPAnnotation *annotation = [[Annotation alloc] initWithCoordinate:CLLocationCoordinate2DMake(latitude, longitude)];
    [myMap addAnnotation:annotation];
    
    0 讨论(0)
  • 2020-12-10 07:42

    Try this

    Pin *selected = [[Pin alloc] init];
    
    for(i=0; i<[[newxml dataarray] count]; i++)
    {
        gapi = [newxml.dataarray objectAtIndex:i]; 
        MKCoordinateRegion region ;  // = { {0.0, 0.0 }, { 0.0, 0.0 } }; 
        region.center. latitude = [gapi.lat floatValue];
        region.center. longitude = [gapi.lng floatValue];
    
        region.span.longitudeDelta = 0.05f;
        region.span.latitudeDelta = 0.05f;
    
        [self.mapview setRegion:region animated:YES]; 
    
        Pin* myAnnotation1=[[Pin alloc] init];
    
        myAnnotation1.coordinate=region.center;;
        myAnnotation1.title=gapi.name;
        myAnnotation1.itemIndex = i;
        myAnnotation1.subtitle=gapi.address;
        [self.mapview addAnnotation:myAnnotation1];
    
        if (j == i)
        {  
                selected = myAnnotation1;
        }
    
        if(k==123)
        {
            [self.mapview selectAnnotation:selected animated:NO]; 
        }
    

    }

    0 讨论(0)
提交回复
热议问题