如何在模拟器中获取ios中的当前位置?

时间:2016-06-24 09:25:55

标签: ios objective-c currentlocation

我正在使用此代码获取当前位置但未获得正确的结果以获取模拟器中的当前位置,

-(void)initLocationManager
{
    locationManager=[[CLLocationManager alloc] init];
    locationManager.delegate = self;
    if (([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]))
    {
        [locationManager requestWhenInUseAuthorization];
    }
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.distanceFilter = kCLDistanceFilterNone;

    //[locationManager requestWhenInUseAuthorization];
    // [locationManager startMonitoringSignificantLocationChanges];
    [locationManager startUpdatingLocation];

}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation* location = [locations lastObject];
    //   NSDate* eventDate = location.timestamp;
    // NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];

    latitud = location.coordinate.latitude;
    longitud = location.coordinate.longitude;
    NSLog(@"%f,%f",latitud,longitud);
    [locationManager stopUpdatingLocation];

}

请告诉我如何获取当前位置,我从早上开始厌倦了。请帮帮我

3 个答案:

答案 0 :(得分:8)

使用以下步骤创建GPX文件:

  • 进入项目 - >添加新内容 - > iOS - >资源并选择GPX文件

enter image description here

  • 现在您需要在GPX文件中执行以下小代码:
<!--
        Provide one or more waypoints containing a latitude/longitude pair. If you provide one
        waypoint, Xcode will simulate that specific location. If you provide multiple waypoints,
        Xcode will simulate a route visitng each waypoint.
 -->
<wpt lat="37.331705" lon="-122.030237">  // here change lat long to your lat long
     <name>Cupertino</name> // here set name 

     <!--   
            Optionally provide a time element for each waypoint. Xcode will interpolate movement
            at a rate of speed based on the time elapsed between each waypoint. If you do not provide
            a time element, then Xcode will use a fixed rate of speed.

            Waypoints must be sorted by time in ascending order.
      -->
     <time>2014-09-24T14:55:37Z</time>
</wpt>

  • 现在转到编辑架构

    enter image description here

  • 然后选择run并在Option菜单中选择如下所示,我们的GPX文件会选择并运行并关闭:

enter image description here

现在您可以在GPX文件中获取已添加lat的位置。

<强>更新

以下是启用位置的代码:

  • 在info.plist中,您需要设置关于位置服务的NSLocationWhenInUseUsageDescriptionNSLocationWhenInUseUsageDescription

NSLocationWhenInUseUsageDescription

现在你的.h课

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController<CLLocationManagerDelegate>
{

    __weak IBOutlet UILabel *lblLat;
    __weak IBOutlet UILabel *lblLong;
}
@property(strong,nonatomic) CLLocationManager *locationManager;
@end

.M class

- (void)viewDidLoad {
    [super viewDidLoad];

    self.locationManager = [[CLLocationManager alloc] init];
    [self.locationManager requestWhenInUseAuthorization];


    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    [self.locationManager startUpdatingLocation];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError: %@", error);
    UIAlertView *errorAlert = [[UIAlertView alloc]
                               initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

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

工作演示代码:https://github.com/nitingohel/UserCurrentLocation_Objc

答案 1 :(得分:3)

在模拟器中,您需要在调试区域中选择位置模拟

enter image description here

或编辑方案

enter image description here

或者您可以添加自己的坐标

的GPX文件

答案 2 :(得分:2)

您无法直接在模拟器中获取当前位置,您必须转到模拟器Debug>location>custom中的位置并设置纬度和经度。