如何在不使用mapview的情况下连接google api

时间:2012-09-21 07:16:06

标签: api xcode4 google-places-api

我对Xcode很新,但我想要完成的是当你打开应用程序时我们被要求填写我们对食物的品味,然后点击co按钮后我必须得到当前食物的名单在我周围,也是我在列表中的当前位置。我没有自己的数据库,我想尝试使用谷歌这个功能。 我只从这里找到了这段代码。

 -(void) queryGooglePlaces: (NSString *) googleType {
        // Build the url string to send to Google. NOTE: The kGOOGLE_API_KEY is a constant that should contain your own API key that you obtain from Google. See this link for more info:
        // https://developers.google.com/maps/documentation/places/#Authentication
        NSString *url = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=%@&types=%@&sensor=true&key=%@", currentCentre.latitude, currentCentre.longitude, [NSString stringWithFormat:@"%i", currenDist], googleType, kGOOGLE_API_KEY];

        //Formulate the string as a URL object.
        NSURL *googleRequestURL=[NSURL URLWithString:url];

        // Retrieve the results of the URL.
        dispatch_async(kBgQueue, ^{
            NSData* data = [NSData dataWithContentsOfURL: googleRequestURL];
            [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
        });
    }


-(void)fetchedData:(NSData *)responseData {
    //parse out the json data
    NSError* error;
    NSDictionary* json = [NSJSONSerialization 
                          JSONObjectWithData:responseData 

                          options:kNilOptions 
                          error:&error];

    //The results from Google will be an array obtained from the NSDictionary object with the key "results".
    NSArray* places = [json objectForKey:@"results"]; 

    //Write out the data to the console.
    NSLog(@"Google Data: %@", places);
}

-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    //Get the east and west points on the map so you can calculate the distance (zoom level) of the current map view.
    MKMapRect mRect = self.mapView.visibleMapRect;
    MKMapPoint eastMapPoint = MKMapPointMake(MKMapRectGetMinX(mRect), MKMapRectGetMidY(mRect));
    MKMapPoint westMapPoint = MKMapPointMake(MKMapRectGetMaxX(mRect), MKMapRectGetMidY(mRect));

    //Set your current distance instance variable.
    currenDist = MKMetersBetweenMapPoints(eastMapPoint, westMapPoint);

    //Set your current center point on the map instance variable.
    currentCentre = self.mapView.centerCoordinate;
}

实际上就像我之前提到过的,我不想在我的应用程序中使用MapView。我只想在UITableView中列出餐馆及其图像的名称。代码和解释更为可取。感谢您的亲切指导。

0 个答案:

没有答案