8

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.

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)

Any helps ?? thanks in advance

enter image description here

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Googling on Google Place API – Rushabh Feb 19 '13 at 06:04
  • no..I am not talking about google place API,that I am able to show it on a WEBView,I wish to implement it on iOS maps –  Feb 19 '13 at 06:06
  • http://stackoverflow.com/questions/7387154/is-it-possible-for-search-for-nearby-restaurants-and-bars-via-api-for-iphone-sdk – Rushabh Feb 19 '13 at 06:10
  • this is hugely out of date. Simply use MKMapKit, which is totally built-in to iOS these days. it's only a couple lines of code – Fattie Aug 31 '17 at 03:46

2 Answers2

14

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

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.

See https://github.com/robertmryan/MKMapView-custom-annotations for a simple example.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Thanks it worked...:) I can pass the title to the detail page as below,how to pass the subtitle ? Please see this question http://stackoverflow.com/questions/14954240/how-to-pass-subtitle-of-annotation-in-mkmapview –  Feb 19 '13 at 09:39
  • @NithinMK You'd pass it in the same way you'd pass the title. Personally, I think it's just easier to pass the `annotation` itself, that way the detail page can just access all of the properties of the annotation. If you're still having problems, post a new question with your code to create the annotation as well as your code to launch the details page and we can help you further. Feel free to add a link to the new question here so that I can see it when you post it. – Rob Feb 19 '13 at 15:45
0

First refer this Google API Link https://developers.google.com/places/documentation/search and then get id and add this in parameter of googleId and just set radious parameter with 5000 value and for types set value restaurant..

See my bellow example..

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]; 

and implement these whole record on MKMapView like bellow...

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

This is code which i use in my application, just do some changes with your requirement and you will get output..

i hope this helpful to you...

P.J.Radadiya
  • 1,493
  • 1
  • 12
  • 21
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • you most wel-come :) just create MyAnnotation class or something which you want to display with other parameters.. – Paras Joshi Feb 19 '13 at 06:22
  • but...What is ASIFormDataRequest?? –  Feb 19 '13 at 06:26
  • oh i use ASIHTTPRequest for send request and get data from api .. just add ASIHTTPRequest and add .h file in your current class's .m file , its a class which used for send request to server and get data back... – Paras Joshi Feb 19 '13 at 06:28