7

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

Heres my code:

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

NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", 
                       [endereco stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
NSArray *listItems = [locationString componentsSeparatedByString:@","];

double latitude = 0.0;
double longitude = 0.0;

if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) {
    latitude = [[listItems objectAtIndex:2] doubleValue];
    longitude = [[listItems objectAtIndex:3] doubleValue];
}
else {
    //Show error
}

CLLocationCoordinate2D coordinate;
coordinate.latitude = latitude;
coordinate.longitude = longitude;
myMap.region = MKCoordinateRegionMakeWithDistance(coordinate, 2000, 2000);

[self.view addSubview:mapa];


 }

Thanks!

Lucas Veiga
  • 1,758
  • 7
  • 27
  • 45

3 Answers3

16

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];
Stephen Poletto
  • 3,645
  • 24
  • 24
  • Im getting "Use of undeclared identifier 'Annotation'. – Lucas Veiga Mar 30 '11 at 20:21
  • You'll have to define your own Annotation class, since MKAnnotation is a protocol. See this answer: http://stackoverflow.com/questions/2878758/iphone-create-mkannotation – Stephen Poletto Mar 30 '11 at 20:28
  • 6
    You can also use the pre-defined MKPointAnnotation class. –  Mar 30 '11 at 20:40
  • aBitObvious, how can i do that? There is any tutorial? – Lucas Veiga Mar 30 '11 at 20:46
  • @Lucas - Just alloc and init an MKPointAnnotation. Then, call setCoordinate, setTitle and setSubtitle (as needed) on the newly created MKPointAnnotation. Last, [myMap addAnnotation:theNewlyCreatedAnnotation]; – Stephen Poletto Mar 30 '11 at 20:56
  • Yes, i did it! But, how can i set a image for it? – Lucas Veiga Mar 30 '11 at 20:56
  • Check out the sample code for MapCallouts: http://developer.apple.com/library/ios/#samplecode/MapCallouts/Introduction/Intro.html%23//apple_ref/doc/uid/DTS40009746 – Stephen Poletto Mar 30 '11 at 21:16
  • You'll want to look at - (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id )annotation. It allows you to set an image to show on the map, and to customize the callout that is displayed when the user taps the pin. – Stephen Poletto Mar 30 '11 at 21:17
0

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

}

Mat Kelly
  • 2,327
  • 7
  • 29
  • 51
NANNAV
  • 4,875
  • 4
  • 32
  • 50
0

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

@interface MyMapAnnotation : NSObject <MKAnnotation> {
...
}
jmans
  • 5,648
  • 4
  • 27
  • 32
  • You mean to do it in the same header? – Lucas Veiga Mar 30 '11 at 20:29
  • You will want to create a new class for your annotation, with its own header and main file. At a minimum, this class will need to implement - (id) initWithCoords:(CLLocationCoordinate2D) coords and the CLLocationCoordinate2D coordinate property. See: [MKAnnotation docs](http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKAnnotation_Protocol/Reference/Reference.html) – jmans Mar 30 '11 at 20:32