Ios 目标C定位服务地理代码返回值
我正在尝试编写一个方法,该方法获取地理编码,并在方法返回时返回位置标记。我得到位置管理器部分;它等待查找位置,然后运行停止更新。然后我运行地理编码,这是我有最多问题的地方。首先,块表示法是什么意思?我似乎从来没有从中得到任何回报。它在位置标记中得到了位置,但我想把位置信息传回去 我的目标是在视图上创建一个按钮,一个开始查找位置并将开始位置写入表,另一个按钮获取结束位置并将其写入表。我想重用获取位置的代码块 因此,在pseudo中,我需要 按钮AIos 目标C定位服务地理代码返回值,ios,objective-c,ios7,Ios,Objective C,Ios7,我正在尝试编写一个方法,该方法获取地理编码,并在方法返回时返回位置标记。我得到位置管理器部分;它等待查找位置,然后运行停止更新。然后我运行地理编码,这是我有最多问题的地方。首先,块表示法是什么意思?我似乎从来没有从中得到任何回报。它在位置标记中得到了位置,但我想把位置信息传回去 我的目标是在视图上创建一个按钮,一个开始查找位置并将开始位置写入表,另一个按钮获取结束位置并将其写入表。我想重用获取位置的代码块 因此,在pseudo中,我需要 按钮A placemark objectFoo=调用并获取
placemark objectFoo=调用并获取我的位置
objectFoo写入数据库或执行任何操作 按钮B
placemark objectFoo=调用并获取我的位置
objectFoo写入数据库或执行任何操作 来自Java的我对这一块
reverseGeocodeLocation:currentLocation
是什么以及如何修改它感到困惑?我想象一个物体,它移动并得到我的位置,然后把它交回任何我喜欢的东西
- (CLPlacemark *)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
_status.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
NSLog(@"longitude: %@", [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]);
_status.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
NSLog(@"latitude: %@", [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]);
}
// Stop Location Manager
[_locationManager stopUpdatingLocation];
NSLog(@"Resolving the Address");
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
if (error == nil && [placemarks count] > 0) {
placemark = [placemarks lastObject];
_startLocation.text = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@",
placemark.subThoroughfare, placemark.thoroughfare,
placemark.postalCode, placemark.locality,
placemark.administrativeArea,
placemark.country];
NSLog(@"%@", placemark.subThoroughfare); // address number
NSLog(@"%@", placemark.thoroughfare); // address St
NSLog(@"%@", placemark.postalCode); // zip
NSLog(@"%@", placemark.locality); // city
NSLog(@"%@", placemark.administrativeArea); // state
NSLog(@"%@", placemark.country); // country
NSLog(@" debug county == %@", placemark.country); // country
} else {
NSLog(@"%@", error.debugDescription);
//return placemark;
}
//return placemark;
} ];
NSLog(@" needs to log this to see placemerk country == %@", placemark.country);
return placemark; //placemark object?
}
Objective C中的[]符号可能意味着一些事情,但在本例中,我认为您指的是对象上的方法调用。所以
[myObject dosomething white:i]
与myObject.doSomethingWithInt(i)相同代码>在Java中。苹果有一本Objective c指南,iOS和Objective-c标签的信息页面上还有许多其他介绍和教程
就对按钮按下的响应而言,它与Java中的事件模型非常相似。您需要在脚本或xib上的按钮与UIViewController
实例中的事件之间建立链接。同样,最好是通过一些示例和教程来学习,但本质上是你自己
- 创建子类
UIViewController
- 在子类的.h文件中声明iAction方法
- 在序列图像板或XIB文件中的视图中,将视图的控制器类设置为子类
- 将按钮添加到视图后,您可以从按钮上的“内部润色”事件拖动到视图控制器,并将其链接到相应的iAction方法
CLLocationmanagerDelegate
协议的委托方法之一。您可以在.h文件中为包含此方法的类声明一些@property
变量,并将其更改为-
在您的.h文件中
@property (retain,nonatomic) CLPlacemark *currentPlacemark;
在你的.m文件中
- (CLPlacemark *)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
if (error == nil && [placemarks count] > 0) {
self.currentPlacemark = [placemarks lastObject];
}
}
}
在viewController子类.h文件中
@property (strong,nonatomic) *MyLocationMonitor *locationMonitor;
在您的..m文件中
- (void) viewDidLoad
{
[super viewDidLoad];
self.locationMonitor=[[MyLocationMonitor alloc]init];
}
要显示当前位置,然后单击按钮时,可以执行以下操作
-(IBAction)button1Clicked:(id)sender {
CLLocation *currentLocation=self.locationMonitor.currentPlacemark;
// TODO - Do something
}
虽然我不能像你说的那样让它工作,但我有足够的外卖来修复我的东西。我只是习惯了所有的东西是如何组合在一起的,但还没有完全适应。