objective c find out my location? - objective-c

I'm trying to get my location in simulator. A couple of time it worked but... Now I always get 0.000000 as my location. There is code that I used:
#import <CoreLocation/CoreLocation.h>
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
NSLog(#"%f",location.coordinate.latitude);
NSLog(#"%f",location.coordinate.longitude);
After running app I change in the simulator Debug -> Location -> Custom Location... Does somebody can explain why does it stop working?

This will never work, except by accident:
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
When you call startUpdatingLocation you get delegate messages (assuming you have a location manager delegate, which you should, always) — in particular, locationManager:didUpdateLocations:. That is where you receive information about your location.

Related

CLLocation Manager work only for the application first run

CLLocation Manager work only for the application first run.
This is my code.
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if ([self.locationManager respondsToSelector:#selector(requestAlwaysAuthorization)])
{
[self.locationManager requestAlwaysAuthorization];
}
[self.locationManager startUpdatingLocation];
I found the solution, the probleme was from simulator when you restart the application the location turn to the default location which is none.
So when i restart my app i got the the error didFailWithError: Error Domain=kCLErrorDomain Code=0.
You should when reopen the application choose a new location from debug -> location, or use a device for test.
Try the freeway drive in the location debug for simulator to test your location, when you close the app location go to none.
Change it to freeway drive

Latitude and Longitude 0 when called second time in Objective-C

I am using the below code to get coordinates on click of a button
-(CLLocationCoordinate2D) getLocation{
CLLocationCoordinate2D coordinate;
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
if ([CLLocationManager locationServicesEnabled]) {
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
coordinate = [location coordinate];
}
else {
coordinate.latitude = 0.0;
coordinate.longitude = 0.0;
}
return coordinate;
}
When the button is clicked for the first time, I do get my valid coordinates, but if I click the button again, The latitude and longitude values are 0.0000
Any suggestions
Please initialize the CLLocationManager outside the method.Take the You can initialize in viewdidload method.
It may helps to you
You cannot expect [locationManager location] to return anything useful immediately after activating a location manager with [locationManager startUpdatingLocation] .
CLLocationManager takes time to acquire a location. You need to set the CLLocationManager's delegate (eg, to 'self') and then use the delegate method - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations; to get notified when the location manager has got a location that you can use.
See: https://developer.apple.com/reference/corelocation/cllocationmanagerdelegate/1423615-locationmanager?language=objc
(If you only want one location, and not a continuous stream of location updates, instead of calling 'startUpdatingLocation' call [locationManager requestLocation] - but even then you still need to use the delegate method to be notified when a location has been acquired).
You need to split out the code for creating and starting the location manager to where it is only run once (only need one location manager for the app) - eg, viewDidLoad. Then the rest of the code, to process the locations acquired, should go in the delegate method.
In reality, if you want you button click to have a location immediately, you need to have your location manager running and updating locations before your button is available to be clicked. Eg, make your button enabled=NO and when you start getting locations via the delegate method, set the button enabled=YES.

Location authorization issue in Objective C

I have an issue showing user locations in objective C.
I tried everything i could find here in stackoverflow, aaaand, didn't work.
So I have that code :
-(void)setLocation
{
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
MKPointAnnotation *myAnnotation = [[MKPointAnnotation alloc]init];
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
locationManager.distanceFilter = 10.0;
[locationManager startUpdatingLocation];
[locationManager requestWhenInUseAuthorization];
[locationManager requestAlwaysAuthorization];
myAnnotation.coordinate = mapView.userLocation.location.coordinate;
myAnnotation.title = #"Test";
myAnnotation.subtitle = #"I am a test Subtitle";
[self.mapView addAnnotation:myAnnotation];
}
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
[self.mapView setCenterCoordinate:userLocation.coordinate animated:YES];
}
Everything is in ViewController.m, more precisely in a mapView declared in my .h file:
#property (strong, nonatomic) IBOutlet MKMapView *mapView;
Does anyone have any idea ? The error i have is that:
Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.
Thanks :)
I can't post this as a comment so i'll post it here.
You can simply open your Info.plist file in TextEdit and add these lines.
<key>UIBackgroundModes</key>
<array>
<string>location</string>
<string>external-accessory</string>
<string>remote-notification</string>
</array>
<key>NSLocationUsageDescription</key>
<string>App needs to use GPS to keep track of your activity</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>App needs to use GPS to keep track of your activity</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
<string>gps</string>
</array>
EDIT:
I see that your locationManager is a local method variable. It should declared be as an instance variable or as a property.
Look at the following lines
[locationManager startUpdatingLocation];
[locationManager requestWhenInUseAuthorization];
[locationManager requestAlwaysAuthorization];
An error says that you just started the location updates without prompting for location authorization.
Your starting the location update before prompting for the location authorization. So just order of these lines is wrong.
1) Prompt user for location update
2) And then start update location
So your code should be in following order.
//1
[locationManager requestWhenInUseAuthorization];
[locationManager requestAlwaysAuthorization];
//2
[locationManager startUpdatingLocation];
Don't forget to add NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription key in Info.plist

CoreLocation , MapKit Error

I Tried To Show My Current Location In Mapkit and this is code
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//Make this controller the delegate for the map view.
self.MapView.delegate = self;
// Ensure that you can view your own location in the map view.
[self.MapView setShowsUserLocation:YES];
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
}
and I wrote This Delegate Of Mapkit
#pragma mark - MKMapViewDelegate methods.
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views {
MKCoordinateRegion region;
region = MKCoordinateRegionMakeWithDistance(locationManager.location.coordinate,2000,2000);
[mv setRegion:region animated:YES];
}
But I Get Wrong Location Iam In Egypt And This Give ME iam in UnitedStates
Any One Can Hep ME ???
Just in case: if you are using the simulator, the position will not be your real position. You have to set your location in the simulator:
Debug -> Location -> Custom Location (or anything else)
First of all.
You didnt used
[locationManager startUpdatingLocation];
Please add this line after you initiate the location manager variable. Also please let me know if you are seeing this location USA in Simulator.
If it is: Your simulator simulates the location given by user preferences. Get to the simulator window, there you'll get the option to show custom location as per your selection. By default it is selected as USA.
Hope it resolves your issue.

MKMapView not showing current location, or rather, any location (in simulator)

I have a MKMapView with it's delegate set to my controller (m) class.
Code below:
-(BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Create location manager object
locationManager = [[CLLocationManager alloc] init];
// Make this instance of WhereamiAppDelegate the delegate
// it will send its messages to our WhereamiApplDelegate
[locationManager setDelegate:self];
// We want all results from the location manager
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[mapView setShowsUserLocation:YES];
[window makeKeyAndVisible];
return YES;
}
However, all I am seeing is a map of the world without a blue annotation dot?
The simulator does not "really" support that. If you bypass enough checks, it will display Apple's HQ. Any GPS testing you need a real device.
EDIT: I believe that you can drop a pin and it will display that but as far as callbacks from CLLocationManager (via CLLocationManagerDelegate), it ain't happin in the simulator; at least the most recent ones. I can't speak for anything prior to 3.0...
self.mapview.mapType = MKMapTypeStandard;
self.mapview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.mapview.showsUserLocation = YES;
This is all you need if you have a pointer to your map called mapView, but as Merky said, you need a real device. What you can do though is, when the simulator is running, go to "Debug ->location -> own location" and input the coords of your desired coordinate.