CLLocationCoordinate2D 当前中心;当我在 ViewDidLoad 中使用我的方法时不返回 lat/long 如果我使用它 viewDidAppear 它可以工作,但 lat/long 来自其他地方(不准确)。我想在 viewDidLoad 或 viewDidApear 上执行 [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"];
}