0

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?

Navnath Memane
  • 265
  • 1
  • 8
  • 25
  • I am not getting the annotation on map after trying so much... – Navnath Memane Sep 20 '11 at 14:17
  • 1
    What happens when you run this? Why do you use the index `i` to access both the `markers` array _and_ the `lat` and `lng` in the `aMarker` object? Why do you set `location` to `userLocation` and then overwrite it with the values from `markers`? Do you do anything with the `region`? You should log the value of `location` before you call `mapView addAnnotation`. Also, `addAnnotation` is a very confusing name for an annotation variable. –  Sep 20 '11 at 14:28
  • Have u Solved ur problem?If not, just tell me what is location, is it array of Cllocation points?Only if ur location is an array of CLLocation, u will get annotations. – iCoder4777 Sep 21 '11 at 03:44
  • Actually when I gave lattitude = -33, longitude= 150 (exclude the for loop) it perfectely shows annotation on the australia. But I want this values from the XMLparser class. After searching so much I ended up this code which I have pasted....which is not working – Navnath Memane Sep 21 '11 at 05:13
  • After running above code I get warning"NSString may not respond to -objectAtindex" Can you tell me how can I take lat,lng parsing values from parser class to my MapViewcontroller to set the annotation on map. – Navnath Memane Sep 21 '11 at 05:28

1 Answers1

0

The warning NSString may not respond to -objectAtIndex means lat and lng are NSString objects (which don't have an objectAtIndex method).

You don't need to call objectAtIndex on lat and lng--they are the values in the specific marker object 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:

for (int i = 0; i < [appDelegate.markers count]; i++)
{
    marker *aMarker = [appDelegate.markers objectAtIndex:i];

    location.latitude = [aMarker.lat floatValue];
    location.longitude =[aMarker.lng floatValue];

    AddressAnnotation *addrAnnot = [[AddressAnnotation alloc] initWithCoordinate:location];
    [mapView addAnnotation:addrAnnot];
    [addrAnnot release];
}

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.

Community
  • 1
  • 1
  • It works like magic trick for me. thanks for the correction you made Anna – Navnath Memane Sep 22 '11 at 08:48
  • Hi Anna, I am updating this application for iOS6 and it is crashing on above suggested method. Stating as following nsarray was mutated while being enumerated error. Can you suggest me the tweak in the above method – Navnath Memane Feb 19 '13 at 07:04