0

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

}    
4

2 回答 2

0

您需要调用[locationManager startUpdatingLocation];viewDidLoad然后通过查询 location 属性来检查它是否返回了有效位置 - 检查它是否不为零。返回结果可能需要一段时间,因此当您的其余代码执行时,没有有效值。您也许可以使用– locationManager:didUpdateToLocation:fromLocation: – locationManager:didFailWithError:CLLocationManagerDelegate 协议中的方法来处理此问题。

于 2012-06-29T22:36:17.743 回答
0

请检查您不在模拟器中的设备中的运行代码 模拟器无法给出正确的纬度/日志位置

于 2012-06-30T07:05:24.783 回答