I have a mapkit control that is supposed to add annotations for multiple salons. Everything works fine, it returns the latitude and longitude for each one, but when it adds the annotation, it puts both pins at the last set of coordinates. here is a snapshot of my logs, showing there are 2 sets of coordinates:
"all done!"
"there are 2 salons"
"queryString is 343 Boulevard of the Allies,Pittsburgh,United States"
"40.437941,-80.000547"
"queryString is 400 5th Avenue,Pittsburgh,United States"
"40.439492,-79.998256"
Here is my mapkit code. salonsData is a dictionary containing the sets of data for salons:
- (IBAction) showAddress {
NSString *path = @"http://myURL/service.asmx/GetByCityByStateNameByCountryName?city=Pittsburgh&stateName=Pennsylvania&countryName=United%20States";
[self parseXMLFileAtURL:path];
//Hide the keypad
[addressField resignFirstResponder];
for(NSMutableArray *salon in salonsData){
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.2;
span.longitudeDelta=0.2;
NSString *salonAddress = [salon valueForKey:@"address"];
NSString *salonCity = [salon valueForKey:@"city"];
NSString *salonCountry = [salon valueForKey:@"country"];
CLLocationCoordinate2D location = [self addressLocation:salonAddress inCity:salonCity inCountry:salonCountry];
region.span=span;
region.center=location;
if(addAnnotation != nil) {
[mapView removeAnnotation:addAnnotation];
[addAnnotation release];
addAnnotation = nil;
}
addAnnotation = [[AddressAnnotation alloc] initWithCoordinate:location];
[mapView addAnnotation:addAnnotation];
[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];
//NSLog(@"Annotation Added at coordinate %@", location);
[AddressAnnotation release];
}
}
-(CLLocationCoordinate2D) addressLocation:(NSString *)address inCity:(NSString *)city inCountry:(NSString *)country{
NSString *queryString = [NSString stringWithFormat:@"%@,%@,%@", address, city, country];
NSLog(@"queryString is %@", queryString);
NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", [queryString 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 {
//NSLog(@"No Data");
}
CLLocationCoordinate2D location;
location.latitude = latitude;
location.longitude = longitude;
return location;
}
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
annView.pinColor = MKPinAnnotationColorGreen;
annView.animatesDrop=TRUE;
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);
return annView;
}