2

i am using this code to get current location but not getting the correct result to get the current location 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];

}

please tell me how to get this current location, i am getting tired trying this from the morning. please help me

user6438311
  • 117
  • 1
  • 3
  • 13

3 Answers3

9

Create a GPX file by using following steps:

  • Go in project--> Add new --> iOS -- > Resource and select GPX file

enter image description here

  • Now you need to do small code in to GPX file as following:

<!--
        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>

  • Now go to edit schema

    enter image description here

  • And select run and do in Option menu select like following there is appear our GPX file select that and run and close:

enter image description here

That's it now you can get location of your added lat long in GPX file.

UPDATE

Following is a code for enable location:

  • In info.plist you need set NSLocationWhenInUseUsageDescription and NSLocationWhenInUseUsageDescription about location services.

NSLocationWhenInUseUsageDescription

Now your .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];
    }
}

Working demo code: https://github.com/nitingohel/UserCurrentLocation_Objc

Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144
  • there must be some code for to find current location in the device and save that latitiude and longitude in some string , if yes then please share – user6438311 Jun 24 '16 at 11:21
  • but i am not getting the current location from the above code i have done every thing, i am getting zero – user6438311 Jun 24 '16 at 11:23
  • i just create an example code and attached demo as well check you are missing set enable location services – Nitin Gohel Jun 24 '16 at 11:43
  • 2016-06-24 17:23:58.880 UserCurrentLocation_Objc[4617:171406] didFailWithError: Error Domain=kCLErrorDomain Code=0 "(null)" 2016-06-24 17:23:58.972 UserCurrentLocation_Objc[4617:171406] Simulator user has requested new graphics quality: 100 this error is coming – user6438311 Jun 24 '16 at 11:54
  • http://stackoverflow.com/questions/34263626/ios-simulator-simulator-user-has-requested-new-graphics-quality-100 – Nitin Gohel Jun 24 '16 at 11:56
  • 1
    Thank you very much @Nitin Gohel – user6438311 Jun 24 '16 at 12:38
5

In simulator you need choose location simulation in Debug Area

enter image description here

or in Edit Scheme

enter image description here

or you can add your own GPX file with coordinates

iSashok
  • 2,326
  • 13
  • 21
  • there must be some code for to find current location in the device and save that latitiude and longitude in some string , if yes then please share – user6438311 Jun 24 '16 at 11:20
  • Did you entered to delegate method `didUpdateLocations` after `startUpdatingLocation` If not try to add property to your class and set it `self.locationManager = [[CLLocationManager alloc] init];` – iSashok Jun 24 '16 at 11:34
  • This is work perfectly for simulator. Any other solution to custom current location with move and search. I am successfully down this with google integration pods. but when i am customized. It dose' work. – Raksha Saini May 31 '17 at 11:34
2

You cannot get current location directly in simulator, you have to go to location in Debug>location>custom of simulator and set latitude and longitude.

Rahul Patel
  • 1,822
  • 12
  • 21
  • i am setting the location from debug>location>custom location , but still not getting the location which i am setting there – user6438311 Jun 24 '16 at 09:30
  • try this, select none first in location and then again click on custom location and set the lat and long – Rahul Patel Jun 24 '16 at 09:32
  • there must be some code for to find current location in the device and save that latitiude and longitude in some string , if yes then please share – user6438311 Jun 24 '16 at 11:20