I have the following code:
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.0005;
span.longitudeDelta=0.0005;
CLLocationCoordinate2D location = mapView.userLocation.coordinate;
for (int i = 0; i < [appDelegate.markers count]; i++) {
marker *aMarker = [appDelegate.markers objectAtIndex:i];
location.latitude = [[aMarker.lat objectAtIndex:i] floatValue];
location.longitude =[[aMarker.lng objectAtIndex:i] floatValue];
region.span=span;
region.center=location;
if(addAnnotation != nil)
{
[mapView removeAnnotation:addAnnotation];
[addAnnotation release];
addAnnotation = nil;
}
addAnnotation = [[AddressAnnotation alloc] initWithCoordinate:location];
[mapView addAnnotation:addAnnotation];
}
I have parsed latitude and longitude in my XMLparser class. Now I want to add annotation on buttonclick event on Map. Can someone correct my code?
The warning
NSString may not respond to -objectAtIndexmeanslatandlngareNSStringobjects (which don’t have anobjectAtIndexmethod).You don’t need to call
objectAtIndexonlatandlng–they are the values in the specificmarkerobject you just retrieved from the array.(By the way, you get the warning when you compile the code–not when you “run” it like in your comment. Secondly, don’t ignore compiler warnings. In this case, when you run the code you will get an exception:
NSInvalidArgumentException - unrecognized selector.)The other problem is the loop removes the previously added annotation before adding the current one. If you want to add all the markers to the map, this doesn’t make sense. Currently, it would end up adding only the last marker to the map. If you really want to add only the last marker then you don’t need to loop through the array (just grab the last marker from the array directly and create an annotation from it).
If instead you do want to add all the markers to the map, the loop should look like this:
It’s not clear what you’re doing with
region. If you’re trying to set the region so that the map view shows all the annotations, look at the answers to this question: iOS MKMapView zoom to show all markers.