如何在模拟器中获取 ios 中的当前位置?
How to get current location in ios in simulator?
我正在使用此代码获取当前位置,但没有得到正确的结果来获取模拟器中的当前位置,
-(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];
}
请告诉我如何获得这个当前位置,我从早上开始尝试这个就很累了。请帮助我
模拟器中无法直接获取当前位置,必须到模拟器Debug>location>custom
中定位并设置经纬度。
在模拟器中你需要在Debug Area中选择location simulation
或在编辑方案中
或者您可以使用坐标添加自己的 GPX 文件
使用以下步骤创建 GPX 文件:
- 进入项目-->添加新的-->iOS-->资源和selectGPX文件
- 现在您需要在 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>
现在去编辑架构
和 select run
并在 Option
菜单中执行 select 如下所示,出现我们的 GPX 文件 select 并且运行 并关闭:
就是这样,现在您可以在 GPX 文件中获取添加的经纬度的位置。
更新
以下是启用位置的代码:
- 在 info.plist 中,您需要设置
NSLocationWhenInUseUsageDescription
和 NSLocationWhenInUseUsageDescription
关于定位服务。
现在你的.h class
#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
我正在使用此代码获取当前位置,但没有得到正确的结果来获取模拟器中的当前位置,
-(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];
}
请告诉我如何获得这个当前位置,我从早上开始尝试这个就很累了。请帮助我
模拟器中无法直接获取当前位置,必须到模拟器Debug>location>custom
中定位并设置经纬度。
在模拟器中你需要在Debug Area中选择location simulation
或在编辑方案中
或者您可以使用坐标添加自己的 GPX 文件
使用以下步骤创建 GPX 文件:
- 进入项目-->添加新的-->iOS-->资源和selectGPX文件
- 现在您需要在 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>
现在去编辑架构
和 select
run
并在Option
菜单中执行 select 如下所示,出现我们的 GPX 文件 select 并且运行 并关闭:
就是这样,现在您可以在 GPX 文件中获取添加的经纬度的位置。
更新
以下是启用位置的代码:
- 在 info.plist 中,您需要设置
NSLocationWhenInUseUsageDescription
和NSLocationWhenInUseUsageDescription
关于定位服务。
现在你的.h class
#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