Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.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
Iphone 获取当前纬度和经度_Iphone_Ios_Gps_Cllocationmanager - Fatal编程技术网

Iphone 获取当前纬度和经度

Iphone 获取当前纬度和经度,iphone,ios,gps,cllocationmanager,Iphone,Ios,Gps,Cllocationmanager,我想得到当前的位置,我用这个代码看到了,但是当我点击一个按钮时,我如何分配给这个纬度和经度来标记呢 -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { NSLog(@"latitudeAAAAA %f",newLocation.coordinate.latit

我想得到当前的位置,我用这个代码看到了,但是当我点击一个按钮时,我如何分配给这个纬度和经度来标记呢

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

    NSLog(@"latitudeAAAAA %f",newLocation.coordinate.latitude );
    NSLog(@"longitudeAAAA %f",newLocation.coordinate.longitude);
    [corelocation stopUpdatingLocation];
}

您需要将自己的类设置为
CLLocationManager
的委托,然后调用
startUpdatingLocation
()使其调用您提到的方法。回调将在您要求它开始更新位置后的某个时间点出现,并一直出现,直到您要求它停止。您必须为您的用例弄清楚它是否应该自己启动,但只在用户单击按钮时保存位置(或者您想用它做什么),或者更新是否应该在用户单击时启动(我不太确定您的问题的意思)


在界面生成器中,将
触碰内部
事件设置为调用
-(iAction)按钮单击:(id)发送方
(或以编程方式设置操作),然后将按钮委托给self。在
viewDidLoad
上设置您的
locationManager

- (void)viewDidLoad {
    [super viewDidLoad];
    self.locationManager = [[[CLLocationManager alloc] init] autorelease];
    [locationManager setDelegate:self];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [locationManager setDistanceFilter:kCLDistanceFilterNone];
    [locationManager startUpdatingLocation];
}
按钮操作:

- (IBAction)buttonClicked:(id)sender {
    myLabel.text = [NSString stringWithFormat:@"%+.6f,%+.6f", locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude];
    UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Button clicked" message:myLabel.text  delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
    [alert show];

}
位置管理器委派方法:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    NSDate* eventDate = newLocation.timestamp;
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
    if (abs(howRecent) < 5.0) {
        NSLog(@"New Latitude %+.6f, Longitude %+.6f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
    }
}
-(void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*)oldLocation{
NSDate*eventDate=newLocation.timestamp;
NSTimeInterval howRecent=[eventDate timeIntervalSinceNow];
if(防抱死制动系统(最新)<5.0){
NSLog(@“新纬度%+.6f,经度%+.6f”,新位置.坐标.纬度,新位置.坐标.经度);
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    NSDate* eventDate = newLocation.timestamp;
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
    if (abs(howRecent) < 5.0) {
        NSLog(@"New Latitude %+.6f, Longitude %+.6f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
    }
}