User Current Location - ios

I want to receive in coordinates the users current location and I entered this code:
-(void)update{
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.delegate = self;
mapView.showsUserLocation = YES;
[locationManager startUpdatingLocation];
}
On iOS Simulator, this code is crashing and on my device it is not or sometimes its crashes the whole app.... can you help me on what is going on?

It looks like maybe you're not implementing the CLLocationManagerDelegate methods.
In particular -locationManager:didUpdateLocations: (from iOS 6) or -locationManager:didUpdateToLocation:fromLocation: in previous iOS versions.
Maybe (again your question is so vague) you only want to show the blue dot on a map view. In which case you don't need to deal with the Core Location Manager. Check out this post for more information.

1st of all You can't get your current Location in the Simulator . and try with this code for getting the co-ordinates of current Location.
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
if ([CLLocationManager locationServicesEnabled])
{
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
}
location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];;
MKCoordinateRegion region;
region.center=coordinate;
MKCoordinateSpan span;
span.latitudeDelta=10.015;
span.longitudeDelta=10.015;
region.span=span;
[mapView setRegion:region];
NSString *str=[[NSString alloc] initWithFormat:#" latitude:%f longitude:%f",coordinate.latitude,coordinate.longitude];
NSLog(#"%#",str);

Related

How to get current location latitude and longitude? [duplicate]

This question already has answers here:
How to get current location latitude and longitude in IOS SDK 8.0
(4 answers)
Closed 5 years ago.
I'm trying to get user current location.Here is my code:
-(CLLocationCoordinate2D) getLocation{
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
return coordinate;
}
and this is where i call it in viewDidLoad
CLLocationCoordinate2D coordinate = [self getLocation];
double latitude = coordinate.latitude;
double longitude = coordinate.longitude;
and it return 0.000000 for both latitude and longitude.
Any help please! I'm using Xcode 8 and running on My real iPhone 6s not Emulator.
You can use this code:
- (void)viewDidLoad
{
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
[self.locationManager requestWhenInUseAuthorization];
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *location = [locations lastObject];
NSLog(#"lat%f - lon%f", location.coordinate.latitude, location.coordinate.longitude);
}

iOS Get Location after allow location services permission

At the start of the application, the application request for user's current location. AlertBox pop up regarding about giving the app permission to access location services. My question is how do I get the location right after the user allows location services?
My Code
- (void)viewDidLoad
{
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[self.locationManager startUpdatingLocation];
latitude = [NSString stringWithFormat:#"%f",self.locationManager.location.coordinate.latitude];
longtitude = [NSString stringWithFormat:#"%f",self.locationManager.location.coordinate.longitude];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:[latitude floatValue]
longitude:[longtitude floatValue]
zoom:16];
mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView.delegate = self;
mapView.myLocationEnabled = YES;
self.view = mapView;
// Creates a marker in the center of the map.
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake([latitude intValue], [longtitude intValue]);
marker.title = #"Current Location";
marker.map = mapView;
}
Is there any way to get user's current location once it allows location services?
My codes only works when user allows the location services then restart the app.
I tried implementing the code below as well. But the NSLog is not appearing.
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *loc = locations.lastObject;
double speed = loc.speed;
NSLog(#"speed-----%f ", speed);
}
Any advice? Did I left out anything?
Just put:
self.locationManager.delegate = self;
After this line:
self.locationManager = [[CLLocationManager alloc] init];
Note:
Make sure you have added CLLocationManagerDelegate in your header file.
My answer comes a year too late and I'm using Swift 2 but for anyone else requiring an answer:
You need to update the mapview in the didUpdateLocations delegate. Please see the code below.
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
mapUIView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
locationManager.stopUpdatingLocation()
}
}

CLLocationCoordinate2D latitude & longitude always zero

I am trying to get latitude & longitude. I am running following code on actual device but I always get zero for latitude & longitude. I also import library and set delegate also. May I know what is wrong and how to do? My device has ios 8.
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
if ([locationManager locationServicesEnabled])
{
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
}
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
NSString *str=[[NSString alloc] initWithFormat:#" latitude:%f longitude:%f",coordinate.latitude,coordinate.longitude];
NSLog(#"%#",str);
you have to get the current location in delegate method
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *currentLocation = [locations objectAtIndex:0];
}

CLLocationManager fails to get the user's location in device but not in simulator

I'm using a CLLocationManager for a GPS tracking system in my iOS app. The app works fine with the debug feature of the simulator but once the build is in the device, it fails to do so. Giving me an error, "Failed to get location." This is how I did it:
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.pausesLocationUpdatesAutomatically = NO;
then the startupdating is in a uibutton. Any help will be gravely appreciated. Thank you.
First setup the location manager
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
And after that call delegate method
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations {
CLLocation *location = [locations lastObject];
NSLog(#"lat%f - lon%f", location.coordinate.latitude, location.coordinate.longitude);
currentLocationCoordinate.latitude = location.coordinate.latitude;
currentLocationCoordinate.longitude = location.coordinate.longitude;
[[NSNotificationCenter defaultCenter] postNotificationName:#"Address Found" object:self];
}
this is work for me . it will help you

trying to retrieve current location in iOS

I am building an app which requires me to find the distance between a location specified and my current location.
I am using CLLocationManager for the same but the coordinates retrieved are 0,0 despite me specifying a custom location in the simulator. Here's the code:
The .m file
#property (nonatomic, strong) CLLocationManager *locationManager2;
...
#synthesize locationManager2;
- (CLLocationManager *)locationManager2 {
if (locationManager2 != nil) {
return locationManager2;
}
locationManager2 = [[CLLocationManager alloc] init];
[locationManager2 setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
[locationManager2 setDelegate:self];
return locationManager2;
}
...
...
...
CLLocation *locationHome = [locationManager2 location];
NSLog(#"%f",locationHome.coordinate.latitude);
The lattitude is logged as zero.Anything wrong with my code?
Put this code and let me know what you are getting at console.
This code built on iOS 6.
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
if ([CLLocationManager locationServicesEnabled])
{
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
}
location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];;
MKCoordinateRegion region;
region.center=coordinate;
MKCoordinateSpan span;
span.latitudeDelta=10.015;
span.longitudeDelta=10.015;
region.span=span;
[mapView setRegion:region];
NSString *str=[[NSString alloc] initWithFormat:#" latitude:%f longitude:%f",coordinate.latitude,coordinate.longitude];
NSLog(#"%#",str);

Resources