location monitoring in a iphone app - iphone

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.

Related

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

How to get current longitude and latitude in xcode simulator? [duplicate]

This question already has answers here:
Set the location in iPhone Simulator
(17 answers)
Closed 9 years ago.
I am trying to get current long and lat by location but i am getting both 0.0000, the following code i am using
(CLLocationCoordinate2D) getLocation{
CLLocationManager *locationManager = [[[CLLocationManager alloc] init] autorelease];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
return coordinate;
}
And to call it use:
CLLocationCoordinate2D coordinate = [self getLocation];
NSString *latitude = [NSString stringWithFormat:#"%f", coordinate.latitude];
NSString *longitude = [NSString stringWithFormat:#"%f", coordinate.longitude];
NSLog(#"*dLatitude : %#", latitude);
NSLog(#"*dLongitude : %#",longitude);
i also added corelocation to the project.
please advise me.
Implement delegate method of location manager
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation: (CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
and you can get lat and lng using newLocation.coordinate.latitude and newLocation.coordinate.longitude
And for set current location in simulator you can follow #ios_av
You have to add Custom Location using this path:
Select iPhone Simulator and select Debug -->Loction --> Custom Location in menu
IPHone simulator always give 0.0000 for lat and long because it doesn't detect the location.
You can add Custom Location using this path:
Select iPhone Simulator and select Debug -->Loction --> Custom Location
try this
Run project in iPhone Simulator
Create in TextEdit file following file, call it MyOffice for example. Make extension as .gpx
Select in Xcode at the Simulate area Add GPX File to Project...
Add created file from menu to project.
Now you can see your location in Simulate area:
simulator can not return current location,its gives default california latitude and longitude location,but you can set statically location in simulator by - Debug -> Location -> Custom Location
in simulator you can force a local position: go to menu:debug:position and chose one of the voices (some simulates movements via a preregistered route) or enter coordinates of your choice
If you want to get you current location in your app then check this tutorial
http://www.techotopia.com/index.php/An_Example_iOS_5_iPhone_Location_Application
If you are not running this on device then add your latitude and longitude for simulator
for that check this
https://stackoverflow.com/a/12637537/2106973

Userlocation updated to wifi every 10 seconds

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.

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.