iphone - 在 MKMap View 中显示附近的餐馆

标签 iphone ios mkmapview mapkit

在我的 iPhone 应用程序中,我必须在 map View 中显示附近的餐厅,目前我在 WebView 中使用 Google map url。

但是我如何使用本地 MKMap View 显示当前位置 5000 米以内的附近餐馆(我找到了我当前的位置 - 纬度和经度)

我想学习如何在下面的屏幕截图中实现(也可以通过单击注释转到其详细信息)

有什么帮助吗??提前致谢

enter image description here

最佳答案

在 iOS 6.1 中,您可以使用 MKLocalSearch ,标准 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];
}];

我的自定义注释只是一个 MKPlacemark 加上标题和副标题:

@interface CustomAnnotation : MKPlacemark

@property (strong, nonatomic) NSString *title;
@property (strong, nonatomic) NSString *subtitle;
@property (strong, nonatomic) NSString *phone;

@end

如果你想在你的标注上看到披露指示器(这样你就可以转换到另一个 Controller 来查看详细信息,你可以:

- (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;
}

如果您想在用户点击标注附件时打开您的其他 View :

- (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
{

}

有关更多信息(例如自定义注释 View 、确定设备位置等),请参阅 Location Awareness Programming Guide .

参见 https://github.com/robertmryan/MKMapView-custom-annotations举个简单的例子。

关于iphone - 在 MKMap View 中显示附近的餐馆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14950896/

相关文章:

ios - 从 AppDelegate 自定义每个 UISearchBar 不工作

iphone - MKPinAnnotationView 上未显示 rightCalloutAccessoryView

objective-c - 消除 MKMapView 中的引脚重叠

iOS 应用被拒绝 - 您的应用可以在 map 上显示附近用户的位置,但没有采取必要的隐私预防措施

objective-c - 当应用程序在 iOS 中进入非事件/后台/暂停状态时,调度队列中的任务会发生什么?

ios - AVAudioPlayer 的 NSOSStatusErrorDomain Code=-54

iphone - 子类化 UIApplication 以覆盖 sendEvent 导致崩溃

iphone - 如果应用程序未通过点击 'View' 按钮启动,则检索最新的推送通知

iphone - 如何使用 iphone SDK 中的两个按钮在运行时更改应用程序语言?

iphone - 如何在 iPhone SDK 的 TableView 中实现 "Load More Records"?