尝试使用纬度和经度发送POST参数

时间:2015-05-15 13:27:21

标签: ios objective-c location cllocationmanager

我正在尝试将一些参数发送到我的phpMyAdmin表,其中包含一个名为纬度和经度的文件。从那时起,我需要获取设备坐标并将它们转换为字符串。

我遵循了Ray Wenderlich教程,但我的参数仍然是“nil”。看看下面的代码:

1 - 我已将框架添加到代码中并将CoreLocation文件导入到我的“ResumeView.h”中,同时声明了纬度和经度字符串。

#import <CoreLocation/CoreLocation.h>

@interface ResumeView : GAITrackedViewController <UIAlertViewDelegate, CLLocationManagerDelegate>
{
NSString *latitude;
NSString *longitude;

2 - 现在,在我的“ResumeView.m”上,我添加了其余的代码,然后是教程......

@implementation ResumeView {

    CLLocationManager *manager;

}

@synthesize latitude;
@synthesize longitude;

3 - 现在,在viewDidLoad ...

- (void)viewDidLoad
{
    [super viewDidLoad];

    manager = [[CLLocationManager alloc] init];

    manager.delegate = self;

    manager.desiredAccuracy = kCLLocationAccuracyBest;

    [manager startUpdatingLocation];

4 - 然后,在我的代码中引入了这些函数,当然,在发送POST参数之前。

#pragma mark CLLocationManagerDellegate Methods

-(void) locationManager:(CLLocationManager *) manager didFailWithError: (NSError *)error {

    NSLog(@"error: %@", error);
    NSLog(@"Failed to get location! :(");
}

-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

    NSLog(@"Location: %@", newLocation);

    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil) {
        latitude = [NSString stringWithFormat: @"%.8f", currentLocation.coordinate.latitude];
        longitude = [NSString stringWithFormat: @"%.8f", currentLocation.coordinate.longitude];
    }
}

5 - 这就是问题所在,两个坐标值都是“nil”,无法发送到数据库。

 @"latitude":latitude, // <---- error here! is nil when sending to database!
 @"longitude":longitude, // <---- error here! is nil when sending to database!

1 个答案:

答案 0 :(得分:1)

您的值为零,因为位置管理员从不向您发送更新。您需要在iOS 8中调用新功能才能使位置管理器正常工作。请参阅此帖子作为起点:https://stackoverflow.com/a/24063578/78496