我正在使用此代码获取当前位置但未获得正确的结果以获取模拟器中的当前位置,
-(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];
}
请告诉我如何获取当前位置,我从早上开始厌倦了。请帮帮我
答案 0 :(得分:8)
使用以下步骤创建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>
现在您可以在GPX文件中获取已添加lat的位置。
<强>更新强>
以下是启用位置的代码:
NSLocationWhenInUseUsageDescription
和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)
答案 2 :(得分:2)
您无法直接在模拟器中获取当前位置,您必须转到模拟器Debug>location>custom
中的位置并设置纬度和经度。