Showing nearby restaurants in MKMap View

前端 未结 2 1289
感情败类
感情败类 2021-02-01 10:55

In my iphone app,I have to show near by restaurants in Map View,Currently I am using Google map url in a web view.

But how can I show nearby restaurants with in 5000 met

2条回答
  •  北海茫月
    2021-02-01 11:30

    In iOS 6.1, you can use MKLocalSearch, part of the standard iOS MapKit.framework:

    MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
    request.naturalLanguageQuery = @"restaurant";
    request.region = mapView.region;
    
    MKLocalSearch *localSearch = [[MKLocalSearch alloc] initWithRequest:request];
    [localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
    
        NSMutableArray *annotations = [NSMutableArray array];
    
        [response.mapItems enumerateObjectsUsingBlock:^(MKMapItem *item, NSUInteger idx, BOOL *stop) {
            CustomAnnotation *annotation = [[CustomAnnotation alloc] initWithPlacemark:item.placemark];
    
            annotation.title = item.name;
            annotation.subtitle = item.placemark.addressDictionary[(NSString *)kABPersonAddressStreetKey];
            annotation.phone = item.phoneNumber;
    
            [annotations addObject:annotation];
        }];
    
        [self.mapView addAnnotations:annotations];
    }];
    

    My custom annotation is just a MKPlacemark plus a title and subtitle:

    @interface CustomAnnotation : MKPlacemark
    
    @property (strong, nonatomic) NSString *title;
    @property (strong, nonatomic) NSString *subtitle;
    @property (strong, nonatomic) NSString *phone;
    
    @end
    

    If you want to see the disclosure indicator on your callout (so you can transition to another controller to view the details, you can:

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation
    {
        if (![annotation isKindOfClass:[CustomAnnotation class]])
            return nil;
    
        MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
                                                                           reuseIdentifier:@"CustomAnnotationView"];
        annotationView.canShowCallout = YES;
        annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    
        return annotationView;
    }
    

    If you want to open your other view when the user clicks on the callout accessory:

    - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
    {
        if (![view.annotation isKindOfClass:[CustomAnnotation class]])
            return;
        CustomAnnotation *annotation = (CustomAnnotation *)view.annotation;
    
        ABRecordRef person = ABPersonCreate();
        ABRecordSetValue(person, kABPersonOrganizationProperty, (__bridge CFStringRef) annotation.title, NULL);
    
        if (annotation.phone)
        {
            ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
            ABMultiValueAddValueAndLabel(phoneNumberMultiValue, (__bridge CFStringRef) annotation.phone, kABPersonPhoneMainLabel, NULL);
            ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, nil);
            CFRelease(phoneNumberMultiValue);
        }
    
        ABMutableMultiValueRef address = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
        ABMultiValueAddValueAndLabel(address, (__bridge CFDictionaryRef) annotation.addressDictionary, kABWorkLabel, NULL);
        ABRecordSetValue(person, kABPersonAddressProperty, address, NULL);
        ABUnknownPersonViewController *personView = [[ABUnknownPersonViewController alloc] init];
    
        personView.unknownPersonViewDelegate = self;
        personView.displayedPerson = person;
        personView.allowsAddingToAddressBook = YES;
    
        [self.navigationController pushViewController:personView animated:YES];
    
        CFRelease(address);
        CFRelease(person);
    }
    
    - (void)unknownPersonViewController:(ABUnknownPersonViewController *)unknownPersonView didResolveToPerson:(ABRecordRef)person
    {
    
    }
    

    For more information (e.g. customizing the annotation views, determining device location, etc.), refer to the Location Awareness Programming Guide.

    See https://github.com/robertmryan/MKMapView-custom-annotations for a simple example.

提交回复
热议问题