Вопрос

Introduction :

I have done an app,Where the user can search particular place in a map. I have a mapView and a list view which contains UITableView in which i have displayed the name of the places(search results) parsed from the google API.

My Problem:

Here my problem is when i select a specific table cell, Corresponding to the name the place should be focused in the mapView With its annotation and the annotation SHOULD BE SELECTED automatically. Hence i made this code. Also i works when i make 1st select and also when i make a new search but from the second select it never works. (i.e)

1.The annotation works

2.The zooming works

3.BUT from the second select THE "selectAnnotation:annotation animated:YES" DOES NOT WORKS. please some one help in fixing this issue.

Thanks in Advance.

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
       [tableView reloadData];
       [mapView removeAnnotation:annotation];

        annotation = [[MKPointAnnotation alloc]init];
        NSDictionary *searchResults1 = [LocationDetails objectAtIndex:indexPath.row];
        annotation.title = [searchResults1  valueForKey:@"name"];
        annotation.subtitle=[searchResults1 valueForKey:@"formatted_address"];
        annotation.coordinate= CLLocationCoordinate2DMake([[searchResults1 valueForKey:@"lat"] doubleValue], [[searchResults1 valueForKey:@"lng"] doubleValue]);

        [mapView selectAnnotation:annotation animated:YES];
        [mapView addAnnotation:annotation];


     [mapView setRegion: MKCoordinateRegionMakeWithDistance(annotation.coordinate, 1000, 1000)  animated:YES];
     [tableView removeFromSuperview];
}
Это было полезно?

Решение

Add this code in your searchBarSearchButtonClicked: as follows,

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBarr
{

for(annotation in mapView.annotations)
{
    [mapView selectAnnotation:annotation animated:NO];

}
searchData = searchBarr.text;

Your mistake was that you didn't set the animated value for selectAnnotation:animated: to NO . Hence it is showing the last annotation. Which is active.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top