Iphone 在地图视图中显示附近的餐厅
在我的iphone应用程序中,我必须在地图视图中显示附近的餐馆,目前我正在网络视图中使用谷歌地图url 但是我如何用本地MKMap视图(我找到了我的当前位置lat&long)显示当前位置5000米以内的附近餐馆 我想学习如何在下面的屏幕截图中实现(通过单击注释也转到其细节) 有什么帮助吗??提前谢谢Iphone 在地图视图中显示附近的餐厅,iphone,ios,mkmapview,mapkit,Iphone,Ios,Mkmapview,Mapkit,在我的iphone应用程序中,我必须在地图视图中显示附近的餐馆,目前我正在网络视图中使用谷歌地图url 但是我如何用本地MKMap视图(我找到了我的当前位置lat&long)显示当前位置5000米以内的附近餐馆 我想学习如何在下面的屏幕截图中实现(通过单击注释也转到其细节) 有什么帮助吗??提前谢谢 首先参考此Google API链接,然后获取id并将其添加到Google id的参数中,只需将radious参数设置为5000值,并为类型设置值 看我下面的例子 NSString *str=[NSS
首先参考此Google API链接,然后获取id并将其添加到Google id的参数中,只需将radious参数设置为5000值,并为类型设置值 看我下面的例子
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];
}
}
}
}
}
-(void)requestFinished:(ASIHTTPRequest*)请求{
//获取文本数据时使用
NSString*responseString=[request responseString];
NSLog(@“\n\n>>>>>…响应字符串>>>%@”,响应字符串);
if(响应预算)
{
SBJSON*json=[[SBJSON alloc]init];
n错误*错误=nil;
id结果=[json objectWithString:responseString错误:&error];
if([result iskindof类:[NSMutableArray类]])
{
NSLog(@“\n\n…….这是可变数组”);
}
其他的
{
如果([[result objectForKey:@“results”]计数]>0)
{
NSLog(@“>>>>>>>>>>>目录键:%d\n完整地址:%@\n LatLong:%@\n总结果:%d“,[[result objectForKey:@“results”]计数],[result objectForKey:@“results”]valueForKey:@“geometry”],[[result objectForKey:@“results”]计数],[[result objectForKey:@“results”]计数];
对于iOS 6.1中的(int i=0;i,您可以使用标准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;
}
有关更多信息(例如自定义注释视图、确定设备位置等),请参阅
请参阅一个简单的示例。在Google Place API上进行谷歌搜索。我不是在谈论Google Place API,我可以在网络视图上显示它,我希望在iOS地图上实现它。这已经非常过时了。只需使用MKMapKit,它现在完全内置于iOS中。您最欢迎的只是几行代码:)只需创建MyAnnotation类或其他要显示的参数..但是…什么是ASIFormDataRequest?哦,我使用ASIHTTPRequest发送请求并从api获取数据..只需在当前类的.m文件中添加ASIHTTPRequest和add.h文件,它是一个用于向服务器发送请求并获取数据的类…谢谢工作……:)我可以按如下方式将标题传递到详细页面,如何传递副标题?请查看这个问题@NithinMK您将以传递标题的相同方式传递标题。我个人认为传递注释本身更容易,这样详细页面就可以访问注释的所有属性。如果您仍然如果您有任何问题,请使用您的代码发布一个新问题以创建注释,并使用您的代码启动详细信息页面,我们可以为您提供进一步帮助。请在此处随意添加指向新问题的链接,以便在您发布时我可以看到它。
- (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
{
}