Trong iOS 6.1, bạn có thể sử dụng MKLocalSearch
, một phần của tiêu chuẩn 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];
}];
chú thích tùy chỉnh của tôi chỉ là một MKPlacemark
cộng với một tiêu đề và phụ đề:
@interface CustomAnnotation : MKPlacemark
@property (strong, nonatomic) NSString *title;
@property (strong, nonatomic) NSString *subtitle;
@property (strong, nonatomic) NSString *phone;
@end
Nếu bạn muốn xem chỉ báo tiết lộ trên chú dẫn của bạn (để bạn có thể chuyển sang bộ điều khiển khác để xem chi tiết, bạn có thể:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)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;
}
Nếu bạn muốn mở xem khác của bạn khi người dùng nhấp vào các phụ kiện callout:
- (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
{
}
Để biết thêm thông tin (ví dụ tùy chỉnh chế độ xem chú thích, xác định vị trí thiết bị, v.v.), tham khảo Location Awareness Programming Guide.
Xem https://github.com/robertmryan/MKMapView-custom-annotations cho một ví dụ đơn giản.
Googling trên Google Place API – Rushabh
no..Tôi không nói về API Google place, mà tôi có thể hiển thị nó trên WEBView, tôi muốn triển khai trên bản đồ iOS –
http: // stackoverflow.com/questions/7387154/là-có thể-cho-tìm kiếm-cho-gần-nhà hàng-và-thanh-qua-api-cho-iphone-sdk – Rushabh