Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 点击按钮时获取用户位置_Ios_Iphone_Gps_Location_Core Location - Fatal编程技术网

Ios 点击按钮时获取用户位置

Ios 点击按钮时获取用户位置,ios,iphone,gps,location,core-location,Ios,Iphone,Gps,Location,Core Location,在我的应用程序中,有一个按钮可以将记录添加到数据库中。用户每次点击该按钮时,都会向数据库中添加一条记录。该记录的一部分应该是用户点击按钮时的位置 所以,在我的代码中,当点击按钮时,我调用一个跟踪用户位置的方法。问题是iPhone无法立即找到用户的位置,所以记录保存时没有坐标(如果我在大约10秒后创建另一个记录,一切正常) 只有当用户按下按钮并在可用时添加坐标时,才有方法跟踪用户吗 @property(nonatomic,strong) CLLocationManager *locationMan

在我的应用程序中,有一个按钮可以将记录添加到数据库中。用户每次点击该按钮时,都会向数据库中添加一条记录。该记录的一部分应该是用户点击按钮时的位置

所以,在我的代码中,当点击按钮时,我调用一个跟踪用户位置的方法。问题是iPhone无法立即找到用户的位置,所以记录保存时没有坐标(如果我在大约10秒后创建另一个记录,一切正常)

只有当用户按下按钮并在可用时添加坐标时,才有方法跟踪用户吗

@property(nonatomic,strong) CLLocationManager *locationManager;
在ViewDidLoad中

self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate=self;
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
{
    [self.locationManager requestAlwaysAuthorization];
}

if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
{
    [self.locationManager requestWhenInUseAuthorization];
}
然后

-(iAction)按钮\u点击:(id)发送者
{
[self.locationManager startUpdatingLocation];
}
#pragma标记-位置管理器委托
-(无效)locationManager:(CLLocationManager*)经理
didUpdateLocations:(NSArray*)位置
{
CLLocation*location=[locations firstObject];
CLLocationCoordinate2D坐标=location.coordinate;
}
在ViewDidLoad中

self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate=self;
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
{
    [self.locationManager requestAlwaysAuthorization];
}

if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
{
    [self.locationManager requestWhenInUseAuthorization];
}
然后

-(iAction)按钮\u点击:(id)发送者
{
[self.locationManager startUpdatingLocation];
}
#pragma标记-位置管理器委托
-(无效)locationManager:(CLLocationManager*)经理
didUpdateLocations:(NSArray*)位置
{
CLLocation*location=[locations firstObject];
CLLocationCoordinate2D坐标=location.coordinate;
}