0

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

}    
4

2 に答える 2

0

シミュレーターにないデバイスで実行中のコードを確認してください

于 2012-06-30T07:05:24.783 に答える
0

[locationManager startUpdatingLocation];呼び出してviewDidLoadから、場所のプロパティをクエリして有効な場所が返されたことを確認する必要があります-それが nil でないことを確認します。結果を返すのに時間がかかる可能性があるため、コードの残りの部分が実行されたときに有効な値がありません。– locationManager:didUpdateToLocation:fromLocation: – locationManager:didFailWithError:これを処理するには、CLLocationManagerDelegate プロトコルのメソッドを使用できる場合があります。

于 2012-06-29T22:36:17.743 に答える