1

我对 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 中列出餐厅的名称及其图像。代码和解释更可取。感谢您的亲切指导。

4

0 回答 0