【发布时间】:2015-04-04 23:36:02
【问题描述】:
我正在构建一个从网络服务器读取一些数据的小型应用程序,它可以正常工作。然后,我制作了一种无效的方法,只能从某个地址获取纬度和经度。
-(void)getLocation{
if (!self.geocoder) {
self.geocoder = [[CLGeocoder alloc] init];
}
NSString *address = self.label1.text;
[self.geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
if ([placemarks count] > 0) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
CLLocation *location = placemark.location;
CLLocationCoordinate2D coordinate = location.coordinate;
latitude1 = coordinate.latitude;
longitude1 = coordinate.longitude;
}
}];
}
在 .h 文件中,我已将这些声明为变量(不在界面中):
@property double latitude1;
@property double longitude1;
然后我用一个按钮调用它:
- (IBAction)find:(id)sender{
NSLog(@"Latitude1: %f og Longitude1: %f", latitude1, longitude1);
float spanX = 0.00725;
float spanY = 0.00725;
MKCoordinateRegion region;
region.center.latitude = self.latitude1;
region.center.longitude = self.longitude1;
region.span = MKCoordinateSpanMake(spanX, spanY);
[self.mapView setRegion:region animated:YES];
}
这很好用....
但我就是不能这样称呼:
NSLog(@"Latitude: %f and %f",latitude1, longitude1 );
来自 viewDidLoad 方法。
就像我必须在 IBaction 中使用它,并且我不能以任何其他方式使用 latitude1 和 longitude1。我需要的是获取纬度和经度并能够使用我想要的值,而不仅仅是通过按下按钮。
它只是没有意义,如果我手动按下更新按钮,它就会工作,然后它会在地图上显示东西并显示纬度和经度,但是当我在 viewdidload 方法中使用它时,它只显示 0.00000。
我一直在考虑的另一件事是如何从一个方法返回多个值,例如我想从我的 getLocation 方法返回 latitude1 和 longitude1。 nsdictionary 还是数组还是结构体?
【问题讨论】:
标签: ios objective-c xcode geolocation