Showing nearby restaurants in MKMap View

前端 未结 2 1294
感情败类
感情败类 2021-02-01 10:55

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 met

2条回答
  •  遇见更好的自我
    2021-02-01 11:12

    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...

提交回复
热议问题