显示MKMap View附近的餐馆

8 iphone mapkit mkmapview ios

在我的iPhone应用程序中,我必须在地图视图中显示附近的餐馆,目前我在网络视图中使用谷歌地图网址.

但是,我怎样才能在5000米的当前位置显示附近的餐馆,并使用原生的MKMap视图(我发现了我当前的位置-lat&long)

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

有帮助?? 提前致谢

在此输入图像描述

Rob*_*Rob 14

在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];
}];
Run Code Online (Sandbox Code Playgroud)

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

@interface CustomAnnotation : MKPlacemark

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

@end
Run Code Online (Sandbox Code Playgroud)

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

- (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;
}
Run Code Online (Sandbox Code Playgroud)

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

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

}
Run Code Online (Sandbox Code Playgroud)

有关更多信息(例如,自定义注释视图,确定设备位置等),请参阅" 位置感知编程指南".

有关简单示例,请参阅https://github.com/robertmryan/MKMapView-custom-annotations.