Search nearby in iOS Maps

前端 未结 3 1439
轻奢々
轻奢々 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];
            }
    }];
    

    }

    0 讨论(0)
  • 2021-02-11 02:39

    Make the following steps to display nearest places.

    Using google API find the Nearest Places Google Places API

    The google API provide LAt and Long and address Informations.

    Then Store the LAt and Long Values to Array then USing this lat and long values you can display Annotations in Apple map (iOS 6+) and Google Map (iOS 5).

    0 讨论(0)
  • 2021-02-11 02:48

    iOS >= 6.1 provides MKLocalSearch, MKLocalSearchRequest to search for natural language points of interest. Sample

    MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
    request.region = regionToSearchIn;
    request.naturalLanguageQuery = @"restaurants"; // or business name
    MKLocalSearch *localSearch = [[MKLocalSearch alloc] initWithRequest:request];
    [localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
        // do something with the results / error
    }];
    
    0 讨论(0)
提交回复
热议问题