I want to get the current location latitude and longitude without using the map, is it possible to get like that, I am not getting that from searching the internet, can any one help me to find that.
I tried this with using the core location but even I am not got any thing.
please tell me how to find the latitude and longitude, thank you.
//remember to stop before you are done, either here or in view disappearance.
- (void) dealloc
{ [locationManager stopUpdatingLocation]; }
in .h file:
CLLocationManager *locationMgr;
in .m file:on load
locationMgr =[[CLLocationManager alloc] init];
locationMgr.desiredAccuracy = kCLLocationAccuracyBest;
locationMgr.delegate = self;
if ([locationMgr respondsToSelector:#selector(requestWhenInUseAuthorization)]) {
[locationMgr requestWhenInUseAuthorization];
}
[locationMgr startUpdatingLocation];
add following delegates:
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
// Handle location updates
CLLocationCoordinate2D location=newLocation.coordinate;
float altitude = newLocation.altitude;
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
// Handle error
NSLog(#"error: %#",[error localizedDescription]);
}
may you are not authorising the location from user. thats why you are not getting anything
thanks
Related
My iOS app rejected from apple due to following reason :-
Your app enables the display of nearby users' locations on a map, but
does not have the required privacy precautions in place.
And they are sharing this screenshot of my app with rejection message.
In my app, I am using user’s location for showing near by events on the Map. Also I have enabled option showing user’s location on the Map.
I am using following code for setting up location services into my app.
//Declarting objects in .h file
CLGeocoder *geocoder;
CLPlacemark *placemark;
#property (nonatomic,retain) CLLocationManager *locationManager;
//Calling this method from viewDidLoad for setup location services..
-(void)getUserCurrentLocation
{
geocoder = [[CLGeocoder alloc] init];
self.locationManager=[[CLLocationManager alloc]init];
self.locationManager.delegate=self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
_locationManager.distanceFilter=kCLDistanceFilterNone;
if ([_locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)])
{
[_locationManager requestWhenInUseAuthorization];
}
[self.locationManager startUpdatingLocation];
}
-(void)StopUpdateLOcation
{
if([CLLocationManager locationServicesEnabled])
{
[self.locationManager stopUpdatingLocation];
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLLocation *currentLocation = newLocation;
if (currentLocation != nil)
{
//Saving location
[USERDEFAULT setValue:[NSNumber numberWithFloat:currentLocation.coordinate.latitude] forKey:#"CurrentLocationLatitude"];
[USERDEFAULT setValue:[NSNumber numberWithFloat:currentLocation.coordinate.longitude] forKey:#"CurrentLocationLongitude"];
//Here reverseGeocodeLocation method for fetching address from location
[self StopUpdateLOcation];
}
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"didFailWithError: %#", error);
}
Also I have added property “Privacy - Location When In Use Usage Description” with proper message. Also I have disabled the location service when got the location.
Not sure, why they are rejecting app. Did I miss something.?
How can I resolved this issue.?
Any help would be appreciated.
Finally, Got the proper solution for this. Thanks #Anbu.kartik for helping to findout this solution.
I have enabled the option for showing user’s location on the MAP. but didn’t wrote the failure delegate methods for handling, if user’s location not found.
So I write following delegate methods and send an app for approval again. And the app is approved by apple.
- (void)mapView:(MKMapView *)mapView didFailToLocateUserWithError:(NSError *)error{
NSLog(#"didFailToLocateUserWithError %#", error.description);
}
- (void)mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error{
NSLog(#"mapViewDidFailLoadingMap %#", error.description);
}
So we must have to write failure delegates for handling errors for all the functionalities. So never happens rejection of app related this type of issues.
Hope, this is what you're looking for. Any concern get back to me. :)
Have you added this message to iTunes store description:
Continued use of GPS running in the background can dramatically
decrease battery life.
e.g: check the last line in Google Maps description:
https://itunes.apple.com/us/app/google-maps-navigation-transit/id585027354?mt=8
I'm using Parse and with geoPointForCurrentLocationInBackground I can stop updating once a location is received without having to manually stop it.
How do I stop updating location immediately right after I receive location using CLLocationManager?
Edit
I know [self.locationManager stopUpdatingLocation]; stops it.
What I'm really asking is, how do I know I've received location for the first time then stop it immediately?
After getting your location, use this method:
[self.locationManager stopUpdatingLocation];
self.locationManager = nil;
Call stopUpdatingLocation as soon as your didUpdateLocations method is called.
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
[manager stopUpdatingLocation];
//store your location
self.location = [locations lastObject];
}
BOOL first_time = YES; // public
Every time you start updating location set first_time to YES:
first_time = YES;
[self.locationManager startUpdatingLocation];
in your didUpdateUserLocation method:
if (userLocation == nil) {
NSLog(#"User location is nil. maybe wating for permission");
} else if (!CLLocationCoordinate2DIsValid(userLocation.coordinate)) {
NSLog(#"User location is not valid 2d coordinates. maybe called in background");
} else {
NSLog(#"Did update user location: %f %f", userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude);
// here is the first time you receive user location
if (first_time)
{
first_time = NO;
[self.locationManager stopUpdatingLocation];
}
}
call below method to save and stop location after you get it once
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
self.location = [locations lastObject]
self.locationManager.delegate = nil;
[self.locationManager stopUpdatingLocation];
}
The opposite of stopMonitoringSignificantLocationChanges is not stopUpdatingLocation, it is startMonitoringSignificantLocationChanges.
Check out the CLLocation documentation for further detail.
Finally found the answer in another question...reprinting it here because it took me a while to stumble on it.
I had to call this:
[_locationManager stopMonitoringSignificantLocationChanges];
Even though I never called startMonitoringSignificantLocationChanges in the first place, seems I had to "un-call" it...strange that it works this way, but as soon as I added that line, location services shut down promptly. Hope this helps someone else.
Once current location update stop location manager using stopUpdatingLocation.
region.center = self.mapView.userLocation.coordinate;
if (region.center.longitude != 0 && region.center.latitude != 0) {
[self.locationManager stopUpdatingLocation];
}
self.yourLocationManager.stopUpdatingLocation()
In the info.plist :
We have added the NSLocationAlwaysUsageDescription.
On required background modes we have added ALL the bluetooth stuff( communicate and share data with bluetooth) and the location - "register for location updates"
We have imported the location and bluetooth frameworks ,and foundation.
We are starting the bluetooth with this :
if([self.locationManager respondsToSelector:#selector(requestAlwaysAuthorization)])
{
[self.locationManager requestAlwaysAuthorization];
}
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.beaconRegion.notifyOnEntry=YES;
self.beaconRegion.notifyOnExit=YES;
self.beaconRegion.notifyEntryStateOnDisplay=YES;
[self.locationManager requestAlwaysAuthorization ];
[self.locationManager requestWhenInUseAuthorization];
NSUUID *uuid_in = [[NSUUID alloc] initWithUUIDString:uuid];
self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid_in identifier:#"com.name.us"];
[self.locationManager startMonitoringForRegion:self.beaconRegion];
The delegate is called :
- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region {
NSLog(#"started:%#",region);
//[self.locationManager requestStateForRegion:self.beaconRegion];
self.beaconRegion.notifyEntryStateOnDisplay=YES;
}
Problem is that when we turn on the hardware beacon(which is working-checked with other apps) the delegates are not being called (iPhone6):
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
A few tips:
You should only do one of these two. Get rid of the second line if you have the first line. (You also have the same first line repeated above.)
[self.locationManager requestAlwaysAuthorization ];
[self.locationManager requestWhenInUseAuthorization];
No special background modes are needed.
Bluetooth framework is not needed.
You are modifying your CLBeaconRegion before it is initialized. The lines starting with self.beaconRegion.notifyOnEntry=YES; need to be moved down to be after initialization.
Make sure the ProximityUUID defined in uuid_in actually matches your beacon. Please post the code of how this is initialized so we can help be sure.
Try uninstalling and reinstalling your app. Make sure you get prompted for location permissions on first launch and accept. If you do not get prompted, something is wrong.
Couple things to check:
In privacy settings be sure that your app is checked to Allow Location Access. I'm not certain what will happen when you call both of these in your code:
[self.locationManager requestAlwaysAuthorization ];
[self.locationManager requestWhenInUseAuthorization];
Confirm UUID are you passing to initWithProximityUUID. This will need to match the beacons you are interested in finding.
Add monitoringDidFailForRegion to your code and see what you get...
(void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error {
NSLog(error);
}
I am having an issue with sending location to other users. I using Parse.com as my backend and I use this code to get a location:
-(void)sendLocation{
if ([locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)]){
[locationManager requestWhenInUseAuthorization];
}
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
[locationManager stopUpdatingLocation]; //kill it NOW or we have duplicates
if(!self.haveLocation) {
self.haveLocation=YES;
NSLog(#"didUpdateToLocation: %#", newLocation);
self.currentLocation = newLocation;
self.currentLocationGeoPoint= [PFGeoPoint geoPointWithLocation:self.currentLocation];
}
If I send myself a message from my current location and then open the message to view it, by comparing the current location circle with the pin dropped I can see they aren't in the same place and a fair distance apart.
I have the BOOL var haveLocation to make sure it is only refreshed once. Do I need to refresh it more times or something? Any pointers on how to make this more accurate would be really appreciated! Thanks
I tried following the apple example LocateMe:
I now have:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
if (locationAge > 5.0) return;
// test that the horizontal accuracy does not indicate an invalid measurement
if (newLocation.horizontalAccuracy < 0) return;
// test the measurement to see if it is more accurate than the previous measurement
if (self.bestEffortAtLocation == nil || self.bestEffortAtLocation.horizontalAccuracy > newLocation.horizontalAccuracy) {
// store the location as the "best effort"
self.bestEffortAtLocation = newLocation;
if (newLocation.horizontalAccuracy <= locationManager.desiredAccuracy) {
[locationManager stopUpdatingLocation];
//this code after getting the location
NSLog(#"didUpdateToLocation: %#", newLocation);
}
}
}
However when I tap send location it just runs forever and doesn't stop updating.. I have locationManager.desiredAccuracy = kCLLocationAccuracyBest; set too.
The location manager may send you several updates as it improves its accuracy. This is a feature designed to give you information it as as quickly as possible, while eventually getting the best information to you (at least up to the accuracy you requested). You are actively avoiding those updates, so you are likely getting the least accurate information.
You should check the horizontalAccuracy to determine if it is accurate enough for your use.
Note that you cannot compare horizontalAccuracy to kCLLocationAccuracyBest. "Best" is a constant -1. You need to compare it to what you need (in meters), and probably set a timeout to give up if it takes too long to get there (the system may not be able to provide you an arbitrarily accurate value.)
How can I calculate real time speed on device? I've googled a lot but all I got is to calculate distance and speed after completion of journey. Can I calculate speed at runtime?
Suggestions will be much appreciated.
Thanks in advance.
here CLLocationManager class provide different field of location like, Latitude, Longitude,accuracy and speed.
i use CoreLocationController so for location update i call this bellow method
you can get current speed in - (void)locationUpdate:(CLLocation *)location method like bellow
- (void)locationUpdate:(CLLocation *)location
{
NSString *currentspeed = [NSString stringWithFormat:#"SPEED: %f", [location speed]];
}
or otherwise bellow is delegate method
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(#"in update to location");
NSString *currentspeed = [NSString stringWithFormat:#"SPEED: %f", [newLocation speed]];
}
also you can get example from this link...
http://www.vellios.com/2010/08/16/core-location-gps-tutorial/
i hope this help you...
:)
There is a delegate CLLocationManagerDelegate add it to your controller header file.
Initialize Location Manager and set the delegate where you implement the delegate method for location Manager like this
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self; // send loc updates to current class
and you can write method in your class
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(#"Location Speed: %f", newLocation.speed);
}
That's it you will get your location updates in the above method with the speed also, and it will trigger as frequent as possible.