Search nearby in iOS Maps

前端 未结 3 1486
轻奢々
轻奢々 2021-02-11 01:57

I am trying to build a simple application using MapKit that will automatically search for a specific place when the app launches and drop pin(s) on the map at the locations(s).

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-11 02:36

    i know its late but hope this helps!

    [super viewDidLoad];
    [self.searchDisplayController setDelegate:self];
    [self.ibSearchBar setDelegate:self];
    
    self.ibMapView.delegate=self;
    
    // Zoom the map to current location.
    [self.ibMapView setShowsUserLocation:YES];
    [self.ibMapView setUserInteractionEnabled:YES];
    [self.ibMapView setUserTrackingMode:MKUserTrackingModeFollow];
    
    
    
    CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate=self;
    
    [locationManager startUpdatingLocation];
    [self.ibMapView setRegion:MKCoordinateRegionMake(locationManager.location.coordinate, MKCoordinateSpanMake(0.2, 0.2))];
    
    MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
    request.region = self.ibMapView.region;
    request.naturalLanguageQuery = @"restaurant";
    
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    localSearch = [[MKLocalSearch alloc] initWithRequest:request];
    
    [localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error){
    
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    
        results = response;
        if (response.mapItems.count == 0)
            NSLog(@"No Matches");
        else
            for (MKMapItem *item in response.mapItems)
            {
                NSLog(@"name = %@", item.name);
                NSLog(@"Phone = %@", item.phoneNumber);
    
                [_matchingItems addObject:item];
                MKPointAnnotation *annotation =
                [[MKPointAnnotation alloc]init];
                annotation.coordinate = item.placemark.coordinate;
                annotation.title = item.name;
                [self.ibMapView addAnnotation:annotation];
            }
    }];
    

    }

提交回复
热议问题