在我的iphone应用程序中,我必须在地图视图中显示附近的餐馆,目前我在网络视图中使用Google地图网址。
但是,如何在当地位置5000米的当地MKMap视图中显示附近的餐馆(我发现我当前的位置-lat& long)
我想学习如何在下面的屏幕截图中实现(通过单击注释到其详细信息)
有帮助吗?提前谢谢
答案 0 :(得分: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];
}];
我的自定义注释只是MKPlacemark
加上标题和副标题:
@interface CustomAnnotation : MKPlacemark
@property (strong, nonatomic) NSString *title;
@property (strong, nonatomic) NSString *subtitle;
@property (strong, nonatomic) NSString *phone;
@end
如果您想在标注上看到披露指示(因此您可以转换到另一个控制器查看详细信息,您可以:
- (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;
}
如果您想在用户点击标注附件时打开其他视图:
- (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
{
}
有关更多信息(例如,自定义注释视图,确定设备位置等),请参阅Location Awareness Programming Guide。
有关简单示例,请参阅https://github.com/robertmryan/MKMapView-custom-annotations。
答案 1 :(得分:0)
使用google api以json
或xml
格式
答案 2 :(得分:0)
首先参考此Google API链接https://developers.google.com/places/documentation/search,然后获取ID并将其添加到 googleId 的参数中,并将 radious 参数设置为5000,并为< strong>类型设定价值餐厅..
请参阅我的吼叫示例..
NSString *str=[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=5000&types=%@&sensor=false&key=%@",currentLocation.latitude,currentLocation.longitude,@"restaurant",@"AIzaSyB7YTFTYyk9eb8ULNGxoy06-b_0DUOqdrY"];
NSLog(@"\n\n URL %@",str);
NSURL *strurl=[NSURL URLWithString:[str stringByReplacingOccurrencesOfString:@"|" withString:@"%7C"]];
// NSURL *strurl = [NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&types=food&name=harbour&sensor=false&key=AIzaSyB7YTFTYyk9eb8ULNGxoy06-b_0DUOqdrY"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:strurl];
[request setDelegate:self];
[request startAsynchronous];
并在MKMapView
上实现这些整个记录,如下所述......
- (void)requestFinished:(ASIHTTPRequest *)request {
// Use when fetching text data
NSString *responseString = [request responseString];
NSLog(@"\n\n>>>>......Response String >>> %@",responseString);
if(responseString)
{
SBJSON *json = [[SBJSON alloc] init];
NSError *error = nil;
id result = [json objectWithString:responseString error:&error];
if ([result isKindOfClass:[NSMutableArray class]])
{
NSLog(@"\n\n.......This is Mutable Array");
}
else
{
if ([[result objectForKey:@"results"] count]>0)
{
NSLog(@">>>>>>>> dict keys :%d \nFull Address : %@\n LatLong : %@ \n total result : %d", [[result objectForKey:@"results"] count],[[result objectForKey:@"results"]valueForKey:@"vicinity"],[[[result objectForKey:@"results"]valueForKey:@"geometry"]valueForKey:@"location"],[[result objectForKey:@"results"]count]);
for (int i=0; i<[[result objectForKey:@"results"] count]; i++)
{
ann = [[MyAnnotation alloc] init];
ann.title = [[[result objectForKey:@"results"]objectAtIndex:i]valueForKey:@"name"];
ann.subtitle = [[[result objectForKey:@"results"]objectAtIndex:i]valueForKey:@"vicinity"];
ann.annReferance=[[[result objectForKey:@"results"]objectAtIndex:i]valueForKey:@"reference"];
CLLocationCoordinate2D location;
location.latitude = [[[[[[result objectForKey:@"results"]objectAtIndex:i]valueForKey:@"geometry"]valueForKey:@"location"] valueForKey:@"lat"]doubleValue];
location.longitude = [[[[[[result objectForKey:@"results"]objectAtIndex:i]valueForKey:@"geometry"]valueForKey:@"location"] valueForKey:@"lng"] doubleValue];
ann.coordinate=location;
ann.annLocation=[NSString stringWithFormat:@"%f,%f",location.latitude,location.longitude];
// NSLog(@"\n\n rating %@",ann.annrating);
if ([[[[result objectForKey:@"results"]objectAtIndex:i] allKeys] containsObject:@"rating"])
{
// contains key
ann.annrating=[[[[result objectForKey:@"results"]objectAtIndex:i]valueForKey:@"rating"]stringValue];
NSLog(@"\n\n rating %@",ann.annrating);
}
else
{
ann.annrating=@"";
}
ann.annaddress=[[[result objectForKey:@"results"]objectAtIndex:i]valueForKey:@"vicinity"];
[mapView addAnnotation:ann];
}
}
}
}
}
这是我在我的应用程序中使用的代码,只是根据您的要求进行一些更改,您将获得输出..
我希望这对你有帮助......