目标C-一次只有一个图钉?

时间:2018-06-27 18:58:52

标签: objective-c mkmapview core-location mkannotation mkannotationview

我在地图视图顶部有2组不同的图钉。首先出现一个图钉,它是用户位置,然后在搜索栏中搜索某个位置时,根据位置而出现多个图钉。我想一次显示一个图钉。一旦搜索了位置,用户的位置图钉便会消失,并且一旦您选择了多个图钉之一,搜索的其他图钉便会消失。

这是我的代码:

 #import "UbicacionVC.h"
 #import "SWRevealViewController.h"
 #import <MapKit/Mapkit.h>
 #import "Location.h"


 @interface UbicacionVC ()

 @end

 @implementation UbicacionVC
 @synthesize mapView;

 - (void)viewDidLoad {
     [super viewDidLoad];
     // Do any additional setup after loading the view.
     [self inicializarComponentes];
     self.mapView.delegate = self;
     self.searchBar.delegate = self;
 }



 - (void)didReceiveMemoryWarning {
     [super didReceiveMemoryWarning];
     // Dispose of any resources that can be recreated.
 }

 - (void)inicializarComponentes {
     [self.btnContinuar.layer setCornerRadius:20.0f];
     [self.btnCancelar.layer setCornerRadius:20.0f];

     ////
     UITapGestureRecognizer *gestureMenu = [[UITapGestureRecognizer                                                   alloc] init];
     [gestureMenu addTarget:self.revealViewController                                              action:@selector(revealToggle:)];
     [gestureMenu setCancelsTouchesInView:NO];
     [self.btnLeftMenu addGestureRecognizer:gestureMenu];

     [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
////
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    [self->locationManager requestWhenInUseAuthorization];

[locationManager startUpdatingLocation];



 }



 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
     NSLog(@"OldLocation %f %f", oldLocation.coordinate.latitude, oldLocation.coordinate.longitude);
     NSLog(@"NewLocation %f %f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
     CLLocationDegrees lat = newLocation.coordinate.latitude;
     CLLocationDegrees lon = newLocation.coordinate.longitude;
     CLLocation * location = [[CLLocation alloc]initWithLatitude:lat longitude:lon];
     self.viewRegion =      MKCoordinateRegionMakeWithDistance(location.coordinate, 500, 500);
[self.mapView setRegion:self.viewRegion];


 }


 -(void)localSearch:(NSString*)searchString{

     [self.mapView setRegion:self.viewRegion];

     MKLocalSearchRequest * request = [[MKLocalSearchRequest alloc] init];
     request.naturalLanguageQuery = [searchString lowercaseString];
     request.region = self.viewRegion;

     MKLocalSearch* search = [[MKLocalSearch alloc] initWithRequest:request];
     [search startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {

         if([response.mapItems count] == 0){
        NSLog(@"No matches \n");
    }
         else{
             for(MKMapItem * item in response.mapItems){
                 Location * pin = [[Location alloc] initWith:item.placemark.title andSubtitle:item.phoneNumber andCoordinate:item.placemark.coordinate andImageName:@"" andURL:item.url.absoluteString];
                 [self.mapView addAnnotation:pin];


        }
    }

}];

 }

 #pragma mark UISearchBarDelegate

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

     [searchBar resignFirstResponder];
     [self.mapView removeAnnotations:[self.mapView annotations]];
     [self localSearch:searchBar.text];

 }

 #pragma mark MKMapViewDelegate

 -(MKAnnotationView*)mapView:(MKMapView*)sender viewForAnnotation:          (id<MKAnnotation>)annotation{

     static NSString* identifier = @"reusablePin";

     MKAnnotationView * aView = [sender dequeueReusableAnnotationViewWithIdentifier:identifier];
     if(!aView){

         aView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
         aView.canShowCallout = YES;
     }

     aView.annotation = annotation;
     return aView;
 }


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

          NSLog(@"%@",view.annotation.title);

          NSLog(@"%@",view.annotation.subtitle);


 }

1 个答案:

答案 0 :(得分:1)

  

我想一次显示一个图钉。

break;在for循环中如何?

for(MKMapItem * item in response.mapItems){
    Location * pin = [[Location alloc] initWith:item.placemark.title andSubtitle:item.phoneNumber andCoordinate:item.placemark.coordinate andImageName:@"" andURL:item.url.absoluteString];
    [self.mapView addAnnotation:pin];

    // one pin showed
    break;
}
  

搜索位置后,用户的位置图钉应消失,   并且一旦您选择了多个图钉之一,其他图钉就会消失。

您可以使用didSelectAnnotationView方法。

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
  // once you select one of the multiple pins, the others should disappear.
  for (MKPointAnnotation *annotation in mapView.annotations) {
    if (view.annotation != annotation) {
        [mapView removeAnnotation:annotation];
        NSLog(@"yes!!");
    }
  }
}