CLLocationCoordinate2D currentCentre; ViewDidLoad でメソッドを使用すると緯度/経度が返されない viewDidAppear を使用すると機能しますが、緯度/経度は別の場所から来ています (正確ではありません)。私がしたいのは、viewDidLoad または viewDidApear execute [self queryGooglePlaces:@"grocery"]; にあります。これは初めてです よろしくお願いします
-(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//Back Button
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
[[self navigationItem] setBackBarButtonItem:back];
self.mapView.delegate = self;
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[self.mapView setShowsUserLocation:YES];
firstLaunch=YES;
imageName=[[NSString alloc] init];
imageName=[NSString stringWithFormat:@"park.png"];
[self queryGooglePlaces:@"grocery"];
}
-(void) queryGooglePlaces: (NSString *) googleType
{
// Build the url string we are going to sent to Google. NOTE: The kGOOGLE_API_KEY is a constant which should contain your own API key that you can 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=%@&keyword=%@&sensor=true&key=%@", currentCentre.latitude, currentCentre.longitude, [NSString stringWithFormat:@"%i", currenDist], googleType, kGOOGLE_API_KEY];
NSLog(@"test: %@",url);
//Formulate the string as URL object.
NSURL *googleRequestURL=[NSURL URLWithString:url];
//NSLog(url);
// Retrieve the results of the URL.
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL: googleRequestURL];
[self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});
}
私の.hコード
@interface MapViewController : UIViewController<MKMapViewDelegate,CLLocationManagerDelegate>
{
CLLocationManager *locationManager;
NSString *imageName;
BOOL firstLaunch;
CLLocationCoordinate2D currentCentre;
int currenDist;
}
@property (strong, nonatomic) IBOutlet MKMapView *mapView;
-(void) queryGooglePlaces: (NSString *) googleType;
@end
しかし、ボタンで呼び出すと、そのように機能します
- (IBAction)toolbarButtonPresses:(id)sender {
imageName=[[NSString alloc] init];
imageName=[NSString stringWithFormat:@"park.png"];
[self queryGooglePlaces:@"grocery"];
}