Userlocation updated to wifi every 10 seconds - iphone

I am trying to updated the userlocation every 10 secs from my code. But when I move from wifi to 3G then I face an issue. The every alternate request goes from the location where my wifi is located. What can be done?
Here is my code
NSNumber *n1;
NSNumber *n2;
if([ud boolForKey:#"applicationActive"])
{
n1 = [NSNumber numberWithDouble:[map.userLocation coordinate].latitude];
n2 = [NSNumber numberWithDouble:[map.userLocation coordinate].longitude];
}
else
{
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startUpdatingLocation];
n1 = [NSNumber numberWithDouble:locationManager.location.coordinate.latitude];
n2 = [NSNumber numberWithDouble:locationManager.location.coordinate.longitude];
[locationManager stopUpdatingLocation];
}
[dict setObject:[ud objectForKey:#"CurrentDriverID"] forKey:#"DriverId"];
[dict setObject:[n1 stringValue] forKey:#"Latitude"];
[dict setObject:[n2 stringValue] forKey:#"Longitude"];
[requestG SaveDriverlocationWS:dict delegate:self];
NOTE: The application stays in foreground when this happens. The SaveDriverlocationWS method saves the current location to the server

You don't seem to be setting an accuracy level for your location manager - I would try this and see if things improve. It looks from your code like you might be writing an app for drivers, in which case you may want to use the kCLLocationAccuracyBestForNavigation accuracy level (if you, you should use another more appropriate level, since the BestForNavigation filter uses up a lot of power and is designed for situations where the phone is in a car dock and powered).

For my understanding. using Wifi or 3G is system level setting. CLLocationManager uses current system's network to get device's location, while programmer has nowhere to switch network between 3G/Wifi.

Related

location monitoring in a iphone app

I am an newbie with iphone app development. I would like to capture location information without using GPS but only with Cell tower info and Wifi data. To enable this I am using the CLLocationManager to capture location information,
self.locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startMonitoringSignificantLocationChanges];
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *newLocation = [locations lastObject];
float latitude = newLocation.coordinate.latitude;
float longitude = newLocation.coordinate.longitude;
NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
// NSTimeInterval is defined as double
NSNumber *timeStampObj = [NSNumber numberWithDouble: timeStamp];
NSLog(#"latitude %f and longitude %f", latitude, longitude );
NSLog(#"%d", [timeStampObj integerValue]);
NSMutableArray *numArray = [[NSMutableArray alloc]initWithObjects:[NSNumber numberWithDouble:latitude],[NSNumber numberWithDouble:longitude], timeStampObj, nil];
}
When I use the above code when the app is launched I see that the app requests for accessing location services. Once this is accepted, I am not sure if the app is making use of GPS to get the location information as I see a small arrow pointing out, which seems as though GPS is being used to obtain the location information, which is not what I require. I would want the location information to be captured using WiFi and Cell Tower information only.
Also is there a frequency in which this location information gets updated, as there is an initial update of location after that I do not see the location getting modified even after the phone is being continuously moved.
If you use this:
[locationManager startMonitoringSignificantLocationChanges];
You are only getting GPS uploads when a significant change happens (for example, when you move from a telephony tower to other).
You cannot choose if you want to use GPS or towers from the code.

how to avoid calling location did update for three times

location did update calling three time and location did fail with error not call even when no data receive
how to avoid calling location did update three times and call did fail with error for no data receive?
im not taking about internet connection
How to avoid calling three times location did update?
for ios location update delegate may call several time depend upon on speed not three times
so use bool variable to indicate
How to call did fail with error for no internet connection?
you have set location manger delegate with self
How to avoid calling three times location did update?
Set location manager's delegate to nil in this method.
How to call did fail with error for no internet connection?
Why should it be called? Locations may come from three sources: GPS radio, cellular and WiFi, but not from the internet.
LocationManager didUpdateToLocation always check for change in location. If it fing multiple location it will call multiple time. You can avoid it in different way. But this will not update the location if user changes his current place.
You can user this code just to get location and proceed further.
-(void)getLocation{
locationManager = [[CLLocationManager alloc] init] ;
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
// [locationManager startUpdatingLocation]; commented to avoid multiple calling or you can say updating current location
CLLocation *newLocation=[locationManager location];
CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
NSString * location = ([placemarks count] > 0) ? [[placemarks objectAtIndex:0] locality] : #"Not Found";
NSLog(#"location:%#",location);
NSLog(#"lat:%f",locationManager.location.coordinate.latitude);
NSLog(#"long:%f",locationManager.location.coordinate.longitude);
}];
}

Can I change my location on the device to test a user's location?

I'm using the following code to get the current lat loc of the user. I would like to change the currentLocation of the user on the device to see how the map is populated with annotations. I would like to emulate the problem a tester of the app is having on the device in another country than I'm in. For him the map doesn't load the annotation whereas for me it works fine in the Simulator and the device (we're using the same device model etc.). I did try setting the currentUserLatitude currentUserLongitude to my tester's values but the map added the annotations fine for me.
-(void)loadMap{
CLLocationCoordinate2D coordinate = [self getLocation];
currentUserLatitude = [NSString stringWithFormat:#"%f", coordinate.latitude];
currentUserLongitude = [NSString stringWithFormat:#"%f", coordinate.longitude];
NSLog(#"*dLatitude : %#", currentUserLatitude);
NSLog(#"*dLongitude : %#",currentUserLongitude);
}
-(CLLocationCoordinate2D) getLocation{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
return coordinate;
}
You can't simulate a location on a physical device.
However, it is possible to test your app in the simulator and have it claim to be in whatever location you would like.
Yes. You can change your device location. Run your project and go to Xcode Debug Area Click on Simulate Location and select the location from list.Before that you need to allow simulate location from edit scheme
[![enter image description here][1]][1]
You can set coordinates manually
-(CLLocationCoordinate2D) getLocation{
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(47.1726, 9.5153);
return coordinate;
}
Or use simulator.

deferredLocationUpdate iOS 6

I have looked up about 3 different pages on how this works. But, I could really use some help because I am getting kCLError = 15. Here is what I have so far then I will explain more.
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self]
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager startUpdatingLocation];
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *currentLocation = [locations lastObject];
NSLog(#"%#",[locations lastObject];
NSLog(#"%d",[CLLocationManager deferredLocationUpdatesAvailable]);
[locationManager allowDeferredLocationUpdatesUntilTraveled:CLLocationDistanceMax timeout:CLTimeIntervalMax];
After this I have my error code
-(void)locationManager:(CLLocationManager *)manager didFinishDeferredUpdatesWithError:(NSError *)error
{
NSString *stringError = [NSString stringWithFormat:#"error: %#",[error description]];
_whatMonitor.text = stringError;
}
So, anyone that can help me I will seriously be so thankful. I have a count of the locations array as well, but this never changes from 1.. It is my understanding that after closing app to home screen and locking the device, the deferredUpdates should kick in. I have checked the [locations count] and it is still 1. I am expecting it to be greater than this..
I do not claim to be very good at this, so if I am making a careless mistake please let me know. I did not copy and paste so there may be some small typos. Thanks in advance.
I am running iOS 6.0 on an iPhone 5.
For significant change service and deferred location updates you have to move for the system to record an update. You have specified CLLocationDistanceMax which is a high value for distance. You can specify a lower distance to get more frequent changes for example you could specify every 100 meter change to trigger an update as follows:
[locationManager allowDeferredLocationUpdatesUntilTraveled:CLLocation(100) timeout:CLTimeIntervalMax];
Reference: http://developer.apple.com/library/ios/#documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html

Sending location to server with CLLocationManager when iphone is in background

i'm having trouble sending my position when the application lies in the background. I'm using CLLocationManager and startMonitoringSignificantLocationChanges. The posision didUpdateToLocation delegate method is performed once, but not more. I've tried to walk around but no new locations is sent to the server.
I have set the "Required background modes" -> "App registers for location updates" in the info.plist file.
Anyone got an idea on what might be wrong?
Code from where the tracking is started:
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = appDelegate;
[appDelegate setLocationManager:locationManager withDistanceFilter:kCLDistanceFilterNone];
[appDelegate.theLocationManager startMonitoringSignificantLocationChanges];
Code (from CLLocationManagerDelegate):
- (void)setLocationManager:(CLLocationManager*)locationManager withDistanceFilter:(CLLocationDistance)distanceFilter {
// create a new manager and start checking for sig changes
self.theLocationManager.delegate = nil;
[theLocationManager release];
self.theLocationManager = locationManager;
self.theLocationManager.delegate = self;
self.theLocationManager.distanceFilter = distanceFilter;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSDate *newLocationTimestamp = newLocation.timestamp;
NSDate *oldLocationTimestamp = oldLocation.timestamp;
int locationUpdateInterval = 15;//15 sec
if (!([newLocationTimestamp timeIntervalSinceDate:oldLocationTimestamp] < locationUpdateInterval)) {
//NSLog(#"New Location: %#", newLocation);
[self updateToLocation:newLocation];
}
}
- (void)updateToLocation:(CLLocation *)newLocation {
NSLog(#"update location!!");
NSString *latitude = [NSString stringWithFormat:#"%f", [newLocation coordinate].latitude];
NSString *longitude = [NSString stringWithFormat:#"%f", [newLocation coordinate].longitude];
[currentUser updatePositionWithLongitude:longitude andLatitude:latitude];
}
Like Bill Brasky said, the accuracy to which you have set your location manager is likely not registering the distance that you have walked. Try setting your location manager accuracy much higher, just to see if works, then dial it back down to a happy medium between accuracy and battery efficiency. Just for testing, take it all the way up:
[appDelegate.theLocationManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation];
Then instead of:
[appDelegate.theLocationManager startMonitoringSignificantLocationChanges];
try:
[appDelegate.theLocationManager startUpdatingLocation];
The -startMonitoringForSignificantLocationChanges is directly tied to cell tower connectivity. You may need to travel miles to get connection to a new tower and trigger a location change event. I know that the region monitoring is a bit more accurate as it uses updates of location from Wifi, cell tower, and even other apps that inquire on location. You will need to figure out how accurate and how often you need your app to be. You may need to actively monitor location in the background (which would be a battery killer for sure). Hope this helps.