2013-05-31 162 views
-2

我想收到的坐标用户当前的位置,我进入了这个代码:用户当前位置

-(void)update{ 
    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    locationManager.delegate = self; 
    mapView.showsUserLocation = YES; 
    [locationManager startUpdatingLocation]; 
} 

在iOS模拟器,该代码崩溃,我的设备是不是有时它崩溃了整个应用程序....你能帮助我发生什么事吗?

+0

你是什么意思的“它是失败”在模拟器上? “你的整个应用程序失败”是什么意思?你能描述你遇到的崩溃/错误吗? – Rob

+0

该模拟器不能提供整个gps expereince。它具有提供位置的基本功能。例如。在这里:http://stackoverflow.com/questions/12641817/ios-simulator-and-gps –

+0

请更清楚一点不工作。你期待什么,没有发生什么? – Daniel

回答

1

看起来也许你没有实施CLLocationManagerDelegate方法。

特别是在之前iOS版本中的-locationManager:didUpdateLocations:(来自iOS 6)或-locationManager:didUpdateToLocation:fromLocation:

也许(你的问题再次模糊)你只想在地图视图上显示蓝点。在这种情况下,您无需处理核心位置管理器。查看this post了解更多信息。

+0

失败这个词意味着崩溃。我需要CLLLocationmanager来获得我设计的独特功能,而且它是Xcode。 Thks for your help –

+0

所以告诉我你的功能在技术上需要实现。没有更多的技术信息,没有人能帮助你。 – Daniel

1

首先您无法在模拟器中获取当前位置。并尝试使用此代码获取当前位置的坐标。

CLLocationManager *locationManager = [[CLLocationManager alloc] init]; 
    if ([CLLocationManager locationServicesEnabled]) 
    { 
     locationManager.delegate = self; 
     locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
     locationManager.distanceFilter = kCLDistanceFilterNone; 
     [locationManager startUpdatingLocation]; 
    } 

    location = [locationManager location]; 
    CLLocationCoordinate2D coordinate = [location coordinate];; 
    MKCoordinateRegion region; 
    region.center=coordinate; 
    MKCoordinateSpan span; 
    span.latitudeDelta=10.015; 
    span.longitudeDelta=10.015; 
    region.span=span; 
    [mapView setRegion:region]; 

    NSString *str=[[NSString alloc] initWithFormat:@" latitude:%f longitude:%f",coordinate.latitude,coordinate.longitude]; 
    NSLog(@"%@",str);