显示MKMap View附近的餐馆 [英] Showing nearby restaurants in MKMap View
问题描述
在我的iphone应用程序中,我必须在地图视图中显示附近的餐馆,目前我在网络视图中使用谷歌地图网址。
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.
但我怎么能显示附近5000米当前位置的餐馆,其中包含原生MKMap视图(我发现我当前的位置 - 纬度和长度)
But how can I show nearby restaurants with in 5000 meters of current location with native MKMap view(I found out my current location-lat&long)
我想学习如何实现在下面的屏幕截图中(通过点击其细节的注释)
I would like to learn how to implement as in the below screen shot(by clicking the annotations going to its detail too)
任何帮助?提前谢谢
推荐答案
在iOS 6.1中,您可以使用 MKLocalSearch
,标准iOS MapKit.framework的一部分:
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];
}];
我的自定义注释只是 MKPlacemark
加上标题和副标题:
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<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;
}
如果要打开其他视图当用户点击标注附件时:
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.
请参阅 https://github.com/robertmryan/MKMapView-custom-注释是一个简单的例子。
See https://github.com/robertmryan/MKMapView-custom-annotations for a simple example.
这篇关于显示MKMap View附近的餐馆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!