user1724168
user1724168

Reputation:

Coordinate of Selected Annotation-iOS

The following code returns me "no annotation selected" CalloutAccessories delegates helps to figure out which annotation selected. my code is below. Any idea(s)? Thanks in advance

"RemoveViewController.m"

- (IBAction)removePin:(id)sender {
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[[appDelegate viewController] removeAnno];
[self.navigationController popViewControllerAnimated:YES];

}

"ViewController.m"

- (void)mapView:(MKMapView *)mapView 
 annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {

    if ([(UIButton*)control buttonType] == UIButtonTypeDetailDisclosure){
        // Do your thing when the detailDisclosureButton is touched
        RemoveViewController *settingAnnotation = [[RemoveViewController alloc]initWithNibName:@"RemoveViewController" bundle:nil];
        Annotation *annotation1;
        annotation1 = view.annotation;
        settingAnnotation.title = annotation1.title;
        settingAnnotation.subtitle = annotation1.subtitle;
        [self.navigationController pushViewController:settingAnnotation animated:YES];
    } 

}

- (void)removeAnno{

    [mapView removeAnnotations: mapView.selectedAnnotations];
    if (mapView.selectedAnnotations.count == 0)
    {
        NSLog(@"no annotation selected");
    }
    else
    {
        id<MKAnnotation> ann = [mapView.selectedAnnotations objectAtIndex:0];
        CLLocationCoordinate2D coord = ann.coordinate;
        NSLog(@"lat = %f, lon = %f", coord.latitude, coord.longitude);
    }
}

Upvotes: 1

Views: 1570

Answers (4)

user1724168
user1724168

Reputation:

The problem fixed. [mapView removeAnnotations: mapView.selectedAnnotations] should have came after the condition! Below code is working fine!

"RemoveViewController.m"

- (IBAction)removePin:(id)sender {
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[[appDelegate viewController] removeAnno];
[self.navigationController popViewControllerAnimated:YES];

}

"ViewController.m"

- (void)mapView:(MKMapView *)mapView 
 annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {

    if ([(UIButton*)control buttonType] == UIButtonTypeDetailDisclosure){
        // Do your thing when the detailDisclosureButton is touched
        RemoveViewController *settingAnnotation = [[RemoveViewController alloc]initWithNibName:@"RemoveViewController" bundle:nil];
        Annotation *annotation1;
        annotation1 = view.annotation;
        settingAnnotation.title = annotation1.title;
        settingAnnotation.subtitle = annotation1.subtitle;
        [self.navigationController pushViewController:settingAnnotation animated:YES];
    } 

}

- (void)removeAnno{


    if (mapView.selectedAnnotations.count == 0)
    {
        NSLog(@"no annotation selected");
    }
    else
    {
        id<MKAnnotation> ann = [mapView.selectedAnnotations objectAtIndex:0];
        CLLocationCoordinate2D coord = ann.coordinate;
        NSLog(@"lat = %f, lon = %f", coord.latitude, coord.longitude);
    }
  [mapView removeAnnotations: mapView.selectedAnnotations];
}

Upvotes: 1

Ravi
Ravi

Reputation: 8309

Try

CLLocationCoordinate2D coord = [[mapView.selectedAnnotations objectAtIndex:0] coordinate]

assuming you are trying to access the first object of mapView.selectedAnnotations.

Upvotes: 0

Shachar
Shachar

Reputation: 1168

This will give you the coordinate from an address string in the form of: State, City, Street, Number.

-(CLLocationCoordinate2D) addressLocation:(NSString*) locationSTR {
NSString *addressM = [NSString stringWithString:locationSTR];

NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", 
                       [addressM stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURL *urlCont = [NSURL URLWithString:urlString];
NSError* error = nil;
NSString *locationString = [NSString stringWithContentsOfURL:urlCont encoding:NSASCIIStringEncoding error:&error];
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 location;
location.latitude = latitude;
location.longitude = longitude;

return location;

}

Upvotes: 0

user467105
user467105

Reputation:

Yes, get the annotation (if any) at index 0 of the selectedAnnotations array and read its coordinate property:

if (mapView.selectedAnnotations.count == 0)
{
    NSLog(@"no annotation selected");
}
else
{
    id<MKAnnotation> ann = [mapView.selectedAnnotations objectAtIndex:0];
    CLLocationCoordinate2D coord = ann.coordinate;
    NSLog(@"lat = %f, lon = %f", coord.latitude, coord.longitude);
}

You should probably do this before calling removeAnnotations.

Upvotes: 0

Related Questions